: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @param int[] $dimensions {
* An array of width and height values.
* @type int $0 The width in pixels.
* @type int $1 The height in pixels.
* @param int $current_width The current width of the image.
* @param int $current_height The current height of the image.
* @param int $max_width The maximum width permitted.
* @param int $max_height The maximum height permitted.
return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height );
* Retrieves calculated resize dimensions for use in WP_Image_Editor.
* Calculates dimensions and coordinates for a resized image that fits
* within a specified width and height.
* @param int $orig_w Original width in pixels.
* @param int $orig_h Original height in pixels.
* @param int $dest_w New width in pixels.
* @param int $dest_h New height in pixels.
* @param bool|array $crop {
* Optional. Image cropping behavior. If false, the image will be scaled (default).
* If true, image will be cropped to the specified dimensions using center positions.
* If an array, the image will be cropped using the array to specify the crop location:
* @type string $0 The x crop position. Accepts 'left' 'center', or 'right'.
* @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'.
* @return array|false Returned array matches parameters for `imagecopyresampled()`. False on failure.
function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = false ) {
if ( $orig_w <= 0 || $orig_h <= 0 ) {
// At least one of $dest_w or $dest_h must be specific.
if ( $dest_w <= 0 && $dest_h <= 0 ) {
* Filters whether to preempt calculating the image resize dimensions.
* Returning a non-null value from the filter will effectively short-circuit
* image_resize_dimensions(), returning that value instead.
* @param null|mixed $null Whether to preempt output of the resize dimensions.
* @param int $orig_w Original width in pixels.
* @param int $orig_h Original height in pixels.
* @param int $dest_w New width in pixels.
* @param int $dest_h New height in pixels.
* @param bool|array $crop Whether to crop image to specified width and height or resize.
* An array can specify positioning of the crop area. Default false.
$output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop );
if ( null !== $output ) {
// Stop if the destination size is larger than the original image dimensions.
if ( empty( $dest_h ) ) {
if ( $orig_w < $dest_w ) {
} elseif ( empty( $dest_w ) ) {
if ( $orig_h < $dest_h ) {
if ( $orig_w < $dest_w && $orig_h < $dest_h ) {
* Crop the largest possible portion of the original image that we can size to $dest_w x $dest_h.
* Note that the requested crop dimensions are used as a maximum bounding box for the original image.
* If the original image's width or height is less than the requested width or height
* only the greater one will be cropped.
* For example when the original image is 600x300, and the requested crop dimensions are 400x400,
* the resulting image will be 400x300.
$aspect_ratio = $orig_w / $orig_h;
$new_w = min( $dest_w, $orig_w );
$new_h = min( $dest_h, $orig_h );
$new_w = (int) round( $new_h * $aspect_ratio );
$new_h = (int) round( $new_w / $aspect_ratio );
$size_ratio = max( $new_w / $orig_w, $new_h / $orig_h );
$crop_w = round( $new_w / $size_ratio );
$crop_h = round( $new_h / $size_ratio );
if ( ! is_array( $crop ) || count( $crop ) !== 2 ) {
$crop = array( 'center', 'center' );
} elseif ( 'right' === $x ) {
$s_x = $orig_w - $crop_w;
$s_x = floor( ( $orig_w - $crop_w ) / 2 );
} elseif ( 'bottom' === $y ) {
$s_y = $orig_h - $crop_h;
$s_y = floor( ( $orig_h - $crop_h ) / 2 );
// Resize using $dest_w x $dest_h as a maximum bounding box.
list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
if ( wp_fuzzy_number_match( $new_w, $orig_w ) && wp_fuzzy_number_match( $new_h, $orig_h ) ) {
// The new size has virtually the same dimensions as the original image.
* Filters whether to proceed with making an image sub-size with identical dimensions
* with the original/source image. Differences of 1px may be due to rounding and are ignored.
* @param bool $proceed The filtered value.
* @param int $orig_w Original image width.
* @param int $orig_h Original image height.
$proceed = (bool) apply_filters( 'wp_image_resize_identical_dimensions', false, $orig_w, $orig_h );
* The return array matches the parameters to imagecopyresampled().
* int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
* Resizes an image to make a thumbnail or intermediate size.
* The returned array has the file size, the image width, and image height. The
* {@see 'image_make_intermediate_size'} filter can be used to hook in and change the
* values of the returned array. The only parameter is the resized file path.
* @param string $file File path.
* @param int $width Image width.
* @param int $height Image height.
* @param bool|array $crop {
* Optional. Image cropping behavior. If false, the image will be scaled (default).
* If true, image will be cropped to the specified dimensions using center positions.
* If an array, the image will be cropped using the array to specify the crop location:
* @type string $0 The x crop position. Accepts 'left' 'center', or 'right'.
* @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'.
* @return array|false Metadata array on success. False if no image was created.
function image_make_intermediate_size( $file, $width, $height, $crop = false ) {
if ( $width || $height ) {
$editor = wp_get_image_editor( $file );
if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) ) {
$resized_file = $editor->save();
if ( ! is_wp_error( $resized_file ) && $resized_file ) {
unset( $resized_file['path'] );
* Helper function to test if aspect ratios for two images match.
* @param int $source_width Width of the first image in pixels.
* @param int $source_height Height of the first image in pixels.
* @param int $target_width Width of the second image in pixels.
* @param int $target_height Height of the second image in pixels.
* @return bool True if aspect ratios match within 1px. False if not.
function wp_image_matches_ratio( $source_width, $source_height, $target_width, $target_height ) {
* To test for varying crops, we constrain the dimensions of the larger image
* to the dimensions of the smaller image and see if they match.
if ( $source_width > $target_width ) {
$constrained_size = wp_constrain_dimensions( $source_width, $source_height, $target_width );
$expected_size = array( $target_width, $target_height );
$constrained_size = wp_constrain_dimensions( $target_width, $target_height, $source_width );
$expected_size = array( $source_width, $source_height );
// If the image dimensions are within 1px of the expected size, we consider it a match.
$matched = ( wp_fuzzy_number_match( $constrained_size[0], $expected_size[0] ) && wp_fuzzy_number_match( $constrained_size[1], $expected_size[1] ) );
* Retrieves the image's intermediate size (resized) path, width, and height.
* The $size parameter can be an array with the width and height respectively.
* If the size matches the 'sizes' metadata array for width and height, then it
* will be used. If there is no direct match, then the nearest image size larger
* than the specified size will be used. If nothing is found, then the function
* will break out and return false.
* The metadata 'sizes' is used for compatible sizes that can be used for the
* The url path will be given, when the $size parameter is a string.
* If you are passing an array for the $size, you should consider using
* add_image_size() so that a cropped version is generated. It's much more
* efficient than having to find the closest-sized image and then having the
* browser scale down the image.
* @param int $post_id Attachment ID.
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
* of width and height values in pixels (in that order). Default 'thumbnail'.
* Array of file relative path, width, and height on success. Additionally includes absolute
* path and URL if registered size is passed to `$size` parameter. False on failure.
* @type string $file Filename of image.
* @type int $width Width of image in pixels.
* @type int $height Height of image in pixels.
* @type string $path Path of image relative to uploads directory.
* @type string $url URL of image.
function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
$imagedata = wp_get_attachment_metadata( $post_id );
if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) {
// Find the best match when '$size' is an array.
if ( is_array( $size ) ) {
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 ( (int) $data['width'] === (int) $size[0] && (int) $data['height'] === (int) $size[1] ) {
$candidates[ $data['width'] * $data['height'] ] = $data;
// 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'] );
$same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $size[0], $size[1] );
$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 ) ) {
$data = array_shift( $candidates );
* When the size requested is smaller than the thumbnail dimensions, we
* fall back to the thumbnail size to maintain backward 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'];
// 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.
// 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()
* @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 ID of the image attachment.
* @param string|int[] $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size );
* Gets the available intermediate image size names.
* @return string[] An array of image size names.
function get_intermediate_image_sizes() {
$default_sizes = array( 'thumbnail', 'medium', 'medium_large', 'large' );
$additional_sizes = wp_get_additional_image_sizes();
if ( ! empty( $additional_sizes ) ) {
$default_sizes = array_merge( $default_sizes, array_keys( $additional_sizes ) );
* Filters the list of intermediate image sizes.
* @param string[] $default_sizes An array of intermediate image size names. Defaults
* are 'thumbnail', 'medium', 'medium_large', 'large'.
return apply_filters( 'intermediate_image_sizes', $default_sizes );
* Returns a normalized list of all currently registered image sub-sizes.
* @uses wp_get_additional_image_sizes()
* @uses get_intermediate_image_sizes()
* @return array[] Associative array of arrays of image sub-size information,
* keyed by image size name.
function wp_get_registered_image_subsizes() {
$additional_sizes = wp_get_additional_image_sizes();
foreach ( get_intermediate_image_sizes() as $size_name ) {
if ( isset( $additional_sizes[ $size_name ]['width'] ) ) {
// For sizes added by plugins and themes.
$size_data['width'] = (int) $additional_sizes[ $size_name ]['width'];
// For default sizes set in options.
$size_data['width'] = (int) get_option( "{$size_name}_size_w" );
if ( isset( $additional_sizes[ $size_name ]['height'] ) ) {
$size_data['height'] = (int) $additional_sizes[ $size_name ]['height'];
$size_data['height'] = (int) get_option( "{$size_name}_size_h" );
if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) {
if ( isset( $additional_sizes[ $size_name ]['crop'] ) ) {
$size_data['crop'] = $additional_sizes[ $size_name ]['crop'];
$size_data['crop'] = get_option( "{$size_name}_crop" );
if ( ! is_array( $size_data['crop'] ) || empty( $size_data['crop'] ) ) {
$size_data['crop'] = (bool) $size_data['crop'];
$all_sizes[ $size_name ] = $size_data;
* Retrieves an image to represent an attachment.
* @param int $attachment_id Image attachment ID.
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
* width and height values in pixels (in that order). Default 'thumbnail'.
* @param bool $icon Optional. Whether the image should fall back to a mime type icon. Default false.
* Array of image data, or boolean false if no image is available.
* @type string $0 Image source URL.
* @type int $1 Image width in pixels.
* @type int $2 Image height in pixels.
* @type bool $3 Whether the image is a resized image.
function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
// Get a thumbnail or intermediate image if there is one.
$image = image_downsize( $attachment_id, $size );
$src = wp_mime_type_icon( $attachment_id, '.svg' );
/** This filter is documented in wp-includes/post.php */
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
$src_file = $icon_dir . '/' . wp_basename( $src );
list( $width, $height ) = wp_getimagesize( $src_file );
$ext = strtolower( substr( $src_file, -4 ) );
// SVG does not have true dimensions, so this assigns width and height directly.
list( $width, $height ) = wp_getimagesize( $src_file );
if ( $src && $width && $height ) {
$image = array( $src, $width, $height, false );