0
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Base WordPress Image Editor |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Image_Editor |
|
7 |
*/ |
|
8 |
|
|
9 |
/** |
|
10 |
* Base image editor class from which implementations extend |
|
11 |
* |
|
12 |
* @since 3.5.0 |
|
13 |
*/ |
|
14 |
abstract class WP_Image_Editor { |
|
15 |
protected $file = null; |
|
16 |
protected $size = null; |
|
17 |
protected $mime_type = null; |
|
18 |
protected $default_mime_type = 'image/jpeg'; |
5
|
19 |
protected $quality = false; |
|
20 |
protected $default_quality = 90; |
0
|
21 |
|
|
22 |
/** |
|
23 |
* Each instance handles a single file. |
|
24 |
*/ |
|
25 |
public function __construct( $file ) { |
|
26 |
$this->file = $file; |
|
27 |
} |
|
28 |
|
|
29 |
/** |
|
30 |
* Checks to see if current environment supports the editor chosen. |
|
31 |
* Must be overridden in a sub-class. |
|
32 |
* |
|
33 |
* @since 3.5.0 |
|
34 |
* @access public |
|
35 |
* @abstract |
|
36 |
* |
|
37 |
* @param array $args |
|
38 |
* @return boolean |
|
39 |
*/ |
|
40 |
public static function test( $args = array() ) { |
|
41 |
return false; |
|
42 |
} |
|
43 |
|
|
44 |
/** |
|
45 |
* Checks to see if editor supports the mime-type specified. |
|
46 |
* Must be overridden in a sub-class. |
|
47 |
* |
|
48 |
* @since 3.5.0 |
|
49 |
* @access public |
|
50 |
* @abstract |
|
51 |
* |
|
52 |
* @param string $mime_type |
|
53 |
* @return boolean |
|
54 |
*/ |
|
55 |
public static function supports_mime_type( $mime_type ) { |
|
56 |
return false; |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* Loads image from $this->file into editor. |
|
61 |
* |
|
62 |
* @since 3.5.0 |
|
63 |
* @access protected |
|
64 |
* @abstract |
|
65 |
* |
|
66 |
* @return boolean|WP_Error True if loaded; WP_Error on failure. |
|
67 |
*/ |
|
68 |
abstract public function load(); |
|
69 |
|
|
70 |
/** |
|
71 |
* Saves current image to file. |
|
72 |
* |
|
73 |
* @since 3.5.0 |
|
74 |
* @access public |
|
75 |
* @abstract |
|
76 |
* |
|
77 |
* @param string $destfilename |
|
78 |
* @param string $mime_type |
|
79 |
* @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} |
|
80 |
*/ |
|
81 |
abstract public function save( $destfilename = null, $mime_type = null ); |
|
82 |
|
|
83 |
/** |
|
84 |
* Resizes current image. |
|
85 |
* |
5
|
86 |
* At minimum, either a height or width must be provided. |
|
87 |
* If one of the two is set to null, the resize will |
|
88 |
* maintain aspect ratio according to the provided dimension. |
|
89 |
* |
0
|
90 |
* @since 3.5.0 |
|
91 |
* @access public |
|
92 |
* @abstract |
|
93 |
* |
5
|
94 |
* @param int|null $max_w Image width. |
|
95 |
* @param int|null $max_h Image height. |
|
96 |
* @param boolean $crop |
0
|
97 |
* @return boolean|WP_Error |
|
98 |
*/ |
|
99 |
abstract public function resize( $max_w, $max_h, $crop = false ); |
|
100 |
|
|
101 |
/** |
|
102 |
* Resize multiple images from a single source. |
|
103 |
* |
|
104 |
* @since 3.5.0 |
|
105 |
* @access public |
|
106 |
* @abstract |
|
107 |
* |
|
108 |
* @param array $sizes { |
|
109 |
* An array of image size arrays. Default sizes are 'small', 'medium', 'large'. |
|
110 |
* |
|
111 |
* @type array $size { |
|
112 |
* @type int $width Image width. |
|
113 |
* @type int $height Image height. |
|
114 |
* @type bool $crop Optional. Whether to crop the image. Default false. |
|
115 |
* } |
|
116 |
* } |
|
117 |
* @return array An array of resized images metadata by size. |
|
118 |
*/ |
|
119 |
abstract public function multi_resize( $sizes ); |
|
120 |
|
|
121 |
/** |
|
122 |
* Crops Image. |
|
123 |
* |
|
124 |
* @since 3.5.0 |
|
125 |
* @access public |
|
126 |
* @abstract |
|
127 |
* |
|
128 |
* @param int $src_x The start x position to crop from. |
|
129 |
* @param int $src_y The start y position to crop from. |
|
130 |
* @param int $src_w The width to crop. |
|
131 |
* @param int $src_h The height to crop. |
|
132 |
* @param int $dst_w Optional. The destination width. |
|
133 |
* @param int $dst_h Optional. The destination height. |
|
134 |
* @param boolean $src_abs Optional. If the source crop points are absolute. |
|
135 |
* @return boolean|WP_Error |
|
136 |
*/ |
|
137 |
abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ); |
|
138 |
|
|
139 |
/** |
|
140 |
* Rotates current image counter-clockwise by $angle. |
|
141 |
* |
|
142 |
* @since 3.5.0 |
|
143 |
* @access public |
|
144 |
* @abstract |
|
145 |
* |
|
146 |
* @param float $angle |
|
147 |
* @return boolean|WP_Error |
|
148 |
*/ |
|
149 |
abstract public function rotate( $angle ); |
|
150 |
|
|
151 |
/** |
|
152 |
* Flips current image. |
|
153 |
* |
|
154 |
* @since 3.5.0 |
|
155 |
* @access public |
|
156 |
* @abstract |
|
157 |
* |
|
158 |
* @param boolean $horz Flip along Horizontal Axis |
|
159 |
* @param boolean $vert Flip along Vertical Axis |
|
160 |
* @return boolean|WP_Error |
|
161 |
*/ |
|
162 |
abstract public function flip( $horz, $vert ); |
|
163 |
|
|
164 |
/** |
|
165 |
* Streams current image to browser. |
|
166 |
* |
|
167 |
* @since 3.5.0 |
|
168 |
* @access public |
|
169 |
* @abstract |
|
170 |
* |
|
171 |
* @param string $mime_type |
|
172 |
* @return boolean|WP_Error |
|
173 |
*/ |
|
174 |
abstract public function stream( $mime_type = null ); |
|
175 |
|
|
176 |
/** |
|
177 |
* Gets dimensions of image. |
|
178 |
* |
|
179 |
* @since 3.5.0 |
|
180 |
* @access public |
|
181 |
* |
|
182 |
* @return array {'width'=>int, 'height'=>int} |
|
183 |
*/ |
|
184 |
public function get_size() { |
|
185 |
return $this->size; |
|
186 |
} |
|
187 |
|
|
188 |
/** |
|
189 |
* Sets current image size. |
|
190 |
* |
|
191 |
* @since 3.5.0 |
|
192 |
* @access protected |
|
193 |
* |
|
194 |
* @param int $width |
|
195 |
* @param int $height |
5
|
196 |
* @return true |
0
|
197 |
*/ |
|
198 |
protected function update_size( $width = null, $height = null ) { |
|
199 |
$this->size = array( |
|
200 |
'width' => (int) $width, |
|
201 |
'height' => (int) $height |
|
202 |
); |
|
203 |
return true; |
|
204 |
} |
|
205 |
|
|
206 |
/** |
5
|
207 |
* Gets the Image Compression quality on a 1-100% scale. |
|
208 |
* |
|
209 |
* @since 4.0.0 |
|
210 |
* @access public |
|
211 |
* |
|
212 |
* @return int $quality Compression Quality. Range: [1,100] |
|
213 |
*/ |
|
214 |
public function get_quality() { |
|
215 |
if ( ! $this->quality ) { |
|
216 |
$this->set_quality(); |
|
217 |
} |
|
218 |
|
|
219 |
return $this->quality; |
|
220 |
} |
|
221 |
|
|
222 |
/** |
0
|
223 |
* Sets Image Compression quality on a 1-100% scale. |
|
224 |
* |
|
225 |
* @since 3.5.0 |
|
226 |
* @access public |
|
227 |
* |
|
228 |
* @param int $quality Compression Quality. Range: [1,100] |
5
|
229 |
* @return boolean|WP_Error True if set successfully; WP_Error on failure. |
0
|
230 |
*/ |
5
|
231 |
public function set_quality( $quality = null ) { |
|
232 |
if ( null === $quality ) { |
|
233 |
/** |
|
234 |
* Filter the default image compression quality setting. |
|
235 |
* |
|
236 |
* Applies only during initial editor instantiation, or when set_quality() is run |
|
237 |
* manually without the `$quality` argument. |
|
238 |
* |
|
239 |
* set_quality() has priority over the filter. |
|
240 |
* |
|
241 |
* @since 3.5.0 |
|
242 |
* |
|
243 |
* @param int $quality Quality level between 1 (low) and 100 (high). |
|
244 |
* @param string $mime_type Image mime type. |
|
245 |
*/ |
|
246 |
$quality = apply_filters( 'wp_editor_set_quality', $this->default_quality, $this->mime_type ); |
0
|
247 |
|
5
|
248 |
if ( 'image/jpeg' == $this->mime_type ) { |
|
249 |
/** |
|
250 |
* Filter the JPEG compression quality for backward-compatibility. |
|
251 |
* |
|
252 |
* Applies only during initial editor instantiation, or when set_quality() is run |
|
253 |
* manually without the `$quality` argument. |
|
254 |
* |
|
255 |
* set_quality() has priority over the filter. |
|
256 |
* |
|
257 |
* The filter is evaluated under two contexts: 'image_resize', and 'edit_image', |
|
258 |
* (when a JPEG image is saved to file). |
|
259 |
* |
|
260 |
* @since 2.5.0 |
|
261 |
* |
|
262 |
* @param int $quality Quality level between 0 (low) and 100 (high) of the JPEG. |
|
263 |
* @param string $context Context of the filter. |
|
264 |
*/ |
|
265 |
$quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' ); |
|
266 |
} |
|
267 |
|
|
268 |
if ( $quality < 0 || $quality > 100 ) { |
|
269 |
$quality = $this->default_quality; |
|
270 |
} |
|
271 |
} |
|
272 |
|
|
273 |
// Allow 0, but squash to 1 due to identical images in GD, and for backwards compatibility. |
|
274 |
if ( 0 === $quality ) { |
|
275 |
$quality = 1; |
|
276 |
} |
|
277 |
|
|
278 |
if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) { |
|
279 |
$this->quality = $quality; |
|
280 |
return true; |
|
281 |
} else { |
|
282 |
return new WP_Error( 'invalid_image_quality', __('Attempted to set image quality outside of the range [1,100].') ); |
|
283 |
} |
0
|
284 |
} |
|
285 |
|
|
286 |
/** |
|
287 |
* Returns preferred mime-type and extension based on provided |
|
288 |
* file's extension and mime, or current file's extension and mime. |
|
289 |
* |
|
290 |
* Will default to $this->default_mime_type if requested is not supported. |
|
291 |
* |
|
292 |
* Provides corrected filename only if filename is provided. |
|
293 |
* |
|
294 |
* @since 3.5.0 |
|
295 |
* @access protected |
|
296 |
* |
|
297 |
* @param string $filename |
|
298 |
* @param string $mime_type |
|
299 |
* @return array { filename|null, extension, mime-type } |
|
300 |
*/ |
|
301 |
protected function get_output_format( $filename = null, $mime_type = null ) { |
5
|
302 |
$new_ext = null; |
0
|
303 |
|
|
304 |
// By default, assume specified type takes priority |
|
305 |
if ( $mime_type ) { |
|
306 |
$new_ext = $this->get_extension( $mime_type ); |
|
307 |
} |
|
308 |
|
|
309 |
if ( $filename ) { |
|
310 |
$file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ); |
|
311 |
$file_mime = $this->get_mime_type( $file_ext ); |
|
312 |
} |
|
313 |
else { |
|
314 |
// If no file specified, grab editor's current extension and mime-type. |
|
315 |
$file_ext = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) ); |
|
316 |
$file_mime = $this->mime_type; |
|
317 |
} |
|
318 |
|
|
319 |
// Check to see if specified mime-type is the same as type implied by |
|
320 |
// file extension. If so, prefer extension from file. |
|
321 |
if ( ! $mime_type || ( $file_mime == $mime_type ) ) { |
|
322 |
$mime_type = $file_mime; |
|
323 |
$new_ext = $file_ext; |
|
324 |
} |
|
325 |
|
|
326 |
// Double-check that the mime-type selected is supported by the editor. |
|
327 |
// If not, choose a default instead. |
|
328 |
if ( ! $this->supports_mime_type( $mime_type ) ) { |
|
329 |
/** |
|
330 |
* Filter default mime type prior to getting the file extension. |
|
331 |
* |
|
332 |
* @see wp_get_mime_types() |
|
333 |
* |
|
334 |
* @since 3.5.0 |
|
335 |
* |
|
336 |
* @param string $mime_type Mime type string. |
|
337 |
*/ |
|
338 |
$mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type ); |
|
339 |
$new_ext = $this->get_extension( $mime_type ); |
|
340 |
} |
|
341 |
|
|
342 |
if ( $filename ) { |
|
343 |
$ext = ''; |
|
344 |
$info = pathinfo( $filename ); |
|
345 |
$dir = $info['dirname']; |
|
346 |
|
|
347 |
if( isset( $info['extension'] ) ) |
|
348 |
$ext = $info['extension']; |
|
349 |
|
|
350 |
$filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}"; |
|
351 |
} |
|
352 |
|
|
353 |
return array( $filename, $new_ext, $mime_type ); |
|
354 |
} |
|
355 |
|
|
356 |
/** |
|
357 |
* Builds an output filename based on current file, and adding proper suffix |
|
358 |
* |
|
359 |
* @since 3.5.0 |
|
360 |
* @access public |
|
361 |
* |
|
362 |
* @param string $suffix |
|
363 |
* @param string $dest_path |
|
364 |
* @param string $extension |
|
365 |
* @return string filename |
|
366 |
*/ |
|
367 |
public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) { |
|
368 |
// $suffix will be appended to the destination filename, just before the extension |
|
369 |
if ( ! $suffix ) |
|
370 |
$suffix = $this->get_suffix(); |
|
371 |
|
|
372 |
$info = pathinfo( $this->file ); |
|
373 |
$dir = $info['dirname']; |
|
374 |
$ext = $info['extension']; |
|
375 |
|
|
376 |
$name = wp_basename( $this->file, ".$ext" ); |
|
377 |
$new_ext = strtolower( $extension ? $extension : $ext ); |
|
378 |
|
|
379 |
if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) ) |
|
380 |
$dir = $_dest_path; |
|
381 |
|
|
382 |
return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}"; |
|
383 |
} |
|
384 |
|
|
385 |
/** |
|
386 |
* Builds and returns proper suffix for file based on height and width. |
|
387 |
* |
|
388 |
* @since 3.5.0 |
|
389 |
* @access public |
|
390 |
* |
5
|
391 |
* @return false|string suffix |
0
|
392 |
*/ |
|
393 |
public function get_suffix() { |
|
394 |
if ( ! $this->get_size() ) |
|
395 |
return false; |
|
396 |
|
|
397 |
return "{$this->size['width']}x{$this->size['height']}"; |
|
398 |
} |
|
399 |
|
|
400 |
/** |
|
401 |
* Either calls editor's save function or handles file as a stream. |
|
402 |
* |
|
403 |
* @since 3.5.0 |
|
404 |
* @access protected |
|
405 |
* |
|
406 |
* @param string|stream $filename |
|
407 |
* @param callable $function |
|
408 |
* @param array $arguments |
|
409 |
* @return boolean |
|
410 |
*/ |
|
411 |
protected function make_image( $filename, $function, $arguments ) { |
|
412 |
if ( $stream = wp_is_stream( $filename ) ) { |
|
413 |
ob_start(); |
|
414 |
} else { |
|
415 |
// The directory containing the original file may no longer exist when using a replication plugin. |
|
416 |
wp_mkdir_p( dirname( $filename ) ); |
|
417 |
} |
|
418 |
|
|
419 |
$result = call_user_func_array( $function, $arguments ); |
|
420 |
|
|
421 |
if ( $result && $stream ) { |
|
422 |
$contents = ob_get_contents(); |
|
423 |
|
|
424 |
$fp = fopen( $filename, 'w' ); |
|
425 |
|
|
426 |
if ( ! $fp ) |
|
427 |
return false; |
|
428 |
|
|
429 |
fwrite( $fp, $contents ); |
|
430 |
fclose( $fp ); |
|
431 |
} |
|
432 |
|
|
433 |
if ( $stream ) { |
|
434 |
ob_end_clean(); |
|
435 |
} |
|
436 |
|
|
437 |
return $result; |
|
438 |
} |
|
439 |
|
|
440 |
/** |
|
441 |
* Returns first matched mime-type from extension, |
|
442 |
* as mapped from wp_get_mime_types() |
|
443 |
* |
|
444 |
* @since 3.5.0 |
|
445 |
* @access protected |
|
446 |
* |
|
447 |
* @param string $extension |
|
448 |
* @return string|boolean |
|
449 |
*/ |
|
450 |
protected static function get_mime_type( $extension = null ) { |
|
451 |
if ( ! $extension ) |
|
452 |
return false; |
|
453 |
|
|
454 |
$mime_types = wp_get_mime_types(); |
|
455 |
$extensions = array_keys( $mime_types ); |
|
456 |
|
|
457 |
foreach( $extensions as $_extension ) { |
|
458 |
if ( preg_match( "/{$extension}/i", $_extension ) ) { |
|
459 |
return $mime_types[$_extension]; |
|
460 |
} |
|
461 |
} |
|
462 |
|
|
463 |
return false; |
|
464 |
} |
|
465 |
|
|
466 |
/** |
|
467 |
* Returns first matched extension from Mime-type, |
|
468 |
* as mapped from wp_get_mime_types() |
|
469 |
* |
|
470 |
* @since 3.5.0 |
|
471 |
* @access protected |
|
472 |
* |
|
473 |
* @param string $mime_type |
|
474 |
* @return string|boolean |
|
475 |
*/ |
|
476 |
protected static function get_extension( $mime_type = null ) { |
|
477 |
$extensions = explode( '|', array_search( $mime_type, wp_get_mime_types() ) ); |
|
478 |
|
|
479 |
if ( empty( $extensions[0] ) ) |
|
480 |
return false; |
|
481 |
|
|
482 |
return $extensions[0]; |
|
483 |
} |
|
484 |
} |
|
485 |
|