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.
104 * @param array $sizes { {'width'=>int, 'height'=>int, 'crop'=>bool}, ... }
107 abstract public function multi_resize( $sizes );
116 * @param string|int $src The source file or Attachment ID.
117 * @param int $src_x The start x position to crop from.
118 * @param int $src_y The start y position to crop from.
119 * @param int $src_w The width to crop.
120 * @param int $src_h The height to crop.
121 * @param int $dst_w Optional. The destination width.
122 * @param int $dst_h Optional. The destination height.
123 * @param boolean $src_abs Optional. If the source crop points are absolute.
124 * @return boolean|WP_Error
126 abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false );
129 * Rotates current image counter-clockwise by $angle.
135 * @param float $angle
136 * @return boolean|WP_Error
138 abstract public function rotate( $angle );
141 * Flips current image.
147 * @param boolean $horz Horizontal Flip
148 * @param boolean $vert Vertical Flip
149 * @return boolean|WP_Error
151 abstract public function flip( $horz, $vert );
154 * Streams current image to browser.
160 * @param string $mime_type
161 * @return boolean|WP_Error
163 abstract public function stream( $mime_type = null );
166 * Gets dimensions of image.
171 * @return array {'width'=>int, 'height'=>int}
173 public function get_size() {
178 * Sets current image size.
186 protected function update_size( $width = null, $height = null ) {
188 'width' => (int) $width,
189 'height' => (int) $height
195 * Sets Image Compression quality on a 1-100% scale.
200 * @param int $quality Compression Quality. Range: [1,100]
203 public function set_quality( $quality ) {
204 $this->quality = apply_filters( 'wp_editor_set_quality', $quality );
206 return ( (bool) $this->quality );
210 * Returns preferred mime-type and extension based on provided
211 * file's extension and mime, or current file's extension and mime.
213 * Will default to $this->default_mime_type if requested is not supported.
215 * Provides corrected filename only if filename is provided.
220 * @param string $filename
221 * @param string $mime_type
222 * @return array { filename|null, extension, mime-type }
224 protected function get_output_format( $filename = null, $mime_type = null ) {
225 $new_ext = $file_ext = null;
228 // By default, assume specified type takes priority
230 $new_ext = $this->get_extension( $mime_type );
234 $file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
235 $file_mime = $this->get_mime_type( $file_ext );
238 // If no file specified, grab editor's current extension and mime-type.
239 $file_ext = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
240 $file_mime = $this->mime_type;
243 // Check to see if specified mime-type is the same as type implied by
244 // file extension. If so, prefer extension from file.
245 if ( ! $mime_type || ( $file_mime == $mime_type ) ) {
246 $mime_type = $file_mime;
247 $new_ext = $file_ext;
250 // Double-check that the mime-type selected is supported by the editor.
251 // If not, choose a default instead.
252 if ( ! $this->supports_mime_type( $mime_type ) ) {
253 $mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type );
254 $new_ext = $this->get_extension( $mime_type );
259 $info = pathinfo( $filename );
260 $dir = $info['dirname'];
262 if( isset( $info['extension'] ) )
263 $ext = $info['extension'];
265 $filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}";
268 return array( $filename, $new_ext, $mime_type );
272 * Builds an output filename based on current file, and adding proper suffix
277 * @param string $suffix
278 * @param string $dest_path
279 * @param string $extension
280 * @return string filename
282 public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
283 // $suffix will be appended to the destination filename, just before the extension
285 $suffix = $this->get_suffix();
287 $info = pathinfo( $this->file );
288 $dir = $info['dirname'];
289 $ext = $info['extension'];
291 $name = wp_basename( $this->file, ".$ext" );
292 $new_ext = strtolower( $extension ? $extension : $ext );
294 if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) )
297 return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
301 * Builds and returns proper suffix for file based on height and width.
306 * @return string suffix
308 public function get_suffix() {
309 if ( ! $this->get_size() )
312 return "{$this->size['width']}x{$this->size['height']}";
316 * Either calls editor's save function or handles file as a stream.
321 * @param string|stream $filename
322 * @param callable $function
323 * @param array $arguments
326 protected function make_image( $filename, $function, $arguments ) {
327 $dst_file = $filename;
329 if ( $stream = wp_is_stream( $filename ) ) {
334 $result = call_user_func_array( $function, $arguments );
336 if ( $result && $stream ) {
337 $contents = ob_get_contents();
339 $fp = fopen( $dst_file, 'w' );
344 fwrite( $fp, $contents );
356 * Returns first matched mime-type from extension,
357 * as mapped from wp_get_mime_types()
362 * @param string $extension
363 * @return string|boolean
365 protected static function get_mime_type( $extension = null ) {
369 $mime_types = wp_get_mime_types();
370 $extensions = array_keys( $mime_types );
372 foreach( $extensions as $_extension ) {
373 if ( preg_match( "/{$extension}/i", $_extension ) ) {
374 return $mime_types[$_extension];
382 * Returns first matched extension from Mime-type,
383 * as mapped from wp_get_mime_types()
388 * @param string $mime_type
389 * @return string|boolean
391 protected static function get_extension( $mime_type = null ) {
392 $extensions = explode( '|', array_search( $mime_type, wp_get_mime_types() ) );
394 if ( empty( $extensions[0] ) )
397 return $extensions[0];