image_get_intermediate_size

The timeline below displays how wordpress function image_get_intermediate_size has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * 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
 * parameter $size value.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @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'.
 * @return array|false {
 *     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'])) {
        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 ((int) $data['width'] === (int) $size[0] && (int) $data['height'] === (int) $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 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'];
        } 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 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);
}

WordPress Version: 5.6

/**
 * 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
 * parameter $size value.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @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'.
 * @return array|false {
 *     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   Path of image relative to uploads directory.
 *     @type int    $width  Width of image in pixels.
 *     @type int    $height Height of image in pixels.
 *     @type string $path   Absolute filesystem path of image.
 *     @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'])) {
        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 ((int) $data['width'] === (int) $size[0] && (int) $data['height'] === (int) $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 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'];
        } 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 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);
}

WordPress Version: 5.5

/**
 * 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
 * parameter $size value.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param int          $post_id Attachment ID.
 * @param array|string $size    Optional. Image size. Accepts any valid image size, or an array
 *                              of width and height values in pixels (in that order).
 *                              Default 'thumbnail'.
 * @return array|false {
 *     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   Image's path relative to uploads directory
 *     @type int    $width  Width of image
 *     @type int    $height Height of image
 *     @type string $path   Image's absolute filesystem path.
 *     @type string $url    Image's URL.
 * }
 */
function image_get_intermediate_size($post_id, $size = 'thumbnail')
{
    $imagedata = wp_get_attachment_metadata($post_id);
    if (!$size || !is_array($imagedata) || 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 (intval($data['width']) === intval($size[0]) && intval($data['height']) === intval($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 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'];
        } 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);
}

WordPress Version: 5.4

/**
 * 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
 * parameter $size value.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param int          $post_id Attachment ID.
 * @param array|string $size    Optional. Image size. Accepts any valid image size, or an array
 *                              of width and height values in pixels (in that order).
 *                              Default 'thumbnail'.
 * @return array|false $data {
 *     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   Image's path relative to uploads directory
 *     @type int    $width  Width of image
 *     @type int    $height Height of image
 *     @type string $path   Image's absolute filesystem path.
 *     @type string $url    Image's URL.
 * }
 */
function image_get_intermediate_size($post_id, $size = 'thumbnail')
{
    $imagedata = wp_get_attachment_metadata($post_id);
    if (!$size || !is_array($imagedata) || 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 (intval($data['width']) === intval($size[0]) && intval($data['height']) === intval($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 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'];
        } 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);
}

WordPress Version: 5.3

/**
 * 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
 * parameter $size value.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param int          $post_id Attachment ID.
 * @param array|string $size    Optional. Image size. Accepts any valid image size, or an array
 *                              of width and height values in pixels (in that order).
 *                              Default 'thumbnail'.
 * @return false|array $data {
 *     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   Image's path relative to uploads directory
 *     @type int    $width  Width of image
 *     @type int    $height Height of image
 *     @type string $path   Image's absolute filesystem path.
 *     @type string $url    Image's URL.
 * }
 */
function image_get_intermediate_size($post_id, $size = 'thumbnail')
{
    $imagedata = wp_get_attachment_metadata($post_id);
    if (!$size || !is_array($imagedata) || 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 (intval($data['width']) === intval($size[0]) && intval($data['height']) === intval($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 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'];
        } 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);
}

WordPress Version: 5.2

/**
 * 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
 * parameter $size value.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param int          $post_id Attachment ID.
 * @param array|string $size    Optional. Image size. Accepts any valid image size, or an array
 *                              of width and height values in pixels (in that order).
 *                              Default 'thumbnail'.
 * @return false|array $data {
 *     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   Image's path relative to uploads directory
 *     @type int    $width  Width of image
 *     @type int    $height Height of image
 *     @type string $path   Image's absolute filesystem path.
 *     @type string $url    Image's URL.
 * }
 */
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 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'];
        } 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);
}

WordPress Version: 4.7

/**
 * 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
 * parameter $size value.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param int          $post_id Attachment ID.
 * @param array|string $size    Optional. Image size. Accepts any valid image size, or an array
 *                              of width and height values in pixels (in that order).
 *                              Default 'thumbnail'.
 * @return false|array $data {
 *     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   Image's path relative to uploads directory
 *     @type int    $width  Width of image
 *     @type int    $height Height of image
 *     @type string $path   Image's absolute filesystem path.
 *     @type string $url    Image's URL.
 * }
 */
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);
}

WordPress Version: 4.6

/**
 * 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
 * parameter $size value.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param int          $post_id Attachment ID.
 * @param array|string $size    Optional. Image size. Accepts any valid image size, or an array
 *                              of width and height values in pixels (in that order).
 *                              Default 'thumbnail'.
 * @return false|array $data {
 *     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   Image's path relative to uploads directory
 *     @type int    $width  Width of image
 *     @type int    $height Height of image
 *     @type string $path   Image's absolute filesystem path.
 *     @type string $url    Image's URL.
 * }
 */
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();
        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'])) {
        $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);
}

