WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/22/2012 09:52:16 AM (9 years ago)
Author:
nacin
Message:

WP_Image_Editor: the last stand.

  • Have wp_get_image_editor() rather than WP_Image_Editor::get_instance(). Having static factory methods would be less confusing if there weren't also static methods tied to individual editor implementations.
  • Lazy-load the WP_Image_Editor base class and editor implementations.
  • Have WP_Image_Editor_GD::supports_mime_type() actually check which types it supports.
  • Deprecate gd_edit_image_support() in favor of wp_image_editor_supports().

props DH-Shredder, scribu, markoheijnen. fixes #22356. see #6821.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/media.php

    r22816 r22817  
    384384function image_make_intermediate_size( $file, $width, $height, $crop = false ) {
    385385    if ( $width || $height ) {
    386         $editor = WP_Image_Editor::get_instance( $file );
     386        $editor = wp_get_image_editor( $file );
    387387
    388388        if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) )
     
    905905
    906906/**
    907  * Check if the installed version of GD supports particular image type
    908  *
    909  * @since 2.9.0
    910  *
    911  * @param string $mime_type
    912  * @return bool
    913  */
    914 function gd_edit_image_support($mime_type) {
    915     if ( function_exists('imagetypes') ) {
    916         switch( $mime_type ) {
    917             case 'image/jpeg':
    918                 return (imagetypes() & IMG_JPG) != 0;
    919             case 'image/png':
    920                 return (imagetypes() & IMG_PNG) != 0;
    921             case 'image/gif':
    922                 return (imagetypes() & IMG_GIF) != 0;
    923         }
    924     } else {
    925         switch( $mime_type ) {
    926             case 'image/jpeg':
    927                 return function_exists('imagecreatefromjpeg');
    928             case 'image/png':
    929                 return function_exists('imagecreatefrompng');
    930             case 'image/gif':
    931                 return function_exists('imagecreatefromgif');
    932         }
    933     }
    934     return false;
    935 }
    936 
    937 /**
    938907 * Create new GD image resource with transparency support
    939908 * @TODO: Deprecate if possible.
     
    11691138    $bytes   = apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes );
    11701139    return $bytes;
     1140}
     1141
     1142/**
     1143 * Returns a WP_Image_Editor instance and loads file into it.
     1144 *
     1145 * @since 3.5.0
     1146 * @access public
     1147 *
     1148 * @param string $path Path to file to load
     1149 * @param array $args Additional data. Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} }
     1150 * @return WP_Image_Editor|WP_Error
     1151 */
     1152function wp_get_image_editor( $path, $args = array() ) {
     1153    $args['path'] = $path;
     1154
     1155    if ( ! isset( $args['mime_type'] ) ) {
     1156        $file_info  = wp_check_filetype( $args['path'] );
     1157
     1158        // If $file_info['type'] is false, then we let the editor attempt to
     1159        // figure out the file type, rather than forcing a failure based on extension.
     1160        if ( isset( $file_info ) && $file_info['type'] )
     1161            $args['mime_type'] = $file_info['type'];
     1162    }
     1163
     1164    $implementation = apply_filters( 'wp_image_editor_class', _wp_image_editor_choose( $args ) );
     1165
     1166    if ( $implementation ) {
     1167        $editor = new $implementation( $path );
     1168        $loaded = $editor->load();
     1169
     1170        if ( is_wp_error( $loaded ) )
     1171            return $loaded;
     1172
     1173        return $editor;
     1174    }
     1175
     1176    return new WP_Error( 'image_no_editor', __('No editor could be selected.') );
     1177}
     1178
     1179/**
     1180 * Tests whether there is an editor that supports a given mime type or methods.
     1181 *
     1182 * @since 3.5.0
     1183 * @access public
     1184 *
     1185 * @param string|array $args Array of requirements.  Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} }
     1186 * @return boolean true if an eligible editor is found; false otherwise
     1187 */
     1188function wp_image_editor_supports( $args = array() ) {
     1189    return (bool) _wp_image_editor_choose( $args );
     1190}
     1191
     1192/**
     1193 * Tests which editors are capable of supporting the request.
     1194 *
     1195 * @since 3.5.0
     1196 * @access private
     1197 *
     1198 * @param array $args Additional data. Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} }
     1199 * @return string|bool Class name for the first editor that claims to support the request. False if no editor claims to support the request.
     1200 */
     1201function _wp_image_editor_choose( $args = array() ) {
     1202    require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
     1203    require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
     1204    require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
     1205
     1206    $implementations = apply_filters( 'wp_image_editors',
     1207        array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
     1208
     1209    foreach ( $implementations as $implementation ) {
     1210        if ( ! call_user_func( array( $implementation, 'test' ), $args ) )
     1211            continue;
     1212
     1213        if ( isset( $args['mime_type'] ) &&
     1214            ! call_user_func(
     1215                array( $implementation, 'supports_mime_type' ),
     1216                $args['mime_type'] ) ) {
     1217            continue;
     1218        }
     1219
     1220        if ( isset( $args['methods'] ) &&
     1221             array_diff( $args['methods'], get_class_methods( $implementation ) ) ) {
     1222            continue;
     1223        }
     1224
     1225        return $implementation;
     1226    }
     1227
     1228    return false;
    11711229}
    11721230
Note: See TracChangeset for help on using the changeset viewer.