WP_Image_Editor is an abstract class, so it can’t be called directly. It is used for implementations like WP_Image_Editor_GD and WP_Image_Editor_Imagick. It has some base functionality that can be used by those implementations.
Methods
You shouldn’t call an implementation directly. Instead, use wp_get_image_editor(), which looks at which implementation is the best.
(An ampersand (&) before a method name indicates it returns by reference.)
Note: Refer below Methods section for complete list and link to the method.
- supports_mime_type( $mime_type )
- Checks to see if editor supports the mime-type specified.
- save( $destfilename = null, $mime_type = null )
- Saves current image to file.
- resize( $max_w, $max_h, $crop = false )
- Resizes current image.
- Crop value:
1. If false (default), images will not be cropped.
2. If an array in the form of array( x_crop_position, y_crop_position ):
- x_crop_position accepts 'left' 'center', or 'right'.
- y_crop_position accepts 'top', 'center', or 'bottom'.
- Images will be cropped to the specified dimensions within the defined crop area.
3. If true, images will be cropped to the specified dimensions using center positions.
- multi_resize( $sizes );
- Processes current image and saves to disk multiple sizes from single source.
- crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false )
- Crops Image.
- rotate( $angle )
- Rotates current image counter-clockwise by $angle.
- flip( $horz, $vert )
- Flips current image on the horizontal or vertical axis.
- stream( $mime_type = null )
- Streams current image to browser.
- get_size()
- Gets dimensions of image as an array with keys ‘width’ and ‘height’.
- update_size( $width = null, $height = null )
- Sets current image size.
- set_quality( $quality )
- Sets Image Compression quality on a 1-100% scale as an integer (1-100). Default quality defined in WP_Image_Editor class is 90.
- get_output_format( $filename = null, $mime_type = null )
- Returns preferred mime-type and extension based on provided file’s extension and mime, or current file’s extension and mime. Will default to `$this->default_mime_type` if requested is not supported. Provides corrected filename only if filename is provided.
- generate_filename( $suffix = null, $dest_path = null, $extension = null )
- Builds an output filename based on current file, and adding proper suffix.
- get_suffix()
- Builds and returns proper suffix for file based on height and width.
- make_image( $filename, $function, $arguments )
- Either calls editor’s save function or handles file as a stream.
- get_mime_type( $extension = null )
- Returns first matched mime-type from extension, as mapped from wp_get_mime_types().
- get_extension( $mime_type = null )
- Returns first matched extension from Mime-type, as mapped from wp_get_mime_types().
Source
File: wp-includes/class-wp-image-editor.php
abstract class WP_Image_Editor {
protected $file = null;
protected $size = null;
protected $mime_type = null;
protected $default_mime_type = 'image/jpeg';
protected $quality = false;
protected $default_quality = 82;
public function __construct( $file ) {
$this->file = $file;
}
public static function test( $args = array() ) {
return false;
}
public static function supports_mime_type( $mime_type ) {
return false;
}
abstract public function load();
abstract public function save( $destfilename = null, $mime_type = null );
abstract public function resize( $max_w, $max_h, $crop = false );
abstract public function multi_resize( $sizes );
abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false );
abstract public function rotate( $angle );
abstract public function flip( $horz, $vert );
abstract public function stream( $mime_type = null );
public function get_size() {
return $this->size;
}
protected function update_size( $width = null, $height = null ) {
$this->size = array(
'width' => (int) $width,
'height' => (int) $height,
);
return true;
}
public function get_quality() {
if ( ! $this->quality ) {
$this->set_quality();
}
return $this->quality;
}
public function set_quality( $quality = null ) {
if ( null === $quality ) {
$quality = apply_filters( 'wp_editor_set_quality', $this->default_quality, $this->mime_type );
if ( 'image/jpeg' === $this->mime_type ) {
$quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' );
}
if ( $quality < 0 || $quality > 100 ) {
$quality = $this->default_quality;
}
}
if ( 0 === $quality ) {
$quality = 1;
}
if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) {
$this->quality = $quality;
return true;
} else {
return new WP_Error( 'invalid_image_quality', __( 'Attempted to set image quality outside of the range [1,100].' ) );
}
}
protected function get_output_format( $filename = null, $mime_type = null ) {
$new_ext = null;
if ( $mime_type ) {
$new_ext = $this->get_extension( $mime_type );
}
if ( $filename ) {
$file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
$file_mime = $this->get_mime_type( $file_ext );
} else {
$file_ext = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
$file_mime = $this->mime_type;
}
if ( ! $mime_type || ( $file_mime == $mime_type ) ) {
$mime_type = $file_mime;
$new_ext = $file_ext;
}
if ( ! $this->supports_mime_type( $mime_type ) ) {
$mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type );
$new_ext = $this->get_extension( $mime_type );
}
if ( $filename ) {
$dir = pathinfo( $filename, PATHINFO_DIRNAME );
$ext = pathinfo( $filename, PATHINFO_EXTENSION );
$filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}";
}
return array( $filename, $new_ext, $mime_type );
}
public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
if ( ! $suffix ) {
$suffix = $this->get_suffix();
}
$dir = pathinfo( $this->file, PATHINFO_DIRNAME );
$ext = pathinfo( $this->file, PATHINFO_EXTENSION );
$name = wp_basename( $this->file, ".$ext" );
$new_ext = strtolower( $extension ? $extension : $ext );
if ( ! is_null( $dest_path ) ) {
$_dest_path = realpath( $dest_path );
if ( $_dest_path ) {
$dir = $_dest_path;
}
}
return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
}
public function get_suffix() {
if ( ! $this->get_size() ) {
return false;
}
return "{$this->size['width']}x{$this->size['height']}";
}
public function maybe_exif_rotate() {
$orientation = null;
if ( is_callable( 'exif_read_data' ) && 'image/jpeg' === $this->mime_type ) {
$exif_data = @exif_read_data( $this->file );
if ( ! empty( $exif_data['Orientation'] ) ) {
$orientation = (int) $exif_data['Orientation'];
}
}
$orientation = apply_filters( 'wp_image_maybe_exif_rotate', $orientation, $this->file );
if ( ! $orientation || 1 === $orientation ) {
return false;
}
switch ( $orientation ) {
case 2:
$result = $this->flip( true, false );
break;
case 3:
$result = $this->flip( true, true );
break;
case 4:
$result = $this->flip( false, true );
break;
case 5:
$result = $this->rotate( 90 );
if ( ! is_wp_error( $result ) ) {
$result = $this->flip( false, true );
}
break;
case 6:
$result = $this->rotate( 270 );
break;
case 7:
$result = $this->rotate( 90 );
if ( ! is_wp_error( $result ) ) {
$result = $this->flip( true, false );
}
break;
case 8:
$result = $this->rotate( 90 );
break;
}
return $result;
}
protected function make_image( $filename, $function, $arguments ) {
$stream = wp_is_stream( $filename );
if ( $stream ) {
ob_start();
} else {
wp_mkdir_p( dirname( $filename ) );
}
$result = call_user_func_array( $function, $arguments );
if ( $result && $stream ) {
$contents = ob_get_contents();
$fp = fopen( $filename, 'w' );
if ( ! $fp ) {
ob_end_clean();
return false;
}
fwrite( $fp, $contents );
fclose( $fp );
}
if ( $stream ) {
ob_end_clean();
}
return $result;
}
protected static function get_mime_type( $extension = null ) {
if ( ! $extension ) {
return false;
}
$mime_types = wp_get_mime_types();
$extensions = array_keys( $mime_types );
foreach ( $extensions as $_extension ) {
if ( preg_match( "/{$extension}/i", $_extension ) ) {
return $mime_types[ $_extension ];
}
}
return false;
}
protected static function get_extension( $mime_type = null ) {
$extensions = explode( '|', array_search( $mime_type, wp_get_mime_types(), true ) );
if ( empty( $extensions[0] ) ) {
return false;
}
return $extensions[0];
}
}