WordPress Version: 4.4

/**
 * 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
 * parameter $size value.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param int          $post_id Attachment ID.
 * @param array|string $size    Optional. Image size. Accepts any valid image size, or an array
 *                              of width and height values in pixels (in that order).
 *                              Default 'thumbnail'.
 * @return false|array $data {
 *     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   Image's path relative to uploads directory
 *     @type int    $width  Width of image
 *     @type int    $height Height of image
 *     @type string $path   Optional. Image's absolute filesystem path. Only returned if registered
 *                          size is passed to `$size` parameter.
 *     @type string $url    Optional. Image's URL. Only returned if registered size is passed to `$size`
 *                          parameter.
 * }
 */
function image_get_intermediate_size($post_id, $size = 'thumbnail')
{
    if (!is_array($imagedata = wp_get_attachment_metadata($post_id))) {
        return false;
    }
    // get the best one for a specified set of dimensions
    if (is_array($size) && !empty($imagedata['sizes'])) {
        $candidates = array();
        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]) {
                list($data['width'], $data['height']) = image_constrain_size_for_editor($data['width'], $data['height'], $size);
                /** This filter is documented in wp-includes/media.php */
                return apply_filters('image_get_intermediate_size', $data, $post_id, $size);
            }
            // If it's not an exact match but it's at least the dimensions requested.
            if ($data['width'] >= $size[0] && $data['height'] >= $size[1]) {
                $candidates[$data['width'] * $data['height']] = $_size;
            }
        }
        if (!empty($candidates)) {
            // find for the smallest image not smaller than the desired size
            ksort($candidates);
            foreach ($candidates as $_size) {
                $data = $imagedata['sizes'][$_size];
                // Skip images with unexpectedly divergent aspect ratios (crops)
                // First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop
                $maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false);
                // If the size doesn't match within one pixel, then it is of a different aspect ratio, so we skip it, unless it's the thumbnail size
                if ('thumbnail' != $_size && (!$maybe_cropped || $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] || $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'])) {
                    continue;
                }
                // If we're still here, then we're going to use this size.
                list($data['width'], $data['height']) = image_constrain_size_for_editor($data['width'], $data['height'], $size);
                /** This filter is documented in wp-includes/media.php */
                return apply_filters('image_get_intermediate_size', $data, $post_id, $size);
            }
        }
    }
    if (is_array($size) || empty($size) || empty($imagedata['sizes'][$size])) {
        return false;
    }
    $data = $imagedata['sizes'][$size];
    // include the full filesystem path of the intermediate file
    if (empty($data['path']) && !empty($data['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']);
    }
    /**
     * Filter 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);
}

WordPress Version: 4.3

/**
 * 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
 * parameter $size value.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param int          $post_id Attachment ID.
 * @param array|string $size    Optional. Registered image size to retrieve or flat array of height
 *                              and width dimensions. Default 'thumbnail'.
 * @return false|array False on failure or array of file path, width, and height on success.
 */
function image_get_intermediate_size($post_id, $size = 'thumbnail')
{
    if (!is_array($imagedata = wp_get_attachment_metadata($post_id))) {
        return false;
    }
    // get the best one for a specified set of dimensions
    if (is_array($size) && !empty($imagedata['sizes'])) {
        $areas = array();
        foreach ($imagedata['sizes'] as $_size => $data) {
            // already cropped to width or height; so use this size
            if ($data['width'] == $size[0] && $data['height'] <= $size[1] || $data['height'] == $size[1] && $data['width'] <= $size[0]) {
                $file = $data['file'];
                list($width, $height) = image_constrain_size_for_editor($data['width'], $data['height'], $size);
                return compact('file', 'width', 'height');
            }
            // add to lookup table: area => size
            $areas[$data['width'] * $data['height']] = $_size;
        }
        if (!$size || !empty($areas)) {
            // find for the smallest image not smaller than the desired size
            ksort($areas);
            foreach ($areas as $_size) {
                $data = $imagedata['sizes'][$_size];
                if ($data['width'] >= $size[0] || $data['height'] >= $size[1]) {
                    // Skip images with unexpectedly divergent aspect ratios (crops)
                    // First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop
                    $maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false);
                    // If the size doesn't match within one pixel, then it is of a different aspect ratio, so we skip it, unless it's the thumbnail size
                    if ('thumbnail' != $_size && (!$maybe_cropped || $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] || $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'])) {
                        continue;
                    }
                    // If we're still here, then we're going to use this size
                    $file = $data['file'];
                    list($width, $height) = image_constrain_size_for_editor($data['width'], $data['height'], $size);
                    return compact('file', 'width', 'height');
                }
            }
        }
    }
    if (is_array($size) || empty($size) || empty($imagedata['sizes'][$size])) {
        return false;
    }
    $data = $imagedata['sizes'][$size];
    // include the full filesystem path of the intermediate file
    if (empty($data['path']) && !empty($data['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']);
    }
    return $data;
}

