WP_Image_Editor improvements.
* Make test() and supports_mime_type() static. * Add required_methods argument to get_instance(). Allows requesting an implementation that has certain methods/capabilities. * Whitespace cleanup Props markoheijnen see #6821 git-svn-id: https://develop.svn.wordpress.org/trunk@22510 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
80d6ef3b28
commit
e678b95a5a
|
@ -237,7 +237,6 @@ function wp_stream_image( $image, $mime_type, $post_id ) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves Image to File
|
* Saves Image to File
|
||||||
* @TODO: Add mime_type support to WP_Image_Editor
|
|
||||||
*
|
*
|
||||||
* @param string $filename
|
* @param string $filename
|
||||||
* @param WP_Image_Editor $image
|
* @param WP_Image_Editor $image
|
||||||
|
|
|
@ -28,11 +28,11 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
|
||||||
* Checks to see if current environment supports GD
|
* Checks to see if current environment supports GD
|
||||||
*
|
*
|
||||||
* @since 3.5.0
|
* @since 3.5.0
|
||||||
* @access protected
|
* @access public
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function test() {
|
public static function test( $args = null ) {
|
||||||
if ( ! extension_loaded('gd') || ! function_exists('gd_info') )
|
if ( ! extension_loaded('gd') || ! function_exists('gd_info') )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
|
||||||
* @param string $mime_type
|
* @param string $mime_type
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function supports_mime_type( $mime_type ) {
|
public static function supports_mime_type( $mime_type ) {
|
||||||
$allowed_mime_types = array( 'image/gif', 'image/png', 'image/jpeg' );
|
$allowed_mime_types = array( 'image/gif', 'image/png', 'image/jpeg' );
|
||||||
|
|
||||||
return in_array( $mime_type, $allowed_mime_types );
|
return in_array( $mime_type, $allowed_mime_types );
|
||||||
|
|
|
@ -29,11 +29,11 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
|
||||||
* Checks to see if current environment supports Imagick
|
* Checks to see if current environment supports Imagick
|
||||||
*
|
*
|
||||||
* @since 3.5.0
|
* @since 3.5.0
|
||||||
* @access protected
|
* @access public
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function test() {
|
public static function test( $args = null ) {
|
||||||
if ( ! extension_loaded( 'imagick' ) )
|
if ( ! extension_loaded( 'imagick' ) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -143,7 +143,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
|
||||||
* @param string $mime_type
|
* @param string $mime_type
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function supports_mime_type( $mime_type ) {
|
public static function supports_mime_type( $mime_type ) {
|
||||||
if ( ! $mime_type )
|
if ( ! $mime_type )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,6 @@ abstract class WP_Image_Editor {
|
||||||
protected $mime_type = null;
|
protected $mime_type = null;
|
||||||
protected $default_mime_type = 'image/jpeg';
|
protected $default_mime_type = 'image/jpeg';
|
||||||
protected $quality = 90;
|
protected $quality = 90;
|
||||||
private static $implementation;
|
|
||||||
|
|
||||||
protected function __construct( $filename ) {
|
protected function __construct( $filename ) {
|
||||||
$this->file = $filename;
|
$this->file = $filename;
|
||||||
|
@ -30,16 +29,17 @@ abstract class WP_Image_Editor {
|
||||||
* @access public
|
* @access public
|
||||||
*
|
*
|
||||||
* @param string $path Path to File to Load
|
* @param string $path Path to File to Load
|
||||||
|
* @param array $required_methods Methods to require in implementation
|
||||||
* @return WP_Image_Editor|WP_Error|boolean
|
* @return WP_Image_Editor|WP_Error|boolean
|
||||||
*/
|
*/
|
||||||
public final static function get_instance( $path = null ) {
|
public final static function get_instance( $path = null, $required_methods = null ) {
|
||||||
$implementation = apply_filters( 'image_editor_class', self::choose_implementation(), $path );
|
$implementation = apply_filters( 'wp_image_editor_class', self::choose_implementation( $required_methods ), $path );
|
||||||
|
|
||||||
if ( $implementation ) {
|
if ( $implementation ) {
|
||||||
$editor = new $implementation( $path );
|
$editor = new $implementation( $path );
|
||||||
$loaded = $editor->load();
|
$loaded = $editor->load();
|
||||||
|
|
||||||
if ( is_wp_error ( $loaded ) )
|
if ( is_wp_error( $loaded ) )
|
||||||
return $loaded;
|
return $loaded;
|
||||||
|
|
||||||
return $editor;
|
return $editor;
|
||||||
|
@ -54,38 +54,76 @@ abstract class WP_Image_Editor {
|
||||||
* @since 3.5.0
|
* @since 3.5.0
|
||||||
* @access private
|
* @access private
|
||||||
*
|
*
|
||||||
|
* @param $required_methods Array String array of all methods required for implementation returned.
|
||||||
|
*
|
||||||
* @return string|bool Class name for the first editor that claims to support the request. False if no editor claims to support the request.
|
* @return string|bool Class name for the first editor that claims to support the request. False if no editor claims to support the request.
|
||||||
*/
|
*/
|
||||||
private final static function choose_implementation() {
|
private final static function choose_implementation( $required_methods = null ) {
|
||||||
|
$request_order = apply_filters( 'wp_image_editors',
|
||||||
|
array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
|
||||||
|
|
||||||
if ( null === self::$implementation ) {
|
if ( ! $required_methods )
|
||||||
$request_order = apply_filters( 'wp_editors', array( 'imagick', 'gd' ) );
|
$required_methods = apply_filters( 'wp_image_editor_default_methods',
|
||||||
|
array( 'resize', 'multi_resize', 'crop', 'rotate', 'flip', 'stream' ) );
|
||||||
|
|
||||||
// Loop over each editor on each request looking for one which will serve this request's needs
|
// Loop over each editor on each request looking for one which will serve this request's needs
|
||||||
foreach ( $request_order as $editor ) {
|
foreach ( $request_order as $editor ) {
|
||||||
$class = 'WP_Image_Editor_' . $editor;
|
|
||||||
|
|
||||||
// Check to see if this editor is a possibility, calls the editor statically
|
// Check to see if this editor is a possibility, calls the editor statically
|
||||||
if ( ! call_user_func( array( $class, 'test' ) ) )
|
if ( ! call_user_func( array( $editor, 'test' ) ) )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
self::$implementation = $class;
|
// Make sure that all methods are supported by editor.
|
||||||
break;
|
if ( array_diff( $required_methods, get_class_methods( $editor ) ) )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
return $editor;
|
||||||
}
|
}
|
||||||
}
|
return false;
|
||||||
return self::$implementation;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public function test(); // returns bool
|
|
||||||
abstract protected function load(); // returns bool|WP_Error
|
abstract protected function load(); // returns bool|WP_Error
|
||||||
abstract public function supports_mime_type( $mime_type ); // returns bool
|
|
||||||
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 save( $destfilename = null, $mime_type = null );
|
abstract public function save( $destfilename = null, $mime_type = null );
|
||||||
abstract public function stream( $mime_type = null );
|
|
||||||
|
/**
|
||||||
|
* Implement all of the below to support natively used functions:
|
||||||
|
*
|
||||||
|
* public function resize( $max_w, $max_h, $crop = false )
|
||||||
|
* public function multi_resize( $sizes )
|
||||||
|
* public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false )
|
||||||
|
* public function rotate( $angle )
|
||||||
|
* public function flip( $horz, $vert )
|
||||||
|
* public function stream( $mime_type = null )
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks to see if current environment supports the editor chosen.
|
||||||
|
* Must be overridden in a sub-class.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
* @access public
|
||||||
|
* @abstract
|
||||||
|
*
|
||||||
|
* @param $args array
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public static function test( $args = null ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks to see if editor supports mime-type specified
|
||||||
|
* Must be overridden in a sub-class.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
* @access public
|
||||||
|
* @abstract
|
||||||
|
*
|
||||||
|
* @param string $mime_type
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public static function supports_mime_type( $mime_type ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets dimensions of image
|
* Gets dimensions of image
|
||||||
|
@ -258,19 +296,19 @@ abstract class WP_Image_Editor {
|
||||||
|
|
||||||
$result = call_user_func_array( $function, $arguments );
|
$result = call_user_func_array( $function, $arguments );
|
||||||
|
|
||||||
if( $result && $stream ) {
|
if ( $result && $stream ) {
|
||||||
$contents = ob_get_contents();
|
$contents = ob_get_contents();
|
||||||
|
|
||||||
$fp = fopen( $dst_file, 'w' );
|
$fp = fopen( $dst_file, 'w' );
|
||||||
|
|
||||||
if( ! $fp )
|
if ( ! $fp )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
fwrite( $fp, $contents );
|
fwrite( $fp, $contents );
|
||||||
fclose( $fp );
|
fclose( $fp );
|
||||||
}
|
}
|
||||||
|
|
||||||
if( $stream ) {
|
if ( $stream ) {
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,8 +333,8 @@ abstract class WP_Image_Editor {
|
||||||
$extensions = array_keys( $mime_types );
|
$extensions = array_keys( $mime_types );
|
||||||
|
|
||||||
foreach( $extensions as $_extension ) {
|
foreach( $extensions as $_extension ) {
|
||||||
if( preg_match("/{$extension}/i", $_extension ) ) {
|
if ( preg_match( "/{$extension}/i", $_extension ) ) {
|
||||||
return $mime_types[ $_extension ];
|
return $mime_types[$_extension];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue