forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditorGd.php
More file actions
665 lines (557 loc) · 14.6 KB
/
editorGd.php
File metadata and controls
665 lines (557 loc) · 14.6 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
<?php
/**
* Test the WP_Image_Editor_GD class
*
* @group image
* @group media
* @group wp-image-editor-gd
*/
require_once __DIR__ . '/base.php';
class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
public $editor_engine = 'WP_Image_Editor_GD';
public function setUp() {
require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
// This needs to come after the mock image editor class is loaded.
parent::setUp();
}
public function tearDown() {
$folder = DIR_TESTDATA . '/images/waffles-*.jpg';
foreach ( glob( $folder ) as $file ) {
unlink( $file );
}
$this->remove_added_uploads();
parent::tearDown();
}
public function test_supports_mime_type_jpeg() {
$gd_image_editor = new WP_Image_Editor_GD( null );
$expected = (bool) ( imagetypes() & IMG_JPG );
$this->assertSame( $expected, $gd_image_editor->supports_mime_type( 'image/jpeg' ) );
}
public function test_supports_mime_type_png() {
$gd_image_editor = new WP_Image_Editor_GD( null );
$expected = (bool) ( imagetypes() & IMG_PNG );
$this->assertSame( $expected, $gd_image_editor->supports_mime_type( 'image/png' ) );
}
public function test_supports_mime_type_gif() {
$gd_image_editor = new WP_Image_Editor_GD( null );
$expected = (bool) ( imagetypes() & IMG_GIF );
$this->assertSame( $expected, $gd_image_editor->supports_mime_type( 'image/gif' ) );
}
/**
* Test resizing an image, not using crop
*
* @requires function imagejpeg
*/
public function test_resize() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
$gd_image_editor = new WP_Image_Editor_GD( $file );
$gd_image_editor->load();
$gd_image_editor->resize( 100, 50 );
$this->assertSame(
array(
'width' => 75,
'height' => 50,
),
$gd_image_editor->get_size()
);
}
/**
* Test multi_resize with single image resize and no crop
*
* @requires function imagejpeg
*/
public function test_single_multi_resize() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
$gd_image_editor = new WP_Image_Editor_GD( $file );
$gd_image_editor->load();
$sizes_array = array(
array(
'width' => 50,
'height' => 50,
),
);
$resized = $gd_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',
),
);
$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']
);
}
/**
* Ensure multi_resize doesn't 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';
$gd_image_editor = new WP_Image_Editor_GD( $file );
$gd_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 = $gd_image_editor->multi_resize( $sizes_array );
// If no images are generated, the returned array is empty.
$this->assertEmpty( $resized );
}
/**
* Test multi_resize with multiple sizes
*
* @ticket 26823
* @requires function imagejpeg
*/
public function test_multi_resize() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
$gd_image_editor = new WP_Image_Editor_GD( $file );
$gd_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 = $gd_image_editor->multi_resize( $sizes_array );
$expected_array = array(
// #0
array(
'file' => 'waffles-10x7.jpg',
'width' => 10,
'height' => 7,
'mime-type' => 'image/jpeg',
),
// #1
array(
'file' => 'waffles-75x50.jpg',
'width' => 75,
'height' => 50,
'mime-type' => 'image/jpeg',
),
// #2
array(
'file' => 'waffles-30x20.jpg',
'width' => 30,
'height' => 20,
'mime-type' => 'image/jpeg',
),
// #3
array(
'file' => 'waffles-45x400.jpg',
'width' => 45,
'height' => 400,
'mime-type' => 'image/jpeg',
),
// #4
array(
'file' => 'waffles-50x33.jpg',
'width' => 50,
'height' => 33,
'mime-type' => 'image/jpeg',
),
// #5
array(
'file' => 'waffles-55x37.jpg',
'width' => 55,
'height' => 37,
'mime-type' => 'image/jpeg',
),
// #6
array(
'file' => 'waffles-83x55.jpg',
'width' => 83,
'height' => 55,
'mime-type' => 'image/jpeg',
),
// #7
array(
'file' => 'waffles-90x60.jpg',
'width' => 90,
'height' => 60,
'mime-type' => 'image/jpeg',
),
// #8
array(
'file' => 'waffles-105x70.jpg',
'width' => 105,
'height' => 70,
'mime-type' => 'image/jpeg',
),
// #9
array(
'file' => 'waffles-200x133.jpg',
'width' => 200,
'height' => 133,
'mime-type' => 'image/jpeg',
),
);
$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']
);
}
}
/**
* Test resizing an image with cropping
*/
public function test_resize_and_crop() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
$gd_image_editor = new WP_Image_Editor_GD( $file );
$gd_image_editor->load();
$gd_image_editor->resize( 100, 50, true );
$this->assertSame(
array(
'width' => 100,
'height' => 50,
),
$gd_image_editor->get_size()
);
}
/**
* Test cropping an image.
*
* @ticket 51937
*
* @dataProvider data_crop
*/
public function test_crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
$file = DIR_TESTDATA . '/images/gradient-square.jpg';
$gd_image_editor = new WP_Image_Editor_GD( $file );
$gd_image_editor->load();
$gd_image_editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
$this->assertSame(
array(
'width' => (int) $src_w,
'height' => (int) $src_h,
),
$gd_image_editor->get_size()
);
}
public function data_crop() {
return array(
'src height and width must be greater than 0' => array(
'src_x' => 0,
'src_y' => 0,
'src_w' => 50,
'src_h' => 50,
),
'src height and width can be string but must be greater than 0' => array(
'src_x' => 10,
'src_y' => '10',
'src_w' => '50',
'src_h' => '50',
),
'dst height and width must be greater than 0' => array(
'src_x' => 10,
'src_y' => '10',
'src_w' => 150,
'src_h' => 150,
'dst_w' => 150,
'dst_h' => 150,
),
'dst height and width can be string but must be greater than 0' => array(
'src_x' => 10,
'src_y' => '10',
'src_w' => 150,
'src_h' => 150,
'dst_w' => '150',
'dst_h' => '150',
),
);
}
/**
* Test should return WP_Error when dimensions are not integer or are <= 0.
*
* @ticket 51937
*
* @dataProvider data_crop_invalid_dimensions
*/
public function test_crop_invalid_dimensions( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
$file = DIR_TESTDATA . '/images/gradient-square.jpg';
$gd_image_editor = new WP_Image_Editor_GD( $file );
$gd_image_editor->load();
$actual = $gd_image_editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
$this->assertInstanceOf( 'WP_Error', $actual );
$this->assertSame( 'image_crop_error', $actual->get_error_code() );
}
public function data_crop_invalid_dimensions() {
return array(
'src height must be greater than 0' => array(
'src_x' => 0,
'src_y' => 0,
'src_w' => 100,
'src_h' => 0,
),
'src width must be greater than 0' => array(
'src_x' => 10,
'src_y' => '10',
'src_w' => 0,
'src_h' => 100,
),
'src height must be numeric and greater than 0' => array(
'src_x' => 10,
'src_y' => '10',
'src_w' => 100,
'src_h' => 'NaN',
),
'dst height must be numeric and greater than 0' => array(
'src_x' => 0,
'src_y' => 0,
'src_w' => 100,
'src_h' => 50,
'dst_w' => '100',
'dst_h' => 'NaN',
),
'src and dst height and width must be greater than 0' => array(
'src_x' => 0,
'src_y' => 0,
'src_w' => 0,
'src_h' => 0,
'dst_w' => 0,
'dst_h' => 0,
),
'src and dst height and width can be string but must be greater than 0' => array(
'src_x' => 0,
'src_y' => 0,
'src_w' => '0',
'src_h' => '0',
'dst_w' => '0',
'dst_h' => '0',
),
);
}
/**
* Test rotating an image 180 deg
*/
public function test_rotate() {
$file = DIR_TESTDATA . '/images/gradient-square.jpg';
$gd_image_editor = new WP_Image_Editor_GD( $file );
$gd_image_editor->load();
$property = new ReflectionProperty( $gd_image_editor, 'image' );
$property->setAccessible( true );
$color_top_left = imagecolorat( $property->getValue( $gd_image_editor ), 0, 0 );
$gd_image_editor->rotate( 180 );
$this->assertSame( $color_top_left, imagecolorat( $property->getValue( $gd_image_editor ), 99, 99 ) );
}
/**
* Test flipping an image
*/
public function test_flip() {
$file = DIR_TESTDATA . '/images/gradient-square.jpg';
$gd_image_editor = new WP_Image_Editor_GD( $file );
$gd_image_editor->load();
$property = new ReflectionProperty( $gd_image_editor, 'image' );
$property->setAccessible( true );
$color_top_left = imagecolorat( $property->getValue( $gd_image_editor ), 0, 0 );
$gd_image_editor->flip( true, false );
$this->assertSame( $color_top_left, imagecolorat( $property->getValue( $gd_image_editor ), 0, 99 ) );
}
/**
* Test the image created with WP_Image_Editor_GD preserves alpha when resizing
*
* @ticket 23039
*/
public function test_image_preserves_alpha_on_resize() {
if ( ! ( imagetypes() & IMG_PNG ) ) {
$this->fail( 'This test requires PHP to be compiled with PNG support.' );
}
$file = DIR_TESTDATA . '/images/transparent.png';
$editor = wp_get_image_editor( $file );
$this->assertNotWPError( $editor );
$editor->load();
$editor->resize( 5, 5 );
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$editor->save( $save_to_file );
$this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), 127 );
unlink( $save_to_file );
}
/**
* Test the image created with WP_Image_Editor_GD preserves alpha with no resizing etc
*
* @ticket 23039
*/
public function test_image_preserves_alpha() {
if ( ! ( imagetypes() & IMG_PNG ) ) {
$this->fail( 'This test requires PHP to be compiled with PNG support.' );
}
$file = DIR_TESTDATA . '/images/transparent.png';
$editor = wp_get_image_editor( $file );
$this->assertNotWPError( $editor );
$editor->load();
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$editor->save( $save_to_file );
$this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), 127 );
unlink( $save_to_file );
}
/**
* @ticket 30596
*/
public function test_image_preserves_alpha_on_rotate() {
if ( ! ( imagetypes() & IMG_PNG ) ) {
$this->fail( 'This test requires PHP to be compiled with PNG support.' );
}
$file = DIR_TESTDATA . '/images/transparent.png';
$image = imagecreatefrompng( $file );
$rgb = imagecolorat( $image, 0, 0 );
$expected = imagecolorsforindex( $image, $rgb );
$editor = new WP_Image_Editor_GD( $file );
$editor->load();
$editor->rotate( 180 );
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$editor->save( $save_to_file );
$this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), $expected['alpha'] );
unlink( $save_to_file );
}
/**
* Test WP_Image_Editor_GD handles extension-less images
*
* @ticket 39195
*/
public function test_image_non_existent_extension() {
$image_editor = new WP_Image_Editor_GD( DIR_TESTDATA . '/images/test-image-no-extension' );
$result = $image_editor->load();
$this->assertTrue( $result );
}
}