forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditorImagick.php
More file actions
940 lines (773 loc) · 25.5 KB
/
editorImagick.php
File metadata and controls
940 lines (773 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
<?php
/**
* Test the WP_Image_Editor_Imagick class
*
* @group image
* @group media
* @group wp-image-editor-imagick
*/
require_once __DIR__ . '/base.php';
class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
public $editor_engine = 'WP_Image_Editor_Imagick';
public function set_up() {
require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php';
// This needs to come after the mock image editor class is loaded.
parent::set_up();
}
public function tear_down() {
$folder = DIR_TESTDATA . '/images/waffles-*.jpg';
foreach ( glob( $folder ) as $file ) {
unlink( $file );
}
$this->remove_added_uploads();
parent::tear_down();
}
/**
* Tests support for ImageMagick compatible mime types.
*/
public function test_supports_mime_type() {
$imagick_image_editor = new WP_Image_Editor_Imagick( null );
$this->assertTrue( $imagick_image_editor->supports_mime_type( 'image/jpeg' ), 'Does not support image/jpeg' );
$this->assertTrue( $imagick_image_editor->supports_mime_type( 'image/png' ), 'Does not support image/png' );
$this->assertTrue( $imagick_image_editor->supports_mime_type( 'image/gif' ), 'Does not support image/gif' );
}
/**
* Tests resizing an image, not using crop.
*/
public function test_resize() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$imagick_image_editor->resize( 100, 50 );
$this->assertSame(
array(
'width' => 75,
'height' => 50,
),
$imagick_image_editor->get_size()
);
}
/**
* Tests multi_resize() with single image resize and no crop.
*/
public function test_single_multi_resize() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$sizes_array = array(
array(
'width' => 50,
'height' => 50,
),
);
$resized = $imagick_image_editor->multi_resize( $sizes_array );
// First, check to see if returned array is as expected.
$expected_array = array(
array(
'file' => 'waffles-50x33.jpg',
'width' => 50,
'height' => 33,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
),
);
$this->assertSame( $expected_array, $resized );
// Now, verify real dimensions are as expected.
$image_path = DIR_TESTDATA . '/images/' . $resized[0]['file'];
$this->assertImageDimensions(
$image_path,
$expected_array[0]['width'],
$expected_array[0]['height']
);
}
/**
* Tests that multi_resize() does not create an image when
* both height and weight are missing, null, or 0.
*
* @ticket 26823
*/
public function test_multi_resize_does_not_create() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$sizes_array = array(
array(
'width' => 0,
'height' => 0,
),
array(
'width' => 0,
'height' => 0,
'crop' => true,
),
array(
'width' => null,
'height' => null,
),
array(
'width' => null,
'height' => null,
'crop' => true,
),
array(
'width' => '',
'height' => '',
),
array(
'width' => '',
'height' => '',
'crop' => true,
),
array(
'width' => 0,
),
array(
'width' => 0,
'crop' => true,
),
array(
'width' => null,
),
array(
'width' => null,
'crop' => true,
),
array(
'width' => '',
),
array(
'width' => '',
'crop' => true,
),
);
$resized = $imagick_image_editor->multi_resize( $sizes_array );
// If no images are generated, the returned array is empty.
$this->assertEmpty( $resized );
}
/**
* Tests multi_resize() with multiple sizes.
*
* @ticket 26823
*/
public function test_multi_resize() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$sizes_array = array(
/*
* #0 - 10x10 resize, no cropping.
* By aspect, should be 10x6 output.
*/
array(
'width' => 10,
'height' => 10,
'crop' => false,
),
/*
* #1 - 75x50 resize, with cropping.
* Output dimensions should be 75x50
*/
array(
'width' => 75,
'height' => 50,
'crop' => true,
),
/*
* #2 - 20 pixel max height, no cropping.
* By aspect, should be 30x20 output.
*/
array(
'width' => 9999, // Arbitrary high value.
'height' => 20,
'crop' => false,
),
/*
* #3 - 45 pixel max height, with cropping.
* By aspect, should be 45x400 output.
*/
array(
'width' => 45,
'height' => 9999, // Arbitrary high value.
'crop' => true,
),
/*
* #4 - 50 pixel max width, no cropping.
* By aspect, should be 50x33 output.
*/
array(
'width' => 50,
),
/*
* #5 - 55 pixel max width, no cropping, null height
* By aspect, should be 55x36 output.
*/
array(
'width' => 55,
'height' => null,
),
/*
* #6 - 55 pixel max height, no cropping, no width specified.
* By aspect, should be 82x55 output.
*/
array(
'height' => 55,
),
/*
* #7 - 60 pixel max height, no cropping, null width.
* By aspect, should be 90x60 output.
*/
array(
'width' => null,
'height' => 60,
),
/*
* #8 - 70 pixel max height, no cropping, negative width.
* By aspect, should be 105x70 output.
*/
array(
'width' => -9999, // Arbitrary negative value.
'height' => 70,
),
/*
* #9 - 200 pixel max width, no cropping, negative height.
* By aspect, should be 200x133 output.
*/
array(
'width' => 200,
'height' => -9999, // Arbitrary negative value.
),
);
$resized = $imagick_image_editor->multi_resize( $sizes_array );
$expected_array = array(
// #0
array(
'file' => 'waffles-10x7.jpg',
'width' => 10,
'height' => 7,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ),
),
// #1
array(
'file' => 'waffles-75x50.jpg',
'width' => 75,
'height' => 50,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ),
),
// #2
array(
'file' => 'waffles-30x20.jpg',
'width' => 30,
'height' => 20,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ),
),
// #3
array(
'file' => 'waffles-45x400.jpg',
'width' => 45,
'height' => 400,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ),
),
// #4
array(
'file' => 'waffles-50x33.jpg',
'width' => 50,
'height' => 33,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
),
// #5
array(
'file' => 'waffles-55x37.jpg',
'width' => 55,
'height' => 37,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ),
),
// #6
array(
'file' => 'waffles-83x55.jpg',
'width' => 83,
'height' => 55,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ),
),
// #7
array(
'file' => 'waffles-90x60.jpg',
'width' => 90,
'height' => 60,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ),
),
// #8
array(
'file' => 'waffles-105x70.jpg',
'width' => 105,
'height' => 70,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ),
),
// #9
array(
'file' => 'waffles-200x133.jpg',
'width' => 200,
'height' => 133,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ),
),
);
$this->assertNotNull( $resized );
$this->assertSame( $expected_array, $resized );
foreach ( $resized as $key => $image_data ) {
$image_path = DIR_TESTDATA . '/images/' . $image_data['file'];
// Now, verify real dimensions are as expected.
$this->assertImageDimensions(
$image_path,
$expected_array[ $key ]['width'],
$expected_array[ $key ]['height']
);
}
}
/**
* Tests resizing an image with cropping.
*/
public function test_resize_and_crop() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$imagick_image_editor->resize( 100, 50, true );
$this->assertSame(
array(
'width' => 100,
'height' => 50,
),
$imagick_image_editor->get_size()
);
}
/**
* Tests cropping an image.
*/
public function test_crop() {
$file = DIR_TESTDATA . '/images/gradient-square.jpg';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$imagick_image_editor->crop( 0, 0, 50, 50 );
$this->assertSame(
array(
'width' => 50,
'height' => 50,
),
$imagick_image_editor->get_size()
);
}
/**
* Tests rotating an image 180 deg.
*/
public function test_rotate() {
$file = DIR_TESTDATA . '/images/one-blue-pixel-100x100.png';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$property = new ReflectionProperty( $imagick_image_editor, 'image' );
if ( PHP_VERSION_ID < 80100 ) {
$property->setAccessible( true );
}
$color_top_left = $property->getValue( $imagick_image_editor )->getImagePixelColor( 0, 0 )->getColor();
$imagick_image_editor->rotate( 180 );
$this->assertSame( $color_top_left, $property->getValue( $imagick_image_editor )->getImagePixelColor( 99, 99 )->getColor() );
}
/**
* Tests flipping an image.
*/
public function test_flip() {
$file = DIR_TESTDATA . '/images/one-blue-pixel-100x100.png';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$property = new ReflectionProperty( $imagick_image_editor, 'image' );
if ( PHP_VERSION_ID < 80100 ) {
$property->setAccessible( true );
}
$color_top_left = $property->getValue( $imagick_image_editor )->getImagePixelColor( 0, 0 )->getColor();
$imagick_image_editor->flip( true, false );
$this->assertSame( $color_top_left, $property->getValue( $imagick_image_editor )->getImagePixelColor( 0, 99 )->getColor() );
}
/**
* Tests that an image created with WP_Image_Editor_Imagick preserves alpha with no resizing.
*
* @ticket 24871
*/
public function test_image_preserves_alpha() {
$file = DIR_TESTDATA . '/images/transparent.png';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$imagick_image_editor->save( $save_to_file );
$im = new Imagick( $save_to_file );
$pixel = $im->getImagePixelColor( 0, 0 );
$expected = $pixel->getColorValue( imagick::COLOR_ALPHA );
$this->assertImageAlphaAtPointImagick( $save_to_file, array( 0, 0 ), $expected );
unlink( $save_to_file );
}
/**
* Tests that an image created with WP_Image_Editor_Imagick preserves alpha when resizing.
*
* @ticket 24871
*/
public function test_image_preserves_alpha_on_resize() {
$file = DIR_TESTDATA . '/images/transparent.png';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$imagick_image_editor->resize( 5, 5 );
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$imagick_image_editor->save( $save_to_file );
$im = new Imagick( $save_to_file );
$pixel = $im->getImagePixelColor( 0, 0 );
$expected = $pixel->getColorValue( imagick::COLOR_ALPHA );
$this->assertImageAlphaAtPointImagick( $save_to_file, array( 0, 0 ), $expected );
unlink( $save_to_file );
}
/**
* @ticket 30596
*/
public function test_image_preserves_alpha_on_rotate() {
$file = DIR_TESTDATA . '/images/transparent.png';
$pre_rotate_editor = new Imagick( $file );
$pre_rotate_pixel = $pre_rotate_editor->getImagePixelColor( 0, 0 );
$pre_rotate_alpha = $pre_rotate_pixel->getColorValue( imagick::COLOR_ALPHA );
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$pre_rotate_editor->writeImage( $save_to_file );
$pre_rotate_editor->destroy();
$imagick_image_editor = new WP_Image_Editor_Imagick( $save_to_file );
$imagick_image_editor->load();
$imagick_image_editor->rotate( 180 );
$imagick_image_editor->save( $save_to_file );
$this->assertImageAlphaAtPointImagick( $save_to_file, array( 0, 0 ), $pre_rotate_alpha );
unlink( $save_to_file );
}
/**
* Tests that WP_Image_Editor_Imagick handles extensionless images.
*
* @ticket 39195
*/
public function test_image_non_existent_extension() {
$imagick_image_editor = new WP_Image_Editor_Imagick( DIR_TESTDATA . '/images/test-image-no-extension' );
$loaded = $imagick_image_editor->load();
$this->assertTrue( $loaded );
}
/**
* Tests resetting Exif orientation data on rotate.
*
* @ticket 37140
* @requires function exif_read_data
*/
public function test_remove_orientation_data_on_rotate() {
$file = DIR_TESTDATA . '/images/test-image-upside-down.jpg';
$data = wp_read_image_metadata( $file );
// The orientation value 3 is equivalent to rotated upside down (180 degrees).
$this->assertSame( 3, (int) $data['orientation'], 'Orientation value read from does not match image file Exif data: ' . $file );
$temp_file = wp_tempnam( $file );
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
// Test a value that would not lead back to 1, as WP is resetting the value to 1 manually.
$imagick_image_editor->rotate( 90 );
$saved = $imagick_image_editor->save( $temp_file, 'image/jpeg' );
$data = wp_read_image_metadata( $saved['path'] );
// Remove both the generated file ending in .tmp and tmp.jpg due to wp_tempnam().
unlink( $temp_file );
unlink( $saved['path'] );
// Make sure the image is no longer in The Upside Down Exif orientation.
$this->assertSame( 1, (int) $data['orientation'], 'Orientation Exif data was not updated after rotating image: ' . $file );
}
/**
* Tests that images can be loaded and written over streams.
*/
public function test_streams() {
stream_wrapper_register( 'wptest', 'WP_Test_Stream' );
WP_Test_Stream::$data = array(
'Tests_Image_Editor_Imagick' => array(
'/read.jpg' => file_get_contents( DIR_TESTDATA . '/images/waffles.jpg' ),
),
);
$file = 'wptest://Tests_Image_Editor_Imagick/read.jpg';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$loaded = $imagick_image_editor->load();
$this->assertNotWPError( $loaded );
$temp_file = 'wptest://Tests_Image_Editor_Imagick/write.jpg';
$saved = $imagick_image_editor->save( $temp_file );
if ( $temp_file !== $saved['path'] ) {
unlink( $saved['path'] );
}
unlink( $temp_file );
$this->assertNotWPError( $saved );
$this->assertSame( $temp_file, $saved['path'] );
}
/**
* @ticket 51665
*/
public function test_directory_creation() {
$file = realpath( DIR_TESTDATA ) . '/images/a2-small.jpg';
$directory = realpath( DIR_TESTDATA ) . '/images/nonexistent-directory';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$this->assertFileDoesNotExist( $directory );
$loaded = $imagick_image_editor->load();
$this->assertNotWPError( $loaded );
$resized = $imagick_image_editor->resize( 100, 100, true );
$this->assertNotWPError( $resized );
$saved = $imagick_image_editor->save( $directory . '/a2-small-cropped.jpg' );
unlink( $directory . '/a2-small-cropped.jpg' );
rmdir( $directory );
$this->assertNotWPError( $saved );
}
/**
* Tests that the alpha channel of PDFs is removed from PDF previews.
*
* Only affects systems with Ghostscript version >= 9.14.
*
* @ticket 39216
*
* @covers WP_Image_Editor_Imagick::remove_pdf_alpha_channel
*/
public function test_remove_pdf_alpha_channel_should_remove_the_alpha_channel_in_preview() {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
$this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
}
$version = Imagick::getVersion();
if ( $version['versionNumber'] < 0x675 ) {
$this->markTestSkipped( 'The version of ImageMagick does not support removing alpha channels from PDFs.' );
}
$test_file = DIR_TESTDATA . '/images/test-alpha.pdf';
$attachment_id = $this->factory->attachment->create_upload_object( $test_file );
$this->assertNotEmpty( $attachment_id, 'The attachment was not created before testing.' );
$attached_file = get_attached_file( $attachment_id );
$this->assertNotEmpty( $attached_file, 'The attached file was not returned.' );
$rgb = array(
'r' => true,
'g' => true,
'b' => true,
);
// White.
$expected = array(
'r' => 1,
'g' => 1,
'b' => 1,
);
$check = image_get_intermediate_size( $attachment_id, 'full' );
$this->assertIsArray( $check, 'The intermediate size could not be retrieved.' );
$this->assertArrayHasKey( 'file', $check, 'The intermediate size file was not found.' );
$check_file = path_join( dirname( $attached_file ), $check['file'] );
$imagick = new Imagick( $check_file );
$output = array_map(
static function ( $value ) {
return (int) round( $value );
},
array_intersect_key( $imagick->getImagePixelColor( 100, 100 )->getColor( true /* normalized */ ), $rgb )
);
$imagick->destroy();
$this->assertSame( $expected, $output, 'The image color of the generated thumb does not match expected opaque background.' ); // Allow for floating point equivalence.
}
/**
* Test filter `image_max_bit_depth` correctly sets the maximum bit depth of resized images.
*
* @ticket 62285
*
* Temporarily disabled until we can figure out why it fails on the Trixie based PHP container.
* See https://core.trac.wordpress.org/ticket/63932.
* @requires PHP < 8.3
*/
public function test_image_max_bit_depth() {
$file = DIR_TESTDATA . '/images/colors_hdr_p3.avif';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
// Skip if AVIF not supported.
if ( ! $imagick_image_editor->supports_mime_type( 'image/avif' ) ) {
$this->markTestSkipped( 'The image editor does not support the AVIF mime type.' );
}
// Skip if depth methods not available.
if ( ! method_exists( 'Imagick', 'getImageDepth' ) || ! method_exists( 'Imagick', 'setImageDepth' ) ) {
$this->markTestSkipped( 'The image editor does not support get or setImageDepth.' );
}
// Verify source image has 10-bit depth.
$imagick = new Imagick( $file );
$this->assertSame( 10, $imagick->getImageDepth() );
// Test ability to save 10-bit image.
$imagick->setImageDepth( 10 );
$test_file = tempnam( get_temp_dir(), '' ) . 'test10.avif';
$imagick->writeImage( $test_file );
$im = new Imagick( $test_file );
if ( $im->getImageDepth() !== 10 ) {
$this->markTestSkipped( 'Imagick is unable to save a 10 bit image.' );
}
$im->destroy();
unlink( $test_file );
// Test default behavior preserves 10-bit depth.
$imagick_image_editor->load();
$imagick_image_editor->resize( 100, 50 );
$test_file = tempnam( get_temp_dir(), '' ) . 'test1.avif';
$imagick_image_editor->save( $test_file );
$im = new Imagick( $test_file );
$this->assertSame( 10, $im->getImageDepth() );
unlink( $test_file );
$im->destroy();
// Test filter can set 8-bit depth
add_filter( 'image_max_bit_depth', array( $this, '__return_eight' ) );
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$imagick_image_editor->resize( 100, 50 );
$test_file = tempnam( get_temp_dir(), '' ) . 'test2.avif';
$imagick_image_editor->save( $test_file );
$im = new Imagick( $test_file );
$this->assertSame( 8, $im->getImageDepth() );
unlink( $test_file );
$im->destroy();
}
/**
* Helper function to return 8 for the `image_max_bit_depth` filter.
*
* @return int
*/
public function __return_eight() {
return 8;
}
/**
* Test that resizes are smaller for 16 bit PNG images.
*
* @ticket 36477
*
* @dataProvider data_resizes_are_small_for_16bit_images
*
* @param string $file Path to the image file.
*/
public function test_resizes_are_small_for_16bit_images( $file ) {
// Temporarily disabled. See https://core.trac.wordpress.org/ticket/63932.
if ( DIR_TESTDATA . '/images/png-tests/test8.png' === $file ) {
$version = Imagick::getVersion();
if ( $version['versionNumber'] >= 0x700 ) {
$this->markTestSkipped( 'ImageMagick 7 is unable to optimize grayscale images with 1-bit transparency.' );
}
}
$temp_file = DIR_TESTDATA . '/images/test-temp.png';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$size = $imagick_image_editor->get_size();
$org_filesize = filesize( $file );
$imagick_image_editor->resize( $size['width'] * .5, $size['height'] * .5 );
$imagick_image_editor->save( $temp_file );
$new_filesize = filesize( $temp_file );
unlink( $temp_file );
$this->assertLessThan( $org_filesize, $new_filesize, 'The resized image file size is not smaller than the original file size.' );
}
/**
* Data provider for test_resizes_are_small_for_16bit_images.
*
* @return array[]
*/
public static function data_resizes_are_small_for_16bit_images() {
return array(
'cloudflare-status' => array(
DIR_TESTDATA . '/images/png-tests/cloudflare-status.png',
),
'deskcat8' => array(
DIR_TESTDATA . '/images/png-tests/deskcat8.png',
),
'17-c3-duplicate-entries' => array(
DIR_TESTDATA . '/images/png-tests/Palette_icon-or8.png',
),
'rabbit-time-paletted' => array(
DIR_TESTDATA . '/images/png-tests/rabbit-time-paletted-or8.png',
),
'test8' => array(
DIR_TESTDATA . '/images/png-tests/test8.png',
),
);
}
/**
* Tests that the 'png:IHDR.color-type-orig' property is preserved after resizing.
* Used to identify indexed PNG images, see https://www.w3.org/TR/PNG-Chunks.html#C.IHDR.
*
* @ticket 63448
*
* @dataProvider data_png_color_type_after_resize
*
* @param string $file_path Path to the image file.
* @param int $expected_color_type The expected original color type.
*/
public function test_png_color_type_is_preserved_after_resize( $file_path, $expected_color_type ) {
// Temporarily disabled. See https://core.trac.wordpress.org/ticket/63932.
if ( DIR_TESTDATA . '/images/png-tests/test8.png' === $file_path ) {
$version = Imagick::getVersion();
if ( $version['versionNumber'] >= 0x700 ) {
$this->markTestSkipped( 'ImageMagick 7 is unable to optimize grayscale images with 1-bit transparency.' );
}
}
$temp_file = DIR_TESTDATA . '/images/test-temp.png';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file_path );
$imagick_image_editor->load();
$size = $imagick_image_editor->get_size();
$imagick_image_editor->resize( $size['width'] * 0.5, $size['height'] * 0.5 );
$imagick_image_editor->save( $temp_file );
$imagick = new Imagick( $temp_file );
$actual_color_type = $imagick->getImageProperty( 'png:IHDR.color-type-orig' );
unlink( $temp_file );
$this->assertSame( (string) $expected_color_type, $actual_color_type, "The PNG original color type should be preserved after resize for {$file_path}." );
}
/**
* Data provider for test_png_color_type_is_preserved_after_resize.
*
* @return array[]
*/
public static function data_png_color_type_after_resize() {
return array(
'vivid-green-bird_color_type_6' => array(
DIR_TESTDATA . '/images/png-tests/vivid-green-bird.png',
6, // RGBA.
),
'grayscale-test-image_color_type_4' => array(
DIR_TESTDATA . '/images/png-tests/grayscale-test-image.png',
4, // Grayscale with Alpha.
),
'rabbit-time-paletted-or8_color_type_3' => array(
DIR_TESTDATA . '/images/png-tests/rabbit-time-paletted-or8.png',
3, // Paletted.
),
'test8_color_type_3' => array(
DIR_TESTDATA . '/images/png-tests/test8.png',
3, // Paletted.
),
);
}
/**
* Tests that alpha transparency is preserved after resizing.
*
* @ticket 63448
*
* @dataProvider data_alpha_transparency_is_preserved_after_resize
*
* @param string $file_path Path to the image file.
*/
public function test_alpha_transparency_is_preserved_after_resize( $file_path ) {
$temp_file = DIR_TESTDATA . '/images/test-temp.png';
$imagick_image_editor = new WP_Image_Editor_Imagick( $file_path );
$imagick_image_editor->load();
$size = $imagick_image_editor->get_size();
$imagick_image_editor->resize( $size['width'] * 0.5, $size['height'] * 0.5 );
$imagick_image_editor->save( $temp_file );
$imagick = new Imagick( $temp_file );
$alpha_channel_depth = $imagick->getImageChannelDepth( Imagick::CHANNEL_ALPHA );
unlink( $temp_file );
$this->assertGreaterThan( 1, $alpha_channel_depth, "Alpha transparency should be preserved after resize for {$file_path}." );
}
public static function data_alpha_transparency_is_preserved_after_resize() {
return array(
'oval-or8' => array(
DIR_TESTDATA . '/images/png-tests/oval-or8.png',
),
'oval-or8-grayscale-indexed' => array(
DIR_TESTDATA . '/images/png-tests/oval-or8-grayscale-indexed.png',
),
);
}
}