WC_Regenerate_Images_Request::filter_image_sizes_to_only_missing_thumbnails()
Filters the list of thumbnail sizes to only include those which have missing files.
Method of the class: WC_Regenerate_Images_Request{}
No Hooks.
Return
Array
. An associative array of image sizes.
Usage
$WC_Regenerate_Images_Request = new WC_Regenerate_Images_Request(); $WC_Regenerate_Images_Request->filter_image_sizes_to_only_missing_thumbnails( $sizes, $metadata, $attachment_id );
- $sizes(array) (required)
- An associative array of registered thumbnail image sizes.
- $metadata(array) (required)
- An associative array of fullsize image metadata: width, height, file.
- $attachment_id(int)
- Attachment ID. Only passed from WP 5.0+.
Default: null
WC_Regenerate_Images_Request::filter_image_sizes_to_only_missing_thumbnails() WC Regenerate Images Request::filter image sizes to only missing thumbnails code WC 7.7.0
public function filter_image_sizes_to_only_missing_thumbnails( $sizes, $metadata, $attachment_id = null ) { $attachment_id = is_null( $attachment_id ) ? $this->attachment_id : $attachment_id; if ( ! $sizes || ! $attachment_id ) { return $sizes; } $fullsizepath = get_attached_file( $attachment_id ); $editor = wp_get_image_editor( $fullsizepath ); if ( is_wp_error( $editor ) ) { return $sizes; } $metadata = wp_get_attachment_metadata( $attachment_id ); // This is based on WP_Image_Editor_GD::multi_resize() and others. foreach ( $sizes as $size => $size_data ) { if ( empty( $metadata['sizes'][ $size ] ) ) { continue; } if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { continue; } if ( ! isset( $size_data['width'] ) ) { $size_data['width'] = null; } if ( ! isset( $size_data['height'] ) ) { $size_data['height'] = null; } if ( ! isset( $size_data['crop'] ) ) { $size_data['crop'] = false; } $image_sizes = getimagesize( $fullsizepath ); if ( false === $image_sizes ) { continue; } list( $orig_w, $orig_h ) = $image_sizes; $dimensions = image_resize_dimensions( $orig_w, $orig_h, $size_data['width'], $size_data['height'], $size_data['crop'] ); if ( ! $dimensions || ! is_array( $dimensions ) ) { continue; } $info = pathinfo( $fullsizepath ); $ext = $info['extension']; $dst_w = $dimensions[4]; $dst_h = $dimensions[5]; $suffix = "{$dst_w}x{$dst_h}"; $dst_rel_path = str_replace( '.' . $ext, '', $fullsizepath ); $thumbnail = "{$dst_rel_path}-{$suffix}.{$ext}"; if ( $dst_w === $metadata['sizes'][ $size ]['width'] && $dst_h === $metadata['sizes'][ $size ]['height'] && file_exists( $thumbnail ) ) { unset( $sizes[ $size ] ); } } return $sizes; }