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