Make WordPress Core

Changeset 22510


Ignore:
Timestamp:
11/09/2012 07:38:54 PM (12 years ago)
Author:
ryan
Message:

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

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/image-edit.php

    r22192 r22510  
    238238/**
    239239 * Saves Image to File
    240  * @TODO: Add mime_type support to WP_Image_Editor
    241240 *
    242241 * @param string $filename
  • trunk/wp-includes/class-wp-image-editor-gd.php

    r22463 r22510  
    2929     *
    3030     * @since 3.5.0
    31      * @access protected
     31     * @access public
    3232     *
    3333     * @return boolean
    3434     */
    35     public function test() {
     35    public static function test( $args = null ) {
    3636        if ( ! extension_loaded('gd') || ! function_exists('gd_info') )
    3737            return false;
     
    100100     * @return boolean
    101101     */
    102     public function supports_mime_type( $mime_type ) {
     102    public static function supports_mime_type( $mime_type ) {
    103103        $allowed_mime_types = array( 'image/gif', 'image/png', 'image/jpeg' );
    104104
  • trunk/wp-includes/class-wp-image-editor-imagick.php

    r22463 r22510  
    3030     *
    3131     * @since 3.5.0
    32      * @access protected
     32     * @access public
    3333     *
    3434     * @return boolean
    3535     */
    36     public function test() {
     36    public static function test( $args = null ) {
    3737        if ( ! extension_loaded( 'imagick' ) )
    3838            return false;
     
    144144     * @return boolean
    145145     */
    146     public function supports_mime_type( $mime_type ) {
     146    public static function supports_mime_type( $mime_type ) {
    147147        if ( ! $mime_type )
    148148            return false;
  • trunk/wp-includes/class-wp-image-editor.php

    r22243 r22510  
    1818    protected $default_mime_type = 'image/jpeg';
    1919    protected $quality = 90;
    20     private static $implementation;
    2120
    2221    protected function __construct( $filename ) {
     
    3130     *
    3231     * @param string $path Path to File to Load
     32     * @param array $required_methods Methods to require in implementation
    3333     * @return WP_Image_Editor|WP_Error|boolean
    3434     */
    35     public final static function get_instance( $path = null ) {
    36         $implementation = apply_filters( 'image_editor_class', self::choose_implementation(), $path );
     35    public final static function get_instance( $path = null, $required_methods = null ) {
     36        $implementation = apply_filters( 'wp_image_editor_class', self::choose_implementation( $required_methods ), $path );
    3737
    3838        if ( $implementation ) {
     
    4040            $loaded = $editor->load();
    4141
    42             if ( is_wp_error ( $loaded ) )
     42            if ( is_wp_error( $loaded ) )
    4343                return $loaded;
    4444
     
    5555     * @access private
    5656     *
     57     * @param $required_methods Array String array of all methods required for implementation returned.
     58     *
    5759     * @return string|bool Class name for the first editor that claims to support the request. False if no editor claims to support the request.
    5860     */
    59     private final static function choose_implementation() {
    60 
    61         if ( null === self::$implementation ) {
    62             $request_order = apply_filters( 'wp_editors', array( 'imagick', 'gd' ) );
    63 
    64             // Loop over each editor on each request looking for one which will serve this request's needs
    65             foreach ( $request_order as $editor ) {
    66                 $class = 'WP_Image_Editor_' . $editor;
    67 
    68                 // Check to see if this editor is a possibility, calls the editor statically
    69                 if ( ! call_user_func( array( $class, 'test' ) ) )
    70                     continue;
    71 
    72                 self::$implementation = $class;
    73                 break;
    74             }
    75         }
    76         return self::$implementation;
    77     }
    78 
    79     abstract public function test(); // returns bool
     61    private final static function choose_implementation( $required_methods = null ) {
     62        $request_order = apply_filters( 'wp_image_editors',
     63            array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
     64
     65        if ( ! $required_methods )
     66            $required_methods = apply_filters( 'wp_image_editor_default_methods',
     67                array( 'resize', 'multi_resize', 'crop', 'rotate', 'flip', 'stream' ) );
     68
     69        // Loop over each editor on each request looking for one which will serve this request's needs
     70        foreach ( $request_order as $editor ) {
     71            // Check to see if this editor is a possibility, calls the editor statically
     72            if ( ! call_user_func( array( $editor, 'test' ) ) )
     73                continue;
     74
     75            // Make sure that all methods are supported by editor.
     76            if ( array_diff( $required_methods, get_class_methods( $editor ) ) )
     77                continue;
     78
     79            return $editor;
     80        }
     81        return false;
     82    }
     83
    8084    abstract protected function load(); // returns bool|WP_Error
    81     abstract public function supports_mime_type( $mime_type ); // returns bool
    82     abstract public function resize( $max_w, $max_h, $crop = false );
    83     abstract public function multi_resize( $sizes );
    84     abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false );
    85     abstract public function rotate( $angle );
    86     abstract public function flip( $horz, $vert );
    8785    abstract public function save( $destfilename = null, $mime_type = null );
    88     abstract public function stream( $mime_type = null );
     86
     87    /**
     88     * Implement all of the below to support natively used functions:
     89     *
     90     * public function resize( $max_w, $max_h, $crop = false )
     91     * public function multi_resize( $sizes )
     92     * public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false )
     93     * public function rotate( $angle )
     94     * public function flip( $horz, $vert )
     95     * public function stream( $mime_type = null )
     96     */
     97
     98    /**
     99     * Checks to see if current environment supports the editor chosen.
     100     * Must be overridden in a sub-class.
     101     *
     102     * @since 3.5.0
     103     * @access public
     104     * @abstract
     105     *
     106     * @param $args array
     107     * @return boolean
     108     */
     109    public static function test( $args = null ) {
     110        return false;
     111    }
     112
     113    /**
     114     * Checks to see if editor supports mime-type specified
     115     * Must be overridden in a sub-class.
     116     *
     117     * @since 3.5.0
     118     * @access public
     119     * @abstract
     120     *
     121     * @param string $mime_type
     122     * @return boolean
     123     */
     124    public static function supports_mime_type( $mime_type ) {
     125        return false;
     126    }
    89127
    90128    /**
     
    259297        $result = call_user_func_array( $function, $arguments );
    260298
    261         if( $result && $stream ) {
     299        if ( $result && $stream ) {
    262300            $contents = ob_get_contents();
    263301
    264302            $fp = fopen( $dst_file, 'w' );
    265303
    266             if( ! $fp )
     304            if ( ! $fp )
    267305                return false;
    268306
     
    271309        }
    272310
    273         if( $stream ) {
     311        if ( $stream ) {
    274312            ob_end_clean();
    275313        }
     
    296334
    297335        foreach( $extensions as $_extension ) {
    298             if( preg_match("/{$extension}/i", $_extension ) ) {
    299                 return $mime_types[ $_extension ];
     336            if ( preg_match( "/{$extension}/i", $_extension ) ) {
     337                return $mime_types[$_extension];
    300338            }
    301339        }
Note: See TracChangeset for help on using the changeset viewer.