WC_Regenerate_Images::maybe_resize_image()
Check if we should maybe generate a new image size if not already there.
Method of the class: WC_Regenerate_Images{}
Hooks from the method
Return
Array
.
Usage
$result = WC_Regenerate_Images::maybe_resize_image( $image, $attachment_id, $size, $icon );
- $image(array) (required)
- Properties of the image.
- $attachment_id(int) (required)
- Attachment ID.
- $size(string|array) (required)
- Image size.
- $icon(true|false) (required)
- If icon or not.
WC_Regenerate_Images::maybe_resize_image() WC Regenerate Images::maybe resize image code WC 9.3.1
public static function maybe_resize_image( $image, $attachment_id, $size, $icon ) { if ( ! apply_filters( 'woocommerce_resize_images', true ) ) { return $image; } // List of sizes we want to resize. Ignore others. if ( ! $image || ! in_array( $size, apply_filters( 'woocommerce_image_sizes_to_resize', array( 'woocommerce_thumbnail', 'woocommerce_gallery_thumbnail', 'woocommerce_single' ) ), true ) ) { return $image; } $target_size = wc_get_image_size( $size ); $image_width = $image[1]; $image_height = $image[2]; $ratio_match = false; $target_uncropped = '' === $target_size['width'] || '' === $target_size['height'] || ! $target_size['crop']; // If '' is passed to either size, we test ratios against the original file. It's uncropped. if ( $target_uncropped ) { $full_size = self::get_full_size_image_dimensions( $attachment_id ); if ( ! $full_size || ! $full_size['width'] || ! $full_size['height'] ) { return $image; } $ratio_match = wp_image_matches_ratio( $image_width, $image_height, $full_size['width'], $full_size['height'] ); } else { $ratio_match = wp_image_matches_ratio( $image_width, $image_height, $target_size['width'], $target_size['height'] ); } if ( ! $ratio_match ) { $full_size = self::get_full_size_image_dimensions( $attachment_id ); if ( ! $full_size ) { return $image; } // Check if the actual image has a larger dimension than the requested image size. Smaller images are not zoom-cropped. if ( $image_width === $target_size['width'] && $full_size['height'] < $target_size['height'] ) { return $image; } if ( $image_height === $target_size['height'] && $full_size['width'] < $target_size['width'] ) { return $image; } // If the full size image is smaller both ways, don't scale it up. if ( $full_size['height'] < $target_size['height'] && $full_size['width'] < $target_size['width'] ) { return $image; } return self::resize_and_return_image( $attachment_id, $image, $size, $icon ); } return $image; }