Skip to content

Commit db322a3

Browse files
authored
Merge pull request #1887 from b1ink0/fix/no-crop-if-crop-is-array
Fix Modern Image Format not cropping image if crop is an array
2 parents 84baae2 + 742cc2f commit db322a3

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

plugins/webp-uploads/helper.php

Lines changed: 8 additions & 8 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{string, 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{string, 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
}
@@ -241,7 +241,7 @@ function webp_uploads_generate_image_size( int $attachment_id, string $size, str
241241
}
242242

243243
if ( isset( $sizes[ $size ]['crop'] ) ) {
244-
$size_data['crop'] = (bool) $sizes[ $size ]['crop'];
244+
$size_data['crop'] = $sizes[ $size ]['crop'];
245245
}
246246

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

plugins/webp-uploads/tests/test-helper.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,56 @@ static function () {
344344
$this->assertSame( 'image_additional_generated_error', $result->get_error_code() );
345345
}
346346

347+
/**
348+
* Test that image is cropped correctly when crop is an array.
349+
*
350+
* @covers ::webp_uploads_generate_additional_image_source
351+
*/
352+
public function test_it_should_crop_image_with_array_crop_value(): void {
353+
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
354+
$this->markTestSkipped( 'Mime type image/webp is not supported.' );
355+
}
356+
357+
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
358+
$size_data = array(
359+
'width' => 300,
360+
'height' => 300,
361+
'crop' => array( 'left', 'top' ),
362+
);
363+
364+
$captured_crop = null;
365+
// Add filter to intercept the crop value.
366+
remove_all_filters( 'image_resize_dimensions' );
367+
add_filter(
368+
'image_resize_dimensions',
369+
static function ( $passthrough, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) use ( &$captured_crop ) {
370+
$captured_crop = $crop;
371+
return $passthrough;
372+
},
373+
10,
374+
6
375+
);
376+
377+
$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' );
378+
379+
$this->assertIsArray( $result );
380+
$this->assertArrayHasKey( 'filesize', $result );
381+
$this->assertArrayHasKey( 'file', $result );
382+
$this->assertStringEndsWith( 'image.webp', $result['file'] );
383+
$this->assertFileExists( '/tmp/image.webp' );
384+
385+
// Get image dimensions to verify crop worked.
386+
$image_editor = wp_get_image_editor( '/tmp/image.webp' );
387+
$size = $image_editor->get_size();
388+
389+
// Verify dimensions are as expected.
390+
$this->assertEquals( 300, $size['width'] );
391+
$this->assertEquals( 300, $size['height'] );
392+
393+
// Verify crop value is as expected.
394+
$this->assertEquals( array( 'left', 'top' ), $captured_crop );
395+
}
396+
347397
/**
348398
* Returns an empty array when the overwritten with empty array by webp_uploads_upload_image_mime_transforms filter.
349399
*/

0 commit comments

Comments
 (0)