WC_Regenerate_Images::maybe_resize_image() public WC 1.0
Check if we should maybe generate a new image size if not already there.
{} Это метод класса: WC_Regenerate_Images{}
Хуки из метода
Возвращает
Массив.
Использование
$result = WC_Regenerate_Images::maybe_resize_image( $image, $attachment_id, $size, $icon );
- $image(массив) (обязательный)
- Properties of the image.
- $attachment_id(число) (обязательный)
- Attachment ID.
- $size(строка/массив) (обязательный)
- Image size.
- $icon(true/false) (обязательный)
- If icon or not.
Код WC_Regenerate_Images::maybe_resize_image() WC Regenerate Images::maybe resize image WC 5.0.0
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', 'shop_thumbnail', 'shop_catalog', 'shop_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;
}