Skip to content

Commit 83b0edf

Browse files
committed
Refactor image size handling to support additional crop data structure
1 parent 8050a1b commit 83b0edf

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

plugins/webp-uploads/helper.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ function webp_uploads_get_upload_image_mime_transforms(): array {
8282
* @since 1.0.0
8383
* @access private
8484
*
85-
* @param int $attachment_id The ID of the attachment from where this image would be created.
86-
* @param string $image_size The size name that would be used to create the image source, out of the registered subsizes.
87-
* @param array{ width: int, height: int, crop: bool } $size_data An array with the dimensions of the image: height, width and crop.
88-
* @param string $mime The target mime in which the image should be created.
89-
* @param string|null $destination_file_name The path where the file would be stored, including the extension. If null, `generate_filename` is used to create the destination file name.
85+
* @param int $attachment_id The ID of the attachment from where this image would be created.
86+
* @param string $image_size The size name that would be used to create the image source, out of the registered subsizes.
87+
* @param array{ width: int, height: int, crop: bool|array{0: string, 1: string}} $size_data An array with the dimensions of the image: height, width and crop.
88+
* @param string $mime The target mime in which the image should be created.
89+
* @param string|null $destination_file_name The path where the file would be stored, including the extension. If null, `generate_filename` is used to create the destination file name.
9090
*
9191
* @return array{ file: string, filesize: int }|WP_Error An array with the file and filesize if the image was created correctly, otherwise a WP_Error.
9292
*/
@@ -109,7 +109,7 @@ function webp_uploads_generate_additional_image_source( int $attachment_id, stri
109109
* @param array{
110110
* width: int,
111111
* height: int,
112-
* crop: bool
112+
* crop: bool|array{0: string, 1: string}
113113
* } $size_data An array with the dimensions of the image.
114114
* @param string $mime The target mime in which the image should be created.
115115
*/
@@ -156,7 +156,7 @@ function webp_uploads_generate_additional_image_source( int $attachment_id, stri
156156

157157
$height = isset( $size_data['height'] ) ? (int) $size_data['height'] : 0;
158158
$width = isset( $size_data['width'] ) ? (int) $size_data['width'] : 0;
159-
$crop = isset( $size_data['crop'] ) && $size_data['crop'];
159+
$crop = isset( $size_data['crop'] ) ? $size_data['crop'] : false;
160160
if ( $width <= 0 && $height <= 0 ) {
161161
return new WP_Error( 'image_wrong_dimensions', __( 'At least one of the dimensions must be a positive number.', 'webp-uploads' ) );
162162
}
@@ -240,8 +240,21 @@ function webp_uploads_generate_image_size( int $attachment_id, string $size, str
240240
$size_data['height'] = $metadata['sizes'][ $size ]['height'];
241241
}
242242

243-
if ( isset( $sizes[ $size ]['crop'] ) ) {
244-
$size_data['crop'] = (bool) $sizes[ $size ]['crop'];
243+
if (
244+
isset( $sizes[ $size ]['crop'] ) &&
245+
(
246+
(
247+
is_array( $sizes[ $size ]['crop'] ) &&
248+
count( $sizes[ $size ]['crop'] ) === 2 &&
249+
isset( $sizes[ $size ]['crop'][0] ) &&
250+
isset( $sizes[ $size ]['crop'][1] ) &&
251+
is_string( $sizes[ $size ]['crop'][0] ) &&
252+
is_string( $sizes[ $size ]['crop'][1] )
253+
) ||
254+
is_bool( $sizes[ $size ]['crop'] )
255+
)
256+
) {
257+
$size_data['crop'] = $sizes[ $size ]['crop'];
245258
}
246259

247260
return webp_uploads_generate_additional_image_source( $attachment_id, $size, $size_data, $mime );

0 commit comments

Comments
 (0)