15 protected $file = null; |
15 protected $file = null; |
16 protected $size = null; |
16 protected $size = null; |
17 protected $mime_type = null; |
17 protected $mime_type = null; |
18 protected $default_mime_type = 'image/jpeg'; |
18 protected $default_mime_type = 'image/jpeg'; |
19 protected $quality = false; |
19 protected $quality = false; |
20 protected $default_quality = 90; |
20 protected $default_quality = 82; |
21 |
21 |
22 /** |
22 /** |
23 * Each instance handles a single file. |
23 * Each instance handles a single file. |
|
24 * |
|
25 * @param string $file Path to the file to load. |
24 */ |
26 */ |
25 public function __construct( $file ) { |
27 public function __construct( $file ) { |
26 $this->file = $file; |
28 $this->file = $file; |
27 } |
29 } |
28 |
30 |
29 /** |
31 /** |
30 * Checks to see if current environment supports the editor chosen. |
32 * Checks to see if current environment supports the editor chosen. |
31 * Must be overridden in a sub-class. |
33 * Must be overridden in a sub-class. |
32 * |
34 * |
33 * @since 3.5.0 |
35 * @since 3.5.0 |
34 * @access public |
36 * |
|
37 * @static |
35 * @abstract |
38 * @abstract |
36 * |
39 * |
37 * @param array $args |
40 * @param array $args |
38 * @return boolean |
41 * @return bool |
39 */ |
42 */ |
40 public static function test( $args = array() ) { |
43 public static function test( $args = array() ) { |
41 return false; |
44 return false; |
42 } |
45 } |
43 |
46 |
44 /** |
47 /** |
45 * Checks to see if editor supports the mime-type specified. |
48 * Checks to see if editor supports the mime-type specified. |
46 * Must be overridden in a sub-class. |
49 * Must be overridden in a sub-class. |
47 * |
50 * |
48 * @since 3.5.0 |
51 * @since 3.5.0 |
49 * @access public |
52 * |
|
53 * @static |
50 * @abstract |
54 * @abstract |
51 * |
55 * |
52 * @param string $mime_type |
56 * @param string $mime_type |
53 * @return boolean |
57 * @return bool |
54 */ |
58 */ |
55 public static function supports_mime_type( $mime_type ) { |
59 public static function supports_mime_type( $mime_type ) { |
56 return false; |
60 return false; |
57 } |
61 } |
58 |
62 |
59 /** |
63 /** |
60 * Loads image from $this->file into editor. |
64 * Loads image from $this->file into editor. |
61 * |
65 * |
62 * @since 3.5.0 |
66 * @since 3.5.0 |
63 * @access protected |
67 * @abstract |
64 * @abstract |
68 * |
65 * |
69 * @return bool|WP_Error True if loaded; WP_Error on failure. |
66 * @return boolean|WP_Error True if loaded; WP_Error on failure. |
|
67 */ |
70 */ |
68 abstract public function load(); |
71 abstract public function load(); |
69 |
72 |
70 /** |
73 /** |
71 * Saves current image to file. |
74 * Saves current image to file. |
72 * |
75 * |
73 * @since 3.5.0 |
76 * @since 3.5.0 |
74 * @access public |
|
75 * @abstract |
77 * @abstract |
76 * |
78 * |
77 * @param string $destfilename |
79 * @param string $destfilename |
78 * @param string $mime_type |
80 * @param string $mime_type |
79 * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} |
81 * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} |
86 * At minimum, either a height or width must be provided. |
88 * At minimum, either a height or width must be provided. |
87 * If one of the two is set to null, the resize will |
89 * If one of the two is set to null, the resize will |
88 * maintain aspect ratio according to the provided dimension. |
90 * maintain aspect ratio according to the provided dimension. |
89 * |
91 * |
90 * @since 3.5.0 |
92 * @since 3.5.0 |
91 * @access public |
|
92 * @abstract |
93 * @abstract |
93 * |
94 * |
94 * @param int|null $max_w Image width. |
95 * @param int|null $max_w Image width. |
95 * @param int|null $max_h Image height. |
96 * @param int|null $max_h Image height. |
96 * @param boolean $crop |
97 * @param bool $crop |
97 * @return boolean|WP_Error |
98 * @return bool|WP_Error |
98 */ |
99 */ |
99 abstract public function resize( $max_w, $max_h, $crop = false ); |
100 abstract public function resize( $max_w, $max_h, $crop = false ); |
100 |
101 |
101 /** |
102 /** |
102 * Resize multiple images from a single source. |
103 * Resize multiple images from a single source. |
103 * |
104 * |
104 * @since 3.5.0 |
105 * @since 3.5.0 |
105 * @access public |
|
106 * @abstract |
106 * @abstract |
107 * |
107 * |
108 * @param array $sizes { |
108 * @param array $sizes { |
109 * An array of image size arrays. Default sizes are 'small', 'medium', 'large'. |
109 * An array of image size arrays. Default sizes are 'small', 'medium', 'large'. |
110 * |
110 * |
120 |
120 |
121 /** |
121 /** |
122 * Crops Image. |
122 * Crops Image. |
123 * |
123 * |
124 * @since 3.5.0 |
124 * @since 3.5.0 |
125 * @access public |
|
126 * @abstract |
125 * @abstract |
127 * |
126 * |
128 * @param int $src_x The start x position to crop from. |
127 * @param int $src_x The start x position to crop from. |
129 * @param int $src_y The start y position to crop from. |
128 * @param int $src_y The start y position to crop from. |
130 * @param int $src_w The width to crop. |
129 * @param int $src_w The width to crop. |
131 * @param int $src_h The height to crop. |
130 * @param int $src_h The height to crop. |
132 * @param int $dst_w Optional. The destination width. |
131 * @param int $dst_w Optional. The destination width. |
133 * @param int $dst_h Optional. The destination height. |
132 * @param int $dst_h Optional. The destination height. |
134 * @param boolean $src_abs Optional. If the source crop points are absolute. |
133 * @param bool $src_abs Optional. If the source crop points are absolute. |
135 * @return boolean|WP_Error |
134 * @return bool|WP_Error |
136 */ |
135 */ |
137 abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ); |
136 abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ); |
138 |
137 |
139 /** |
138 /** |
140 * Rotates current image counter-clockwise by $angle. |
139 * Rotates current image counter-clockwise by $angle. |
141 * |
140 * |
142 * @since 3.5.0 |
141 * @since 3.5.0 |
143 * @access public |
|
144 * @abstract |
142 * @abstract |
145 * |
143 * |
146 * @param float $angle |
144 * @param float $angle |
147 * @return boolean|WP_Error |
145 * @return bool|WP_Error |
148 */ |
146 */ |
149 abstract public function rotate( $angle ); |
147 abstract public function rotate( $angle ); |
150 |
148 |
151 /** |
149 /** |
152 * Flips current image. |
150 * Flips current image. |
153 * |
151 * |
154 * @since 3.5.0 |
152 * @since 3.5.0 |
155 * @access public |
153 * @abstract |
156 * @abstract |
154 * |
157 * |
155 * @param bool $horz Flip along Horizontal Axis |
158 * @param boolean $horz Flip along Horizontal Axis |
156 * @param bool $vert Flip along Vertical Axis |
159 * @param boolean $vert Flip along Vertical Axis |
157 * @return bool|WP_Error |
160 * @return boolean|WP_Error |
|
161 */ |
158 */ |
162 abstract public function flip( $horz, $vert ); |
159 abstract public function flip( $horz, $vert ); |
163 |
160 |
164 /** |
161 /** |
165 * Streams current image to browser. |
162 * Streams current image to browser. |
166 * |
163 * |
167 * @since 3.5.0 |
164 * @since 3.5.0 |
168 * @access public |
165 * @abstract |
169 * @abstract |
166 * |
170 * |
167 * @param string $mime_type The mime type of the image. |
171 * @param string $mime_type |
168 * @return bool|WP_Error True on success, WP_Error object or false on failure. |
172 * @return boolean|WP_Error |
|
173 */ |
169 */ |
174 abstract public function stream( $mime_type = null ); |
170 abstract public function stream( $mime_type = null ); |
175 |
171 |
176 /** |
172 /** |
177 * Gets dimensions of image. |
173 * Gets dimensions of image. |
178 * |
174 * |
179 * @since 3.5.0 |
175 * @since 3.5.0 |
180 * @access public |
|
181 * |
176 * |
182 * @return array {'width'=>int, 'height'=>int} |
177 * @return array {'width'=>int, 'height'=>int} |
183 */ |
178 */ |
184 public function get_size() { |
179 public function get_size() { |
185 return $this->size; |
180 return $this->size; |
221 |
214 |
222 /** |
215 /** |
223 * Sets Image Compression quality on a 1-100% scale. |
216 * Sets Image Compression quality on a 1-100% scale. |
224 * |
217 * |
225 * @since 3.5.0 |
218 * @since 3.5.0 |
226 * @access public |
|
227 * |
219 * |
228 * @param int $quality Compression Quality. Range: [1,100] |
220 * @param int $quality Compression Quality. Range: [1,100] |
229 * @return boolean|WP_Error True if set successfully; WP_Error on failure. |
221 * @return true|WP_Error True if set successfully; WP_Error on failure. |
230 */ |
222 */ |
231 public function set_quality( $quality = null ) { |
223 public function set_quality( $quality = null ) { |
232 if ( null === $quality ) { |
224 if ( null === $quality ) { |
233 /** |
225 /** |
234 * Filter the default image compression quality setting. |
226 * Filters the default image compression quality setting. |
235 * |
227 * |
236 * Applies only during initial editor instantiation, or when set_quality() is run |
228 * Applies only during initial editor instantiation, or when set_quality() is run |
237 * manually without the `$quality` argument. |
229 * manually without the `$quality` argument. |
238 * |
230 * |
239 * set_quality() has priority over the filter. |
231 * set_quality() has priority over the filter. |
245 */ |
237 */ |
246 $quality = apply_filters( 'wp_editor_set_quality', $this->default_quality, $this->mime_type ); |
238 $quality = apply_filters( 'wp_editor_set_quality', $this->default_quality, $this->mime_type ); |
247 |
239 |
248 if ( 'image/jpeg' == $this->mime_type ) { |
240 if ( 'image/jpeg' == $this->mime_type ) { |
249 /** |
241 /** |
250 * Filter the JPEG compression quality for backward-compatibility. |
242 * Filters the JPEG compression quality for backward-compatibility. |
251 * |
243 * |
252 * Applies only during initial editor instantiation, or when set_quality() is run |
244 * Applies only during initial editor instantiation, or when set_quality() is run |
253 * manually without the `$quality` argument. |
245 * manually without the `$quality` argument. |
254 * |
246 * |
255 * set_quality() has priority over the filter. |
247 * set_quality() has priority over the filter. |
268 if ( $quality < 0 || $quality > 100 ) { |
260 if ( $quality < 0 || $quality > 100 ) { |
269 $quality = $this->default_quality; |
261 $quality = $this->default_quality; |
270 } |
262 } |
271 } |
263 } |
272 |
264 |
273 // Allow 0, but squash to 1 due to identical images in GD, and for backwards compatibility. |
265 // Allow 0, but squash to 1 due to identical images in GD, and for backward compatibility. |
274 if ( 0 === $quality ) { |
266 if ( 0 === $quality ) { |
275 $quality = 1; |
267 $quality = 1; |
276 } |
268 } |
277 |
269 |
278 if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) { |
270 if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) { |
290 * Will default to $this->default_mime_type if requested is not supported. |
282 * Will default to $this->default_mime_type if requested is not supported. |
291 * |
283 * |
292 * Provides corrected filename only if filename is provided. |
284 * Provides corrected filename only if filename is provided. |
293 * |
285 * |
294 * @since 3.5.0 |
286 * @since 3.5.0 |
295 * @access protected |
|
296 * |
287 * |
297 * @param string $filename |
288 * @param string $filename |
298 * @param string $mime_type |
289 * @param string $mime_type |
299 * @return array { filename|null, extension, mime-type } |
290 * @return array { filename|null, extension, mime-type } |
300 */ |
291 */ |
338 $mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type ); |
329 $mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type ); |
339 $new_ext = $this->get_extension( $mime_type ); |
330 $new_ext = $this->get_extension( $mime_type ); |
340 } |
331 } |
341 |
332 |
342 if ( $filename ) { |
333 if ( $filename ) { |
343 $ext = ''; |
334 $dir = pathinfo( $filename, PATHINFO_DIRNAME ); |
344 $info = pathinfo( $filename ); |
335 $ext = pathinfo( $filename, PATHINFO_EXTENSION ); |
345 $dir = $info['dirname']; |
|
346 |
|
347 if( isset( $info['extension'] ) ) |
|
348 $ext = $info['extension']; |
|
349 |
336 |
350 $filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}"; |
337 $filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}"; |
351 } |
338 } |
352 |
339 |
353 return array( $filename, $new_ext, $mime_type ); |
340 return array( $filename, $new_ext, $mime_type ); |
367 public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) { |
353 public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) { |
368 // $suffix will be appended to the destination filename, just before the extension |
354 // $suffix will be appended to the destination filename, just before the extension |
369 if ( ! $suffix ) |
355 if ( ! $suffix ) |
370 $suffix = $this->get_suffix(); |
356 $suffix = $this->get_suffix(); |
371 |
357 |
372 $info = pathinfo( $this->file ); |
358 $dir = pathinfo( $this->file, PATHINFO_DIRNAME ); |
373 $dir = $info['dirname']; |
359 $ext = pathinfo( $this->file, PATHINFO_EXTENSION ); |
374 $ext = $info['extension']; |
|
375 |
360 |
376 $name = wp_basename( $this->file, ".$ext" ); |
361 $name = wp_basename( $this->file, ".$ext" ); |
377 $new_ext = strtolower( $extension ? $extension : $ext ); |
362 $new_ext = strtolower( $extension ? $extension : $ext ); |
378 |
363 |
379 if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) ) |
364 if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) ) |
399 |
383 |
400 /** |
384 /** |
401 * Either calls editor's save function or handles file as a stream. |
385 * Either calls editor's save function or handles file as a stream. |
402 * |
386 * |
403 * @since 3.5.0 |
387 * @since 3.5.0 |
404 * @access protected |
|
405 * |
388 * |
406 * @param string|stream $filename |
389 * @param string|stream $filename |
407 * @param callable $function |
390 * @param callable $function |
408 * @param array $arguments |
391 * @param array $arguments |
409 * @return boolean |
392 * @return bool |
410 */ |
393 */ |
411 protected function make_image( $filename, $function, $arguments ) { |
394 protected function make_image( $filename, $function, $arguments ) { |
412 if ( $stream = wp_is_stream( $filename ) ) { |
395 if ( $stream = wp_is_stream( $filename ) ) { |
413 ob_start(); |
396 ob_start(); |
414 } else { |
397 } else { |
440 /** |
423 /** |
441 * Returns first matched mime-type from extension, |
424 * Returns first matched mime-type from extension, |
442 * as mapped from wp_get_mime_types() |
425 * as mapped from wp_get_mime_types() |
443 * |
426 * |
444 * @since 3.5.0 |
427 * @since 3.5.0 |
445 * @access protected |
428 * |
|
429 * @static |
446 * |
430 * |
447 * @param string $extension |
431 * @param string $extension |
448 * @return string|boolean |
432 * @return string|false |
449 */ |
433 */ |
450 protected static function get_mime_type( $extension = null ) { |
434 protected static function get_mime_type( $extension = null ) { |
451 if ( ! $extension ) |
435 if ( ! $extension ) |
452 return false; |
436 return false; |
453 |
437 |
454 $mime_types = wp_get_mime_types(); |
438 $mime_types = wp_get_mime_types(); |
455 $extensions = array_keys( $mime_types ); |
439 $extensions = array_keys( $mime_types ); |
456 |
440 |
457 foreach( $extensions as $_extension ) { |
441 foreach ( $extensions as $_extension ) { |
458 if ( preg_match( "/{$extension}/i", $_extension ) ) { |
442 if ( preg_match( "/{$extension}/i", $_extension ) ) { |
459 return $mime_types[$_extension]; |
443 return $mime_types[$_extension]; |
460 } |
444 } |
461 } |
445 } |
462 |
446 |
466 /** |
450 /** |
467 * Returns first matched extension from Mime-type, |
451 * Returns first matched extension from Mime-type, |
468 * as mapped from wp_get_mime_types() |
452 * as mapped from wp_get_mime_types() |
469 * |
453 * |
470 * @since 3.5.0 |
454 * @since 3.5.0 |
471 * @access protected |
455 * |
|
456 * @static |
472 * |
457 * |
473 * @param string $mime_type |
458 * @param string $mime_type |
474 * @return string|boolean |
459 * @return string|false |
475 */ |
460 */ |
476 protected static function get_extension( $mime_type = null ) { |
461 protected static function get_extension( $mime_type = null ) { |
477 $extensions = explode( '|', array_search( $mime_type, wp_get_mime_types() ) ); |
462 $extensions = explode( '|', array_search( $mime_type, wp_get_mime_types() ) ); |
478 |
463 |
479 if ( empty( $extensions[0] ) ) |
464 if ( empty( $extensions[0] ) ) |