forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize.php
More file actions
240 lines (174 loc) · 6.87 KB
/
resize.php
File metadata and controls
240 lines (174 loc) · 6.87 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
<?php
/**
* @group image
* @group media
* @group upload
* @group resize
*/
require_once __DIR__ . '/base.php';
abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase {
public function set_up() {
parent::set_up();
add_filter( 'wp_image_editors', array( $this, 'wp_image_editors' ) );
}
public function wp_image_editors() {
return array( $this->editor_engine );
}
public function test_resize_jpg() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/test-image.jpg', 25, 25 );
list( $w, $h, $type ) = getimagesize( $image );
unlink( $image );
$this->assertSame( 'test-image-25x25.jpg', wp_basename( $image ) );
$this->assertSame( 25, $w );
$this->assertSame( 25, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
}
public function test_resize_png() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/test-image.png', 25, 25 );
if ( ! is_string( $image ) ) { // WP_Error, stop GLib-GObject-CRITICAL assertion.
$this->fail( sprintf( 'No PNG support in the editor engine %s on this system.', $this->editor_engine ) );
}
list( $w, $h, $type ) = getimagesize( $image );
unlink( $image );
$this->assertSame( 'test-image-25x25.png', wp_basename( $image ) );
$this->assertSame( 25, $w );
$this->assertSame( 25, $h );
$this->assertSame( IMAGETYPE_PNG, $type );
}
public function test_resize_gif() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/test-image.gif', 25, 25 );
if ( ! is_string( $image ) ) { // WP_Error, stop GLib-GObject-CRITICAL assertion.
$this->fail( sprintf( 'No GIF support in the editor engine %s on this system.', $this->editor_engine ) );
}
list( $w, $h, $type ) = getimagesize( $image );
unlink( $image );
$this->assertSame( 'test-image-25x25.gif', wp_basename( $image ) );
$this->assertSame( 25, $w );
$this->assertSame( 25, $h );
$this->assertSame( IMAGETYPE_GIF, $type );
}
public function test_resize_webp() {
$file = DIR_TESTDATA . '/images/test-image.webp';
$editor = wp_get_image_editor( $file );
// Check if the editor supports the webp mime type.
if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/webp' ) ) {
$this->markTestSkipped( sprintf( 'No WebP support in the editor engine %s on this system.', $this->editor_engine ) );
}
$image = $this->resize_helper( $file, 25, 25 );
list( $w, $h, $type ) = wp_getimagesize( $image );
unlink( $image );
$this->assertSame( 'test-image-25x25.webp', wp_basename( $image ) );
$this->assertSame( 25, $w );
$this->assertSame( 25, $h );
$this->assertSame( IMAGETYPE_WEBP, $type );
}
/**
* Test resizing AVIF image.
*
* @ticket 51228
*/
public function test_resize_avif() {
$file = DIR_TESTDATA . '/images/avif-lossy.avif';
$editor = wp_get_image_editor( $file );
// Check if the editor supports the avif mime type.
if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/avif' ) ) {
$this->markTestSkipped( sprintf( 'No AVIF support in the editor engine %s on this system.', $this->editor_engine ) );
}
$image = $this->resize_helper( $file, 25, 25 );
list( $w, $h, $type ) = wp_getimagesize( $image );
unlink( $image );
$this->assertSame( 'avif-lossy-25x25.avif', wp_basename( $image ) );
$this->assertSame( 25, $w );
$this->assertSame( 25, $h );
$this->assertSame( IMAGETYPE_AVIF, $type );
}
public function test_resize_larger() {
// image_resize() should refuse to make an image larger.
$image = $this->resize_helper( DIR_TESTDATA . '/images/test-image.jpg', 100, 100 );
$this->assertInstanceOf( 'WP_Error', $image );
$this->assertSame( 'error_getting_dimensions', $image->get_error_code() );
}
public function test_resize_thumb_128x96() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG', 128, 96 );
list( $w, $h, $type ) = getimagesize( $image );
unlink( $image );
$this->assertSame( '2007-06-17DSC_4173-64x96.jpg', wp_basename( $image ) );
$this->assertSame( 64, $w );
$this->assertSame( 96, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
}
public function test_resize_thumb_128x0() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG', 128, 0 );
list( $w, $h, $type ) = getimagesize( $image );
unlink( $image );
$this->assertSame( '2007-06-17DSC_4173-128x193.jpg', wp_basename( $image ) );
$this->assertSame( 128, $w );
$this->assertSame( 193, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
}
public function test_resize_thumb_0x96() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG', 0, 96 );
list( $w, $h, $type ) = getimagesize( $image );
unlink( $image );
$this->assertSame( '2007-06-17DSC_4173-64x96.jpg', wp_basename( $image ) );
$this->assertSame( 64, $w );
$this->assertSame( 96, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
}
public function test_resize_thumb_150x150_crop() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG', 150, 150, true );
list( $w, $h, $type ) = getimagesize( $image );
unlink( $image );
$this->assertSame( '2007-06-17DSC_4173-150x150.jpg', wp_basename( $image ) );
$this->assertSame( 150, $w );
$this->assertSame( 150, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
}
public function test_resize_thumb_150x100_crop() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG', 150, 100, true );
list( $w, $h, $type ) = getimagesize( $image );
unlink( $image );
$this->assertSame( '2007-06-17DSC_4173-150x100.jpg', wp_basename( $image ) );
$this->assertSame( 150, $w );
$this->assertSame( 100, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
}
public function test_resize_thumb_50x150_crop() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG', 50, 150, true );
list( $w, $h, $type ) = getimagesize( $image );
unlink( $image );
$this->assertSame( '2007-06-17DSC_4173-50x150.jpg', wp_basename( $image ) );
$this->assertSame( 50, $w );
$this->assertSame( 150, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
}
/**
* Try resizing a non-existent image
*
* @ticket 6821
*/
public function test_resize_non_existent_image() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/test-non-existent-image.jpg', 25, 25 );
$this->assertInstanceOf( 'WP_Error', $image );
$this->assertSame( 'error_loading_image', $image->get_error_code() );
}
/**
* Function to help out the tests
*/
protected function resize_helper( $file, $width, $height, $crop = false ) {
$editor = wp_get_image_editor( $file );
if ( is_wp_error( $editor ) ) {
return $editor;
}
$resized = $editor->resize( $width, $height, $crop );
if ( is_wp_error( $resized ) ) {
return $resized;
}
$dest_file = $editor->generate_filename();
$saved = $editor->save( $dest_file );
if ( is_wp_error( $saved ) ) {
return $saved;
}
return $dest_file;
}
}