3 * Base WordPress Image Editor
6 * @subpackage Image_Editor
10 * Base image editor class from which implementations extend
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 = 90;
22 * Each instance handles a single file.
24 public function __construct( $file ) {
29 * Checks to see if current environment supports the editor chosen.
30 * Must be overridden in a sub-class.
39 public static function test( $args = array() ) {
44 * Checks to see if editor supports the mime-type specified.
45 * Must be overridden in a sub-class.
51 * @param string $mime_type
54 public static function supports_mime_type( $mime_type ) {
59 * Loads image from $this->file into editor.
65 * @return boolean|WP_Error True if loaded; WP_Error on failure.
67 abstract public function load();
70 * Saves current image to file.
76 * @param string $destfilename
77 * @param string $mime_type
78 * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
80 abstract public function save( $destfilename = null, $mime_type = null );
83 * Resizes current image.
91 * @param boolean $crop
92 * @return boolean|WP_Error
94 abstract public function resize( $max_w, $max_h, $crop = false );
97 * Processes current image and saves to disk
98 * multiple sizes from single source.
100 * 'width' and 'height' are required.
101 * 'crop' defaults to false when not provided.
107 * @param array $sizes { {'width'=>int, 'height'=>int, ['crop'=>bool]}, ... }
110 abstract public function multi_resize( $sizes );
119 * @param string|int $src The source file or Attachment ID.
120 * @param int $src_x The start x position to crop from.
121 * @param int $src_y The start y position to crop from.
122 * @param int $src_w The width to crop.
123 * @param int $src_h The height to crop.
124 * @param int $dst_w Optional. The destination width.
125 * @param int $dst_h Optional. The destination height.
126 * @param boolean $src_abs Optional. If the source crop points are absolute.
127 * @return boolean|WP_Error
129 abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false );
132 * Rotates current image counter-clockwise by $angle.
138 * @param float $angle
139 * @return boolean|WP_Error
141 abstract public function rotate( $angle );
144 * Flips current image.
150 * @param boolean $horz Flip along Horizontal Axis
151 * @param boolean $vert Flip along Vertical Axis
152 * @return boolean|WP_Error
154 abstract public function flip( $horz, $vert );
157 * Streams current image to browser.
163 * @param string $mime_type
164 * @return boolean|WP_Error
166 abstract public function stream( $mime_type = null );
169 * Gets dimensions of image.
174 * @return array {'width'=>int, 'height'=>int}
176 public function get_size() {
181 * Sets current image size.
189 protected function update_size( $width = null, $height = null ) {
191 'width' => (int) $width,
192 'height' => (int) $height
198 * Sets Image Compression quality on a 1-100% scale.
203 * @param int $quality Compression Quality. Range: [1,100]
206 public function set_quality( $quality ) {
207 $this->quality = apply_filters( 'wp_editor_set_quality', $quality );
209 return ( (bool) $this->quality );
213 * Returns preferred mime-type and extension based on provided
214 * file's extension and mime, or current file's extension and mime.
216 * Will default to $this->default_mime_type if requested is not supported.
218 * Provides corrected filename only if filename is provided.
223 * @param string $filename
224 * @param string $mime_type
225 * @return array { filename|null, extension, mime-type }
227 protected function get_output_format( $filename = null, $mime_type = null ) {
228 $new_ext = $file_ext = null;
231 // By default, assume specified type takes priority
233 $new_ext = $this->get_extension( $mime_type );
237 $file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
238 $file_mime = $this->get_mime_type( $file_ext );
241 // If no file specified, grab editor's current extension and mime-type.
242 $file_ext = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
243 $file_mime = $this->mime_type;
246 // Check to see if specified mime-type is the same as type implied by
247 // file extension. If so, prefer extension from file.
248 if ( ! $mime_type || ( $file_mime == $mime_type ) ) {
249 $mime_type = $file_mime;
250 $new_ext = $file_ext;
253 // Double-check that the mime-type selected is supported by the editor.
254 // If not, choose a default instead.
255 if ( ! $this->supports_mime_type( $mime_type ) ) {
256 $mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type );
257 $new_ext = $this->get_extension( $mime_type );
262 $info = pathinfo( $filename );
263 $dir = $info['dirname'];
265 if( isset( $info['extension'] ) )
266 $ext = $info['extension'];
268 $filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}";
271 return array( $filename, $new_ext, $mime_type );
275 * Builds an output filename based on current file, and adding proper suffix
280 * @param string $suffix
281 * @param string $dest_path
282 * @param string $extension
283 * @return string filename
285 public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
286 // $suffix will be appended to the destination filename, just before the extension
288 $suffix = $this->get_suffix();
290 $info = pathinfo( $this->file );
291 $dir = $info['dirname'];
292 $ext = $info['extension'];
294 $name = wp_basename( $this->file, ".$ext" );
295 $new_ext = strtolower( $extension ? $extension : $ext );
297 if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) )
300 return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
304 * Builds and returns proper suffix for file based on height and width.
309 * @return string suffix
311 public function get_suffix() {
312 if ( ! $this->get_size() )
315 return "{$this->size['width']}x{$this->size['height']}";
319 * Either calls editor's save function or handles file as a stream.
324 * @param string|stream $filename
325 * @param callable $function
326 * @param array $arguments
329 protected function make_image( $filename, $function, $arguments ) {
330 if ( $stream = wp_is_stream( $filename ) ) {
333 // The directory containing the original file may no longer exist when using a replication plugin.
334 wp_mkdir_p( dirname( $filename ) );
337 $result = call_user_func_array( $function, $arguments );
339 if ( $result && $stream ) {
340 $contents = ob_get_contents();
342 $fp = fopen( $filename, 'w' );
347 fwrite( $fp, $contents );
359 * Returns first matched mime-type from extension,
360 * as mapped from wp_get_mime_types()
365 * @param string $extension
366 * @return string|boolean
368 protected static function get_mime_type( $extension = null ) {
372 $mime_types = wp_get_mime_types();
373 $extensions = array_keys( $mime_types );
375 foreach( $extensions as $_extension ) {
376 if ( preg_match( "/{$extension}/i", $_extension ) ) {
377 return $mime_types[$_extension];
385 * Returns first matched extension from Mime-type,
386 * as mapped from wp_get_mime_types()
391 * @param string $mime_type
392 * @return string|boolean
394 protected static function get_extension( $mime_type = null ) {
395 $extensions = explode( '|', array_search( $mime_type, wp_get_mime_types() ) );
397 if ( empty( $extensions[0] ) )
400 return $extensions[0];