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