@@ -344,6 +344,56 @@ static function () {
344
344
$ this ->assertSame ( 'image_additional_generated_error ' , $ result ->get_error_code () );
345
345
}
346
346
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
+
347
397
/**
348
398
* Returns an empty array when the overwritten with empty array by webp_uploads_upload_image_mime_transforms filter.
349
399
*/
0 commit comments