wp_get_missing_image_subsizes

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

WordPress Version: 5.9

/**
 * Compare the existing image sub-sizes (as saved in the attachment meta)
 * to the currently registered image sub-sizes, and return the difference.
 *
 * Registered sub-sizes that are larger than the image are skipped.
 *
 * @since 5.3.0
 *
 * @param int $attachment_id The image attachment post ID.
 * @return array[] Associative array of arrays of image sub-size information for
 *                 missing image sizes, keyed by image size name.
 */
function wp_get_missing_image_subsizes($attachment_id)
{
    if (!wp_attachment_is_image($attachment_id)) {
        return array();
    }
    $registered_sizes = wp_get_registered_image_subsizes();
    $image_meta = wp_get_attachment_metadata($attachment_id);
    // Meta error?
    if (empty($image_meta)) {
        return $registered_sizes;
    }
    // Use the originally uploaded image dimensions as full_width and full_height.
    if (!empty($image_meta['original_image'])) {
        $image_file = wp_get_original_image_path($attachment_id);
        $imagesize = wp_getimagesize($image_file);
    }
    if (!empty($imagesize)) {
        $full_width = $imagesize[0];
        $full_height = $imagesize[1];
    } else {
        $full_width = (int) $image_meta['width'];
        $full_height = (int) $image_meta['height'];
    }
    $possible_sizes = array();
    // Skip registered sizes that are too large for the uploaded image.
    foreach ($registered_sizes as $size_name => $size_data) {
        if (image_resize_dimensions($full_width, $full_height, $size_data['width'], $size_data['height'], $size_data['crop'])) {
            $possible_sizes[$size_name] = $size_data;
        }
    }
    if (empty($image_meta['sizes'])) {
        $image_meta['sizes'] = array();
    }
    /*
     * Remove sizes that already exist. Only checks for matching "size names".
     * It is possible that the dimensions for a particular size name have changed.
     * For example the user has changed the values on the Settings -> Media screen.
     * However we keep the old sub-sizes with the previous dimensions
     * as the image may have been used in an older post.
     */
    $missing_sizes = array_diff_key($possible_sizes, $image_meta['sizes']);
    /**
     * Filters the array of missing image sub-sizes for an uploaded image.
     *
     * @since 5.3.0
     *
     * @param array[] $missing_sizes Associative array of arrays of image sub-size information for
     *                               missing image sizes, keyed by image size name.
     * @param array   $image_meta    The image meta data.
     * @param int     $attachment_id The image attachment post ID.
     */
    return apply_filters('wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id);
}

WordPress Version: 5.7

/**
 * Compare the existing image sub-sizes (as saved in the attachment meta)
 * to the currently registered image sub-sizes, and return the difference.
 *
 * Registered sub-sizes that are larger than the image are skipped.
 *
 * @since 5.3.0
 *
 * @param int $attachment_id The image attachment post ID.
 * @return array An array of the image sub-sizes that are currently defined but don't exist for this image.
 */
function wp_get_missing_image_subsizes($attachment_id)
{
    if (!wp_attachment_is_image($attachment_id)) {
        return array();
    }
    $registered_sizes = wp_get_registered_image_subsizes();
    $image_meta = wp_get_attachment_metadata($attachment_id);
    // Meta error?
    if (empty($image_meta)) {
        return $registered_sizes;
    }
    // Use the originally uploaded image dimensions as full_width and full_height.
    if (!empty($image_meta['original_image'])) {
        $image_file = wp_get_original_image_path($attachment_id);
        $imagesize = wp_getimagesize($image_file);
    }
    if (!empty($imagesize)) {
        $full_width = $imagesize[0];
        $full_height = $imagesize[1];
    } else {
        $full_width = (int) $image_meta['width'];
        $full_height = (int) $image_meta['height'];
    }
    $possible_sizes = array();
    // Skip registered sizes that are too large for the uploaded image.
    foreach ($registered_sizes as $size_name => $size_data) {
        if (image_resize_dimensions($full_width, $full_height, $size_data['width'], $size_data['height'], $size_data['crop'])) {
            $possible_sizes[$size_name] = $size_data;
        }
    }
    if (empty($image_meta['sizes'])) {
        $image_meta['sizes'] = array();
    }
    /*
     * Remove sizes that already exist. Only checks for matching "size names".
     * It is possible that the dimensions for a particular size name have changed.
     * For example the user has changed the values on the Settings -> Media screen.
     * However we keep the old sub-sizes with the previous dimensions
     * as the image may have been used in an older post.
     */
    $missing_sizes = array_diff_key($possible_sizes, $image_meta['sizes']);
    /**
     * Filters the array of missing image sub-sizes for an uploaded image.
     *
     * @since 5.3.0
     *
     * @param array $missing_sizes Array with the missing image sub-sizes.
     * @param array $image_meta    The image meta data.
     * @param int   $attachment_id The image attachment post ID.
     */
    return apply_filters('wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id);
}

WordPress Version: 5.4

/**
 * Compare the existing image sub-sizes (as saved in the attachment meta)
 * to the currently registered image sub-sizes, and return the difference.
 *
 * Registered sub-sizes that are larger than the image are skipped.
 *
 * @since 5.3.0
 *
 * @param int $attachment_id The image attachment post ID.
 * @return array An array of the image sub-sizes that are currently defined but don't exist for this image.
 */
function wp_get_missing_image_subsizes($attachment_id)
{
    if (!wp_attachment_is_image($attachment_id)) {
        return array();
    }
    $registered_sizes = wp_get_registered_image_subsizes();
    $image_meta = wp_get_attachment_metadata($attachment_id);
    // Meta error?
    if (empty($image_meta)) {
        return $registered_sizes;
    }
    // Use the originally uploaded image dimensions as full_width and full_height.
    if (!empty($image_meta['original_image'])) {
        $image_file = wp_get_original_image_path($attachment_id);
        $imagesize = @getimagesize($image_file);
    }
    if (!empty($imagesize)) {
        $full_width = $imagesize[0];
        $full_height = $imagesize[1];
    } else {
        $full_width = (int) $image_meta['width'];
        $full_height = (int) $image_meta['height'];
    }
    $possible_sizes = array();
    // Skip registered sizes that are too large for the uploaded image.
    foreach ($registered_sizes as $size_name => $size_data) {
        if (image_resize_dimensions($full_width, $full_height, $size_data['width'], $size_data['height'], $size_data['crop'])) {
            $possible_sizes[$size_name] = $size_data;
        }
    }
    if (empty($image_meta['sizes'])) {
        $image_meta['sizes'] = array();
    }
    /*
     * Remove sizes that already exist. Only checks for matching "size names".
     * It is possible that the dimensions for a particular size name have changed.
     * For example the user has changed the values on the Settings -> Media screen.
     * However we keep the old sub-sizes with the previous dimensions
     * as the image may have been used in an older post.
     */
    $missing_sizes = array_diff_key($possible_sizes, $image_meta['sizes']);
    /**
     * Filters the array of missing image sub-sizes for an uploaded image.
     *
     * @since 5.3.0
     *
     * @param array $missing_sizes Array with the missing image sub-sizes.
     * @param array $image_meta    The image meta data.
     * @param int   $attachment_id The image attachment post ID.
     */
    return apply_filters('wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id);
}

WordPress Version: 5.3

/**
 * Compare the existing image sub-sizes (as saved in the attachment meta)
 * to the currently registered image sub-sizes, and return the difference.
 *
 * Registered sub-sizes that are larger than the image are skipped.
 *
 * @since 5.3.0
 *
 * @param int $attachment_id The image attachment post ID.
 * @return array An array of the image sub-sizes that are currently defined but don't exist for this image.
 */
function wp_get_missing_image_subsizes($attachment_id)
{
    if (!wp_attachment_is_image($attachment_id)) {
        return array();
    }
    $registered_sizes = wp_get_registered_image_subsizes();
    $image_meta = wp_get_attachment_metadata($attachment_id);
    // Meta error?
    if (empty($image_meta)) {
        return $registered_sizes;
    }
    // Use the originally uploaded image dimensions as full_width and full_height.
    if (!empty($image_meta['original_image'])) {
        $image_file = wp_get_original_image_path($attachment_id);
        $imagesize = @getimagesize($image_file);
    }
    if (!empty($imagesize)) {
        $full_width = $imagesize[0];
        $full_height = $imagesize[1];
    } else {
        $full_width = (int) $image_meta['width'];
        $full_height = (int) $image_meta['height'];
    }
    $possible_sizes = array();
    // Skip registered sizes that are too large for the uploaded image.
    foreach ($registered_sizes as $size_name => $size_data) {
        if (image_resize_dimensions($full_width, $full_height, $size_data['width'], $size_data['height'], $size_data['crop'])) {
            $possible_sizes[$size_name] = $size_data;
        }
    }
    if (empty($image_meta['sizes'])) {
        $image_meta['sizes'] = array();
    }
    // Remove sizes that already exist. Only checks for matching "size names".
    // It is possible that the dimensions for a particular size name have changed.
    // For example the user has changed the values on the Settings -> Media screen.
    // However we keep the old sub-sizes with the previous dimensions
    // as the image may have been used in an older post.
    $missing_sizes = array_diff_key($possible_sizes, $image_meta['sizes']);
    /**
     * Filters the array of missing image sub-sizes for an uploaded image.
     *
     * @since 5.3.0
     *
     * @param array $missing_sizes Array with the missing image sub-sizes.
     * @param array $image_meta    The image meta data.
     * @param int   $attachment_id The image attachment post ID.
     */
    return apply_filters('wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id);
}