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 * Resize multiple images from a single source.
103 * @param array $sizes {
104 * An array of image size arrays. Default sizes are 'small', 'medium', 'large'.
106 * @type array $size {
107 * @type int $width Image width.
108 * @type int $height Image height.
109 * @type bool $crop Optional. Whether to crop the image. Default false.
112 * @return array An array of resized images metadata by size.
114 abstract public function multi_resize( $sizes );
123 * @param string|int $src The source file or Attachment ID.
124 * @param int $src_x The start x position to crop from.
125 * @param int $src_y The start y position to crop from.
126 * @param int $src_w The width to crop.
127 * @param int $src_h The height to crop.
128 * @param int $dst_w Optional. The destination width.
129 * @param int $dst_h Optional. The destination height.
130 * @param boolean $src_abs Optional. If the source crop points are absolute.
131 * @return boolean|WP_Error
133 abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false );
136 * Rotates current image counter-clockwise by $angle.
142 * @param float $angle
143 * @return boolean|WP_Error
145 abstract public function rotate( $angle );
148 * Flips current image.
154 * @param boolean $horz Flip along Horizontal Axis
155 * @param boolean $vert Flip along Vertical Axis
156 * @return boolean|WP_Error
158 abstract public function flip( $horz, $vert );
161 * Streams current image to browser.
167 * @param string $mime_type
168 * @return boolean|WP_Error
170 abstract public function stream( $mime_type = null );
173 * Gets dimensions of image.
178 * @return array {'width'=>int, 'height'=>int}
180 public function get_size() {
185 * Sets current image size.
193 protected function update_size( $width = null, $height = null ) {
195 'width' => (int) $width,
196 'height' => (int) $height
202 * Sets Image Compression quality on a 1-100% scale.
207 * @param int $quality Compression Quality. Range: [1,100]
210 public function set_quality( $quality ) {
212 * Filter the default quality setting.
216 * @param int $quality Quality level between 0 (low) and 100 (high).
218 $this->quality = apply_filters( 'wp_editor_set_quality', $quality );
220 return ( (bool) $this->quality );
224 * Returns preferred mime-type and extension based on provided
225 * file's extension and mime, or current file's extension and mime.
227 * Will default to $this->default_mime_type if requested is not supported.
229 * Provides corrected filename only if filename is provided.
234 * @param string $filename
235 * @param string $mime_type
236 * @return array { filename|null, extension, mime-type }
238 protected function get_output_format( $filename = null, $mime_type = null ) {
239 $new_ext = $file_ext = null;
242 // By default, assume specified type takes priority
244 $new_ext = $this->get_extension( $mime_type );
248 $file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
249 $file_mime = $this->get_mime_type( $file_ext );
252 // If no file specified, grab editor's current extension and mime-type.
253 $file_ext = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
254 $file_mime = $this->mime_type;
257 // Check to see if specified mime-type is the same as type implied by
258 // file extension. If so, prefer extension from file.
259 if ( ! $mime_type || ( $file_mime == $mime_type ) ) {
260 $mime_type = $file_mime;
261 $new_ext = $file_ext;
264 // Double-check that the mime-type selected is supported by the editor.
265 // If not, choose a default instead.
266 if ( ! $this->supports_mime_type( $mime_type ) ) {
268 * Filter default mime type prior to getting the file extension.
270 * @see wp_get_mime_types()
274 * @param string $mime_type Mime type string.
276 $mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type );
277 $new_ext = $this->get_extension( $mime_type );
282 $info = pathinfo( $filename );
283 $dir = $info['dirname'];
285 if( isset( $info['extension'] ) )
286 $ext = $info['extension'];
288 $filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}";
291 return array( $filename, $new_ext, $mime_type );
295 * Builds an output filename based on current file, and adding proper suffix
300 * @param string $suffix
301 * @param string $dest_path
302 * @param string $extension
303 * @return string filename
305 public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
306 // $suffix will be appended to the destination filename, just before the extension
308 $suffix = $this->get_suffix();
310 $info = pathinfo( $this->file );
311 $dir = $info['dirname'];
312 $ext = $info['extension'];
314 $name = wp_basename( $this->file, ".$ext" );
315 $new_ext = strtolower( $extension ? $extension : $ext );
317 if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) )
320 return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
324 * Builds and returns proper suffix for file based on height and width.
329 * @return string suffix
331 public function get_suffix() {
332 if ( ! $this->get_size() )
335 return "{$this->size['width']}x{$this->size['height']}";
339 * Either calls editor's save function or handles file as a stream.
344 * @param string|stream $filename
345 * @param callable $function
346 * @param array $arguments
349 protected function make_image( $filename, $function, $arguments ) {
350 if ( $stream = wp_is_stream( $filename ) ) {
353 // The directory containing the original file may no longer exist when using a replication plugin.
354 wp_mkdir_p( dirname( $filename ) );
357 $result = call_user_func_array( $function, $arguments );
359 if ( $result && $stream ) {
360 $contents = ob_get_contents();
362 $fp = fopen( $filename, 'w' );
367 fwrite( $fp, $contents );
379 * Returns first matched mime-type from extension,
380 * as mapped from wp_get_mime_types()
385 * @param string $extension
386 * @return string|boolean
388 protected static function get_mime_type( $extension = null ) {
392 $mime_types = wp_get_mime_types();
393 $extensions = array_keys( $mime_types );
395 foreach( $extensions as $_extension ) {
396 if ( preg_match( "/{$extension}/i", $_extension ) ) {
397 return $mime_types[$_extension];
405 * Returns first matched extension from Mime-type,
406 * as mapped from wp_get_mime_types()
411 * @param string $mime_type
412 * @return string|boolean
414 protected static function get_extension( $mime_type = null ) {
415 $extensions = explode( '|', array_search( $mime_type, wp_get_mime_types() ) );
417 if ( empty( $extensions[0] ) )
420 return $extensions[0];