检索图像的中间大小(已调整大小)路径,宽度和高度。
原型
image_get_intermediate_size( int $post_id, array|string $size = 'thumbnail' )
描述
$ size参数可以是分别具有宽度和高度的数组。如果大小与’sizes’元数据数组的宽度和高度匹配,那么将使用它。如果没有直接匹配,则将使用大于指定大小的最近图像大小。如果找不到任何内容,则该函数将中断并返回false。
参数
$post_id
(int)
(Required)
附件ID。
$size
(array|string)
(Optional)
图片尺寸。接受任何有效的图像大小,或以像素为单位的宽度和高度值数组(按此顺序)。
返回值
(false|array)
$ data {成功时文件相对路径,宽度和高度的数组。如果已将注册大小传递给$ size参数,则还包括绝对路径和URL。失败时是假的。 @type string $ file图像相对于上传目录的路径@type int $ width图像的宽度@type int $ height图像的高度@type string $ path图像的绝对文件系统路径。 @type string $ url图片的URL。 }
源文件
路径:wp-includes/media.php
<?php
...
function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
if ( ! $size || ! is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) || empty( $imagedata['sizes'] ) ) {
return false;
}
$data = array();
// Find the best match when '$size' is an array.
if ( is_array( $size ) ) {
$candidates = array();
if ( ! isset( $imagedata['file'] ) && isset( $imagedata['sizes']['full'] ) ) {
$imagedata['height'] = $imagedata['sizes']['full']['height'];
$imagedata['width'] = $imagedata['sizes']['full']['width'];
}
foreach ( $imagedata['sizes'] as $_size => $data ) {
// If there's an exact match to an existing image size, short circuit.
if ( $data['width'] == $size[0] && $data['height'] == $size[1] ) {
$candidates[ $data['width'] * $data['height'] ] = $data;
break;
}
// If it's not an exact match, consider larger sizes with the same aspect ratio.
if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) {
// If '0' is passed to either size, we test ratios against the original file.
if ( 0 === $size[0] || 0 === $size[1] ) {
$same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $imagedata['width'], $imagedata['height'] );
} else {
$same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $size[0], $size[1] );
}
if ( $same_ratio ) {
$candidates[ $data['width'] * $data['height'] ] = $data;
}
}
}
if ( ! empty( $candidates ) ) {
// Sort the array by size if we have more than one candidate.
if ( 1 < count( $candidates ) ) {
ksort( $candidates );
}
$data = array_shift( $candidates );
/*
* When the size requested is smaller than the thumbnail dimensions, we
* fall back to the thumbnail size to maintain backwards compatibility with
* pre 4.6 versions of WordPress.
*/
} elseif ( ! empty( $imagedata['sizes']['thumbnail'] ) && $imagedata['sizes']['thumbnail']['width'] >= $size[0] && $imagedata['sizes']['thumbnail']['width'] >= $size[1] ) {
$data = $imagedata['sizes']['thumbnail'];
} else {
return false;
}
// Constrain the width and height attributes to the requested values.
list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
} elseif ( ! empty( $imagedata['sizes'][ $size ] ) ) {
$data = $imagedata['sizes'][ $size ];
}
// If we still don't have a match at this point, return false.
if ( empty( $data ) ) {
return false;
}
// include the full filesystem path of the intermediate file
if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) {
$file_url = wp_get_attachment_url($post_id);
$data['path'] = path_join( dirname($imagedata['file']), $data['file'] );
$data['url'] = path_join( dirname($file_url), $data['file'] );
}
/**
* Filters the output of image_get_intermediate_size()
*
* @since 4.4.0
*
* @see image_get_intermediate_size()
*
* @param array $data Array of file relative path, width, and height on success. May also include
* file absolute path and URL.
* @param int $post_id The post_id of the image attachment
* @param string|array $size Registered image size or flat array of initially-requested height and width
* dimensions (in that order).
*/
return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/image_get_intermediate_size/