WordPress Version: 4.2

/**
 * 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
 * parameter $size value.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param int          $post_id Attachment ID.
 * @param array|string $size    Optional. Registered image size to retrieve or flat array of height
 *                              and width dimensions. Default 'thumbnail'.
 * @return bool|array False on failure or array of file path, width, and height on success.
 */
function image_get_intermediate_size($post_id, $size = 'thumbnail')
{
    if (!is_array($imagedata = wp_get_attachment_metadata($post_id))) {
        return false;
    }
    // get the best one for a specified set of dimensions
    if (is_array($size) && !empty($imagedata['sizes'])) {
        $areas = array();
        foreach ($imagedata['sizes'] as $_size => $data) {
            // already cropped to width or height; so use this size
            if ($data['width'] == $size[0] && $data['height'] <= $size[1] || $data['height'] == $size[1] && $data['width'] <= $size[0]) {
                $file = $data['file'];
                list($width, $height) = image_constrain_size_for_editor($data['width'], $data['height'], $size);
                return compact('file', 'width', 'height');
            }
            // add to lookup table: area => size
            $areas[$data['width'] * $data['height']] = $_size;
        }
        if (!$size || !empty($areas)) {
            // find for the smallest image not smaller than the desired size
            ksort($areas);
            foreach ($areas as $_size) {
                $data = $imagedata['sizes'][$_size];
                if ($data['width'] >= $size[0] || $data['height'] >= $size[1]) {
                    // Skip images with unexpectedly divergent aspect ratios (crops)
                    // First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop
                    $maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false);
                    // If the size doesn't match within one pixel, then it is of a different aspect ratio, so we skip it, unless it's the thumbnail size
                    if ('thumbnail' != $_size && (!$maybe_cropped || $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] || $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'])) {
                        continue;
                    }
                    // If we're still here, then we're going to use this size
                    $file = $data['file'];
                    list($width, $height) = image_constrain_size_for_editor($data['width'], $data['height'], $size);
                    return compact('file', 'width', 'height');
                }
            }
        }
    }
    if (is_array($size) || empty($size) || empty($imagedata['sizes'][$size])) {
        return false;
    }
    $data = $imagedata['sizes'][$size];
    // include the full filesystem path of the intermediate file
    if (empty($data['path']) && !empty($data['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']);
    }
    return $data;
}

WordPress Version: 3.7

/**
 * Retrieve 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
 * parameter $size value.
 *
 * 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.
 *
 * @since 2.5.0
 * @see add_image_size()
 *
 * @param int $post_id Attachment ID for image.
 * @param array|string $size Optional, default is 'thumbnail'. Size of image, either array or string.
 * @return bool|array False on failure or array of file path, width, and height on success.
 */
function image_get_intermediate_size($post_id, $size = 'thumbnail')
{
    if (!is_array($imagedata = wp_get_attachment_metadata($post_id))) {
        return false;
    }
    // get the best one for a specified set of dimensions
    if (is_array($size) && !empty($imagedata['sizes'])) {
        foreach ($imagedata['sizes'] as $_size => $data) {
            // already cropped to width or height; so use this size
            if ($data['width'] == $size[0] && $data['height'] <= $size[1] || $data['height'] == $size[1] && $data['width'] <= $size[0]) {
                $file = $data['file'];
                list($width, $height) = image_constrain_size_for_editor($data['width'], $data['height'], $size);
                return compact('file', 'width', 'height');
            }
            // add to lookup table: area => size
            $areas[$data['width'] * $data['height']] = $_size;
        }
        if (!$size || !empty($areas)) {
            // find for the smallest image not smaller than the desired size
            ksort($areas);
            foreach ($areas as $_size) {
                $data = $imagedata['sizes'][$_size];
                if ($data['width'] >= $size[0] || $data['height'] >= $size[1]) {
                    // Skip images with unexpectedly divergent aspect ratios (crops)
                    // First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop
                    $maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false);
                    // If the size doesn't match within one pixel, then it is of a different aspect ratio, so we skip it, unless it's the thumbnail size
                    if ('thumbnail' != $_size && (!$maybe_cropped || $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] || $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'])) {
                        continue;
                    }
                    // If we're still here, then we're going to use this size
                    $file = $data['file'];
                    list($width, $height) = image_constrain_size_for_editor($data['width'], $data['height'], $size);
                    return compact('file', 'width', 'height');
                }
            }
        }
    }
    if (is_array($size) || empty($size) || empty($imagedata['sizes'][$size])) {
        return false;
    }
    $data = $imagedata['sizes'][$size];
    // include the full filesystem path of the intermediate file
    if (empty($data['path']) && !empty($data['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']);
    }
    return $data;
}