]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-image-editor.php
WordPress 4.0.1
[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          * @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          *
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          *
90          * @since 3.5.0
91          * @access public
92          * @abstract
93          *
94          * @param  int|null $max_w Image width.
95          * @param  int|null $max_h Image height.
96          * @param  boolean  $crop
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 string|int $src The source file or Attachment ID.
129          * @param int $src_x The start x position to crop from.
130          * @param int $src_y The start y position to crop from.
131          * @param int $src_w The width to crop.
132          * @param int $src_h The height to crop.
133          * @param int $dst_w Optional. The destination width.
134          * @param int $dst_h Optional. The destination height.
135          * @param boolean $src_abs Optional. If the source crop points are absolute.
136          * @return boolean|WP_Error
137          */
138         abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false );
139
140         /**
141          * Rotates current image counter-clockwise by $angle.
142          *
143          * @since 3.5.0
144          * @access public
145          * @abstract
146          *
147          * @param float $angle
148          * @return boolean|WP_Error
149          */
150         abstract public function rotate( $angle );
151
152         /**
153          * Flips current image.
154          *
155          * @since 3.5.0
156          * @access public
157          * @abstract
158          *
159          * @param boolean $horz Flip along Horizontal Axis
160          * @param boolean $vert Flip along Vertical Axis
161          * @return boolean|WP_Error
162          */
163         abstract public function flip( $horz, $vert );
164
165         /**
166          * Streams current image to browser.
167          *
168          * @since 3.5.0
169          * @access public
170          * @abstract
171          *
172          * @param string $mime_type
173          * @return boolean|WP_Error
174          */
175         abstract public function stream( $mime_type = null );
176
177         /**
178          * Gets dimensions of image.
179          *
180          * @since 3.5.0
181          * @access public
182          *
183          * @return array {'width'=>int, 'height'=>int}
184          */
185         public function get_size() {
186                 return $this->size;
187         }
188
189         /**
190          * Sets current image size.
191          *
192          * @since 3.5.0
193          * @access protected
194          *
195          * @param int $width
196          * @param int $height
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         /**
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                         /**
217                          * Filter the default image compression quality setting.
218                          *
219                          * @since 3.5.0
220                          *
221                          * @param int    $quality   Quality level between 1 (low) and 100 (high).
222                          * @param string $mime_type Image mime type.
223                          */
224                         $quality = apply_filters( 'wp_editor_set_quality', $this->default_quality, $this->mime_type );
225
226                         if ( 'image/jpeg' == $this->mime_type ) {
227                                 /**
228                                  * Filter the JPEG compression quality for backward-compatibility.
229                                  *
230                                  * The filter is evaluated under two contexts: 'image_resize', and 'edit_image',
231                                  * (when a JPEG image is saved to file).
232                                  *
233                                  * @since 2.5.0
234                                  *
235                                  * @param int    $quality Quality level between 0 (low) and 100 (high) of the JPEG.
236                                  * @param string $context Context of the filter.
237                                  */
238                                 $quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' );
239
240                                 if ( ! $this->set_quality( $quality ) ) {
241                                         $this->quality = $this->default_quality;
242                                 }
243                         }
244                 }
245
246                 return $this->quality;
247         }
248
249         /**
250          * Sets Image Compression quality on a 1-100% scale.
251          *
252          * @since 3.5.0
253          * @access public
254          *
255          * @param int $quality Compression Quality. Range: [1,100]
256          * @return boolean|WP_Error True if set successfully; WP_Error on failure.
257          */
258         public function set_quality( $quality = null ) {
259                 // Allow 0, but squash to 1 due to identical images in GD, and for backwards compatibility.
260                 if ( $quality == 0 ) {
261                         $quality = 1;
262                 }
263
264                 if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) {
265                         $this->quality = $quality;
266                         return true;
267                 } else {
268                         return new WP_Error( 'invalid_image_quality', __('Attempted to set image quality outside of the range [1,100].') );
269                 }
270         }
271
272         /**
273          * Returns preferred mime-type and extension based on provided
274          * file's extension and mime, or current file's extension and mime.
275          *
276          * Will default to $this->default_mime_type if requested is not supported.
277          *
278          * Provides corrected filename only if filename is provided.
279          *
280          * @since 3.5.0
281          * @access protected
282          *
283          * @param string $filename
284          * @param string $mime_type
285          * @return array { filename|null, extension, mime-type }
286          */
287         protected function get_output_format( $filename = null, $mime_type = null ) {
288                 $new_ext = null;
289
290                 // By default, assume specified type takes priority
291                 if ( $mime_type ) {
292                         $new_ext = $this->get_extension( $mime_type );
293                 }
294
295                 if ( $filename ) {
296                         $file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
297                         $file_mime = $this->get_mime_type( $file_ext );
298                 }
299                 else {
300                         // If no file specified, grab editor's current extension and mime-type.
301                         $file_ext = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
302                         $file_mime = $this->mime_type;
303                 }
304
305                 // Check to see if specified mime-type is the same as type implied by
306                 // file extension.  If so, prefer extension from file.
307                 if ( ! $mime_type || ( $file_mime == $mime_type ) ) {
308                         $mime_type = $file_mime;
309                         $new_ext = $file_ext;
310                 }
311
312                 // Double-check that the mime-type selected is supported by the editor.
313                 // If not, choose a default instead.
314                 if ( ! $this->supports_mime_type( $mime_type ) ) {
315                         /**
316                          * Filter default mime type prior to getting the file extension.
317                          *
318                          * @see wp_get_mime_types()
319                          *
320                          * @since 3.5.0
321                          *
322                          * @param string $mime_type Mime type string.
323                          */
324                         $mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type );
325                         $new_ext = $this->get_extension( $mime_type );
326                 }
327
328                 if ( $filename ) {
329                         $ext = '';
330                         $info = pathinfo( $filename );
331                         $dir  = $info['dirname'];
332
333                         if( isset( $info['extension'] ) )
334                                 $ext = $info['extension'];
335
336                         $filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}";
337                 }
338
339                 return array( $filename, $new_ext, $mime_type );
340         }
341
342         /**
343          * Builds an output filename based on current file, and adding proper suffix
344          *
345          * @since 3.5.0
346          * @access public
347          *
348          * @param string $suffix
349          * @param string $dest_path
350          * @param string $extension
351          * @return string filename
352          */
353         public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
354                 // $suffix will be appended to the destination filename, just before the extension
355                 if ( ! $suffix )
356                         $suffix = $this->get_suffix();
357
358                 $info = pathinfo( $this->file );
359                 $dir  = $info['dirname'];
360                 $ext  = $info['extension'];
361
362                 $name = wp_basename( $this->file, ".$ext" );
363                 $new_ext = strtolower( $extension ? $extension : $ext );
364
365                 if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) )
366                         $dir = $_dest_path;
367
368                 return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
369         }
370
371         /**
372          * Builds and returns proper suffix for file based on height and width.
373          *
374          * @since 3.5.0
375          * @access public
376          *
377          * @return string suffix
378          */
379         public function get_suffix() {
380                 if ( ! $this->get_size() )
381                         return false;
382
383                 return "{$this->size['width']}x{$this->size['height']}";
384         }
385
386         /**
387          * Either calls editor's save function or handles file as a stream.
388          *
389          * @since 3.5.0
390          * @access protected
391          *
392          * @param string|stream $filename
393          * @param callable $function
394          * @param array $arguments
395          * @return boolean
396          */
397         protected function make_image( $filename, $function, $arguments ) {
398                 if ( $stream = wp_is_stream( $filename ) ) {
399                         ob_start();
400                 } else {
401                         // The directory containing the original file may no longer exist when using a replication plugin.
402                         wp_mkdir_p( dirname( $filename ) );
403                 }
404
405                 $result = call_user_func_array( $function, $arguments );
406
407                 if ( $result && $stream ) {
408                         $contents = ob_get_contents();
409
410                         $fp = fopen( $filename, 'w' );
411
412                         if ( ! $fp )
413                                 return false;
414
415                         fwrite( $fp, $contents );
416                         fclose( $fp );
417                 }
418
419                 if ( $stream ) {
420                         ob_end_clean();
421                 }
422
423                 return $result;
424         }
425
426         /**
427          * Returns first matched mime-type from extension,
428          * as mapped from wp_get_mime_types()
429          *
430          * @since 3.5.0
431          * @access protected
432          *
433          * @param string $extension
434          * @return string|boolean
435          */
436         protected static function get_mime_type( $extension = null ) {
437                 if ( ! $extension )
438                         return false;
439
440                 $mime_types = wp_get_mime_types();
441                 $extensions = array_keys( $mime_types );
442
443                 foreach( $extensions as $_extension ) {
444                         if ( preg_match( "/{$extension}/i", $_extension ) ) {
445                                 return $mime_types[$_extension];
446                         }
447                 }
448
449                 return false;
450         }
451
452         /**
453          * Returns first matched extension from Mime-type,
454          * as mapped from wp_get_mime_types()
455          *
456          * @since 3.5.0
457          * @access protected
458          *
459          * @param string $mime_type
460          * @return string|boolean
461          */
462         protected static function get_extension( $mime_type = null ) {
463                 $extensions = explode( '|', array_search( $mime_type, wp_get_mime_types() ) );
464
465                 if ( empty( $extensions[0] ) )
466                         return false;
467
468                 return $extensions[0];
469         }
470 }
471