WordPress.org

Make WordPress Core

Changeset 22094


Ignore:
Timestamp:
10/01/2012 08:59:06 PM (6 years ago)
Author:
ryan
Message:

Introduce WP_Image_Editor, WP_Image_Editor_Imagick, and WP_Image_Editor_GD. Abstracts image editing API and adds support for ImageMagick.

Props DH-Shredder, kurtpayne, markoheijnen
see #6821

Location:
trunk
Files:
3 added
6 edited

Legend:

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

    r21808 r22094  
    198198}
    199199
    200 function wp_stream_image($image, $mime_type, $post_id) {
    201     $image = apply_filters('image_save_pre', $image, $post_id);
    202 
    203     switch ( $mime_type ) {
    204         case 'image/jpeg':
    205             header('Content-Type: image/jpeg');
    206             return imagejpeg($image, null, 90);
    207         case 'image/png':
    208             header('Content-Type: image/png');
    209             return imagepng($image);
    210         case 'image/gif':
    211             header('Content-Type: image/gif');
    212             return imagegif($image);
    213         default:
     200/**
     201 * Streams image in WP_Image_Editor to browser.
     202 * Provided for backcompat reasons
     203 *
     204 * @param WP_Image_Editor $image
     205 * @param string $mime_type
     206 * @param int $post_id
     207 * @return boolean
     208 */
     209function wp_stream_image( $image, $mime_type, $post_id ) {
     210    if ( $image instanceof WP_Image_Editor ) {
     211        $image = apply_filters('image_editor_save_pre', $image, $post_id);
     212
     213        if ( is_wp_error( $image->stream( $mime_type ) ) )
    214214            return false;
    215     }
    216 }
    217 
    218 function wp_save_image_file($filename, $image, $mime_type, $post_id) {
    219     $image = apply_filters('image_save_pre', $image, $post_id);
    220     $saved = apply_filters('wp_save_image_file', null, $filename, $image, $mime_type, $post_id);
    221     if ( null !== $saved )
    222         return $saved;
    223 
    224     switch ( $mime_type ) {
    225         case 'image/jpeg':
    226             return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) );
    227         case 'image/png':
    228             return imagepng($image, $filename);
    229         case 'image/gif':
    230             return imagegif($image, $filename);
    231         default:
    232             return false;
     215
     216        return true;
     217    } else {
     218        _deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) );
     219
     220        $image = apply_filters('image_save_pre', $image, $post_id);
     221
     222        switch ( $mime_type ) {
     223            case 'image/jpeg':
     224                header( 'Content-Type: image/jpeg' );
     225                return imagejpeg( $image, null, 90 );
     226            case 'image/png':
     227                header( 'Content-Type: image/png' );
     228                return imagepng( $image );
     229            case 'image/gif':
     230                header( 'Content-Type: image/gif' );
     231                return imagegif( $image );
     232            default:
     233                return false;
     234        }
     235    }
     236}
     237
     238/**
     239 * Saves Image to File
     240 * @TODO: Add mime_type support to WP_Image_Editor
     241 *
     242 * @param string $filename
     243 * @param WP_Image_Editor $image
     244 * @param string $mime_type
     245 * @param int $post_id
     246 * @return boolean
     247 */
     248function wp_save_image_file( $filename, $image, $mime_type, $post_id ) {
     249    if ( $image instanceof WP_Image_Editor ) {
     250        $image = apply_filters('image_editor_save_pre', $image, $post_id);
     251        $saved = apply_filters('wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id);
     252
     253        if ( null !== $saved )
     254            return $saved;
     255
     256        return $image->save( $filename, $mime_type );
     257    } else {
     258        _deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) );
     259
     260        $image = apply_filters('image_save_pre', $image, $post_id);
     261        $saved = apply_filters('wp_save_image_file', null, $filename, $image, $mime_type, $post_id);
     262
     263        if ( null !== $saved )
     264            return $saved;
     265
     266        switch ( $mime_type ) {
     267            case 'image/jpeg':
     268                return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) );
     269            case 'image/png':
     270                return imagepng( $image, $filename );
     271            case 'image/gif':
     272                return imagegif( $image, $filename );
     273            default:
     274                return false;
     275        }
    233276    }
    234277}
     
    239282}
    240283
     284// @TODO: Returns GD resource, but is NOT public
    241285function _rotate_image_resource($img, $angle) {
     286    _deprecated_function( __FUNCTION__, '3.5', __( 'Use WP_Image_Editor::rotate' ) );
    242287    if ( function_exists('imagerotate') ) {
    243288        $rotated = imagerotate($img, $angle, 0);
     
    250295}
    251296
     297/**
     298 * @TODO: Only used within image_edit_apply_changes
     299 *        and receives/returns GD Resource.
     300 *        Consider removal.
     301 *
     302 * @param GD_Resource $img
     303 * @param boolean $horz
     304 * @param boolean $vert
     305 * @return GD_Resource
     306 */
    252307function _flip_image_resource($img, $horz, $vert) {
     308    _deprecated_function( __FUNCTION__, '3.5', __( 'Use WP_Image_Editor::flip' ) );
    253309    $w = imagesx($img);
    254310    $h = imagesy($img);
     
    268324}
    269325
     326/**
     327 * @TODO: Only used within image_edit_apply_changes
     328 *        and receives/returns GD Resource.
     329 *        Consider removal.
     330 *
     331 * @param GD_Resource $img
     332 * @param float $x
     333 * @param float $y
     334 * @param float $w
     335 * @param float $h
     336 * @return GD_Resource
     337 */
    270338function _crop_image_resource($img, $x, $y, $w, $h) {
    271339    $dst = wp_imagecreatetruecolor($w, $h);
     
    279347}
    280348
    281 function image_edit_apply_changes($img, $changes) {
     349/**
     350 * Performs group of changes on Editor specified.
     351 *
     352 * @param WP_Image_Editor $image
     353 * @param type $changes
     354 * @return WP_Image_Editor
     355 */
     356function image_edit_apply_changes( $image, $changes ) {
     357    if ( is_resource( $image ) )
     358        _deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) );
    282359
    283360    if ( !is_array($changes) )
    284         return $img;
     361        return $image;
    285362
    286363    // expand change operations
     
    327404
    328405    // image resource before applying the changes
    329     $img = apply_filters('image_edit_before_change', $img, $changes);
     406    if ( $image instanceof WP_Image_Editor )
     407        $image = apply_filters('wp_image_editor_before_change', $image, $changes);
     408    elseif ( is_resource( $image ) )
     409        $image = apply_filters('image_edit_before_change', $image, $changes);
    330410
    331411    foreach ( $changes as $operation ) {
    332412        switch ( $operation->type ) {
    333413            case 'rotate':
    334                 if ( $operation->angle != 0 )
    335                     $img = _rotate_image_resource($img, $operation->angle);
     414                if ( $operation->angle != 0 ) {
     415                    if ( $image instanceof WP_Image_Editor )
     416                        $image->rotate( $operation->angle );
     417                    else
     418                        $image = _rotate_image_resource( $image, $operation->angle );
     419                }
    336420                break;
    337421            case 'flip':
    338422                if ( $operation->axis != 0 )
    339                     $img = _flip_image_resource($img, ($operation->axis & 1) != 0, ($operation->axis & 2) != 0);
     423                    if ( $image instanceof WP_Image_Editor )
     424                        $image->flip( ($operation->axis & 1) != 0, ($operation->axis & 2) != 0 );
     425                    else
     426                        $image = _flip_image_resource( $image, ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 );
    340427                break;
    341428            case 'crop':
    342429                $sel = $operation->sel;
    343                 $scale = 1 / _image_get_preview_ratio( imagesx($img), imagesy($img) ); // discard preview scaling
    344                 $img = _crop_image_resource($img, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale);
     430
     431                if ( $image instanceof WP_Image_Editor ) {
     432                    $size = $image->get_size();
     433                    $w = $size['width'];
     434                    $h = $size['height'];
     435
     436                    $scale = 1 / _image_get_preview_ratio( $w, $h ); // discard preview scaling
     437                    $image->crop( $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
     438                } else {
     439                    $scale = 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // discard preview scaling
     440                    $image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
     441                }
    345442                break;
    346443        }
    347444    }
    348445
    349     return $img;
    350 }
    351 
    352 function stream_preview_image($post_id) {
    353     $post = get_post($post_id);
     446    return $image;
     447}
     448
     449
     450/**
     451 * Streams image in post to browser, along with enqueued changes
     452 * in $_REQUEST['history']
     453 *
     454 * @param int $post_id
     455 * @return boolean
     456 */
     457function stream_preview_image( $post_id ) {
     458    $post = get_post( $post_id );
    354459    @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
    355     $img = load_image_to_edit( $post_id, $post->post_mime_type, array(400, 400) );
    356 
    357     if ( !is_resource($img) )
    358         return false;
     460
     461    $img = WP_Image_Editor::get_instance( _load_image_to_edit_path( $post_id ) );
     462
     463    if ( is_wp_error( $img ) )
     464        return false;
    359465
    360466    $changes = !empty($_REQUEST['history']) ? json_decode( stripslashes($_REQUEST['history']) ) : null;
    361467    if ( $changes )
    362         $img = image_edit_apply_changes($img, $changes);
     468        $img = image_edit_apply_changes( $img, $changes );
    363469
    364470    // scale the image
    365     $w = imagesx($img);
    366     $h = imagesy($img);
    367     $ratio = _image_get_preview_ratio($w, $h);
     471    $size = $img->get_size();
     472    $w = $size['width'];
     473    $h = $size['height'];
     474
     475    $ratio = _image_get_preview_ratio( $w, $h );
    368476    $w2 = $w * $ratio;
    369477    $h2 = $h * $ratio;
    370478
    371     $preview = wp_imagecreatetruecolor($w2, $h2);
    372     imagecopyresampled( $preview, $img, 0, 0, 0, 0, $w2, $h2, $w, $h );
    373     wp_stream_image($preview, $post->post_mime_type, $post_id);
    374 
    375     imagedestroy($preview);
    376     imagedestroy($img);
    377     return true;
     479    if ( is_wp_error( $img->resize( $w2, $h2 ) ) )
     480        return false;
     481
     482    return wp_stream_image( $img, $post->post_mime_type, $post_id );
    378483}
    379484
     
    451556}
    452557
    453 function wp_save_image($post_id) {
     558/**
     559 * Saves image to post along with enqueued changes
     560 * in $_REQUEST['history']
     561 *
     562 * @param int $post_id
     563 * @return \stdClass
     564 */
     565function wp_save_image( $post_id ) {
    454566    $return = new stdClass;
    455567    $success = $delete = $scaled = $nocrop = false;
    456     $post = get_post($post_id);
    457     @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
    458     $img = load_image_to_edit($post_id, $post->post_mime_type);
    459 
    460     if ( !is_resource($img) ) {
     568    $post = get_post( $post_id );
     569
     570    $img = WP_Image_Editor::get_instance( _load_image_to_edit_path( $post_id, 'full' ) );
     571    if ( !$img ) {
    461572        $return->error = esc_js( __('Unable to create new image.') );
    462573        return $return;
     
    469580
    470581    if ( $scale && $fwidth > 0 && $fheight > 0 ) {
    471         $sX = imagesx($img);
    472         $sY = imagesy($img);
     582        $size = $img->get_size();
     583        $sX = $size['width'];
     584        $sY = $size['height'];
    473585
    474586        // check if it has roughly the same w / h ratio
     
    476588        if ( -0.1 < $diff && $diff < 0.1 ) {
    477589            // scale the full size image
    478             $dst = wp_imagecreatetruecolor($fwidth, $fheight);
    479             if ( imagecopyresampled( $dst, $img, 0, 0, 0, 0, $fwidth, $fheight, $sX, $sY ) ) {
    480                 imagedestroy($img);
    481                 $img = $dst;
     590            if ( $img->resize( $fwidth, $fheight ) )
    482591                $scaled = true;
    483             }
    484592        }
    485593
     
    552660            $backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']);
    553661
    554         $success = update_attached_file($post_id, $new_path);
    555 
    556         $meta['file'] = _wp_relative_upload_path($new_path);
    557         $meta['width'] = imagesx($img);
    558         $meta['height'] = imagesy($img);
     662        $success = update_attached_file( $post_id, $new_path );
     663
     664        $meta['file'] = _wp_relative_upload_path( $new_path );
     665
     666        $size = $img->get_size();
     667        $meta['width'] = $size['width'];
     668        $meta['height'] = $size['height'];
    559669
    560670        if ( $success && ('nothumb' == $target || 'all' == $target) ) {
     
    571681    }
    572682
    573     if ( isset($sizes) ) {
     683    if ( isset( $sizes ) ) {
     684        $_sizes = array();
     685
    574686        foreach ( $sizes as $size ) {
    575687            $tag = false;
    576             if ( isset($meta['sizes'][$size]) ) {
     688            if ( isset( $meta['sizes'][$size] ) ) {
    577689                if ( isset($backup_sizes["$size-orig"]) ) {
    578690                    if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] )
     
    587699
    588700            $crop = $nocrop ? false : get_option("{$size}_crop");
    589             $resized = image_make_intermediate_size($new_path, get_option("{$size}_size_w"), get_option("{$size}_size_h"), $crop );
    590 
    591             if ( $resized )
    592                 $meta['sizes'][$size] = $resized;
    593             else
    594                 unset($meta['sizes'][$size]);
    595         }
    596     }
     701            $_sizes[ $size ] = array( 'width' => get_option("{$size}_size_w"), 'height' => get_option("{$size}_size_h"), 'crop' => $crop );
     702        }
     703
     704        $meta['sizes'] = $img->multi_resize( $_sizes );
     705    }
     706
     707    unset( $img );
    597708
    598709    if ( $success ) {
    599         wp_update_attachment_metadata($post_id, $meta);
     710        wp_update_attachment_metadata( $post_id, $meta );
    600711        update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes);
    601712
     
    613724    if ( $delete ) {
    614725        $delpath = apply_filters('wp_delete_file', $new_path);
    615         @unlink($delpath);
    616     }
    617 
    618     imagedestroy($img);
     726        @unlink( $delpath );
     727    }
    619728
    620729    $return->msg = esc_js( __('Image saved') );
  • trunk/wp-admin/includes/image.php

    r21956 r22094  
    2323 * @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
    2424 */
    25 function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
    26     if ( is_numeric( $src ) ) { // Handle int as attachment ID
    27         $src_file = get_attached_file( $src );
     25function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
     26    if ( is_numeric( $src_file ) ) { // Handle int as attachment ID
     27        $src_file = get_attached_file( $src_file );
    2828        if ( ! file_exists( $src_file ) ) {
    2929            // If the file doesn't exist, attempt a url fopen on the src link.
    3030            // This can occur with certain file replication plugins.
    31             $post = get_post( $src );
    32             $image_type = $post->post_mime_type;
    33             $src = load_image_to_edit( $src, $post->post_mime_type, 'full' );
    34         } else {
    35             $size = @getimagesize( $src_file );
    36             $image_type = ( $size ) ? $size['mime'] : '';
    37             $src = wp_load_image( $src_file );
    38         }
    39     } else {
    40         $size = @getimagesize( $src );
    41         $image_type = ( $size ) ? $size['mime'] : '';
    42         $src = wp_load_image( $src );
    43     }
    44 
    45     if ( ! is_resource( $src ) )
    46         return new WP_Error( 'error_loading_image', $src, $src_file );
    47 
    48     $dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
    49 
    50     if ( $src_abs ) {
    51         $src_w -= $src_x;
    52         $src_h -= $src_y;
    53     }
    54 
    55     if ( function_exists( 'imageantialias' ) )
    56         imageantialias( $dst, true );
    57 
    58     imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
    59 
    60     imagedestroy( $src ); // Free up memory
     31            $src_file = _load_image_to_edit_path( $src_file, 'full' );
     32        }
     33    }
     34
     35    $editor = WP_Image_Editor::get_instance( $src_file );
     36    $src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
     37
     38    if ( is_wp_error( $src ) )
     39        return $src;
    6140
    6241    if ( ! $dst_file )
    6342        $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file );
    64 
    65     if ( 'image/png' != $image_type )
    66         $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
    6743
    6844    // The directory containing the original file may no longer exist when
     
    7248    $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
    7349
    74     if ( 'image/png' == $image_type && imagepng( $dst, $dst_file ) )
    75         return $dst_file;
    76     elseif ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) )
    77         return $dst_file;
    78     else
    79         return false;
     50    $result = $editor->save( $dst_file );
     51    return $dst_file;
    8052}
    8153
     
    12294        $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
    12395
    124         foreach ($sizes as $size => $size_data ) {
    125             $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] );
    126             if ( $resized )
    127                 $metadata['sizes'][$size] = $resized;
    128         }
     96        $editor = WP_Image_Editor::get_instance( $file );
     97        $metadata['sizes'] = $editor->multi_resize( $sizes );
    12998
    13099        // fetch additional metadata from exif/iptc
  • trunk/wp-includes/deprecated.php

    r21996 r22094  
    32073207
    32083208/**
     3209 * Load an image from a string, if PHP supports it.
     3210 *
     3211 * @since 2.1.0
     3212 * @deprecated 3.5.0
     3213 * @see WP_Image_Editor
     3214 *
     3215 * @param string $file Filename of the image to load.
     3216 * @return resource The resulting image resource on success, Error string on failure.
     3217 */
     3218function wp_load_image( $file ) {
     3219    _deprecated_function( __FUNCTION__, '3.5', 'WP_Image_Editor' );
     3220
     3221    if ( is_numeric( $file ) )
     3222        $file = get_attached_file( $file );
     3223
     3224    if ( ! file_exists( $file ) )
     3225        return sprintf(__('File &#8220;%s&#8221; doesn&#8217;t exist?'), $file);
     3226
     3227    if ( ! function_exists('imagecreatefromstring') )
     3228        return __('The GD image library is not installed.');
     3229
     3230    // Set artificially high because GD uses uncompressed images in memory
     3231    @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     3232    $image = imagecreatefromstring( file_get_contents( $file ) );
     3233
     3234    if ( !is_resource( $image ) )
     3235        return sprintf(__('File &#8220;%s&#8221; is not an image.'), $file);
     3236
     3237    return $image;
     3238}
     3239
     3240/**
     3241 * Scale down an image to fit a particular size and save a new copy of the image.
     3242 *
     3243 * The PNG transparency will be preserved using the function, as well as the
     3244 * image type. If the file going in is PNG, then the resized image is going to
     3245 * be PNG. The only supported image types are PNG, GIF, and JPEG.
     3246 *
     3247 * Some functionality requires API to exist, so some PHP version may lose out
     3248 * support. This is not the fault of WordPress (where functionality is
     3249 * downgraded, not actual defects), but of your PHP version.
     3250 *
     3251 * @since 2.5.0
     3252 * @deprecated 3.5.0
     3253 * @see WP_Image_Editor
     3254 *
     3255 * @param string $file Image file path.
     3256 * @param int $max_w Maximum width to resize to.
     3257 * @param int $max_h Maximum height to resize to.
     3258 * @param bool $crop Optional. Whether to crop image or resize.
     3259 * @param string $suffix Optional. File suffix.
     3260 * @param string $dest_path Optional. New image file path.
     3261 * @param int $jpeg_quality Optional, default is 90. Image quality percentage.
     3262 * @return mixed WP_Error on failure. String with new destination path.
     3263 */
     3264function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
     3265    _deprecated_function( __FUNCTION__, '3.5', 'WP_Image_Editor' );
     3266
     3267    $editor = WP_Image_Editor::get_instance( $file );
     3268    if ( is_wp_error( $editor ) )
     3269        return $editor;
     3270    $editor->set_quality( $jpeg_quality );
     3271
     3272    $resized = $editor->resize( $max_w, $max_h, $crop );
     3273    if ( is_wp_error( $resized ) )
     3274        return $resized;
     3275
     3276    $dest_file = $editor->generate_filename( $suffix, $dest_path );
     3277    $saved = $editor->save( $dest_file );
     3278
     3279    if ( is_wp_error( $saved ) )
     3280        return $saved;
     3281
     3282    return $dest_file;
     3283}
     3284
     3285/**
    32093286 * Retrieve a single post, based on post ID.
    32103287 *
  • trunk/wp-includes/functions.php

    r22082 r22094  
    12961296 */
    12971297function wp_mkdir_p( $target ) {
     1298    $wrapper = null;
     1299
     1300    // strip the protocol
     1301    if( wp_is_stream( $target ) ) {
     1302        list( $wrapper, $target ) = explode( '://', $target, 2 );
     1303    }
     1304
    12981305    // from php.net/mkdir user contributed notes
    12991306    $target = str_replace( '//', '/', $target );
     1307
     1308    // put the wrapper back on the target
     1309    if( $wrapper !== null ) {
     1310        $target = $wrapper . '://' . $target;
     1311    }
    13001312
    13011313    // safe mode fails with a trailing slash under certain PHP versions.
     
    37513763
    37523764/**
     3765 * Test if a given path is a stream URL
     3766 *
     3767 * @param string $path The resource path or URL
     3768 * @return bool True if the path is a stream URL
     3769 */
     3770function wp_is_stream( $path ) {
     3771    $wrappers = stream_get_wrappers();
     3772    $wrappers_re = '(' . join('|', $wrappers) . ')';
     3773
     3774    return preg_match( "!^$wrappers_re://!", $path ) === 1;
     3775}
     3776
     3777/**
    37533778 * Test if the supplied date is valid for the Gregorian calendar
    37543779 *
  • trunk/wp-includes/media.php

    r22073 r22094  
    237237
    238238/**
    239  * Load an image from a string, if PHP supports it.
    240  *
    241  * @since 2.1.0
    242  *
    243  * @param string $file Filename of the image to load.
    244  * @return resource The resulting image resource on success, Error string on failure.
    245  */
    246 function wp_load_image( $file ) {
    247     if ( is_numeric( $file ) )
    248         $file = get_attached_file( $file );
    249 
    250     if ( ! file_exists( $file ) )
    251         return sprintf(__('File &#8220;%s&#8221; doesn&#8217;t exist?'), $file);
    252 
    253     if ( ! function_exists('imagecreatefromstring') )
    254         return __('The GD image library is not installed.');
    255 
    256     // Set artificially high because GD uses uncompressed images in memory
    257     @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
    258     $image = imagecreatefromstring( file_get_contents( $file ) );
    259 
    260     if ( !is_resource( $image ) )
    261         return sprintf(__('File &#8220;%s&#8221; is not an image.'), $file);
    262 
    263     return $image;
    264 }
    265 
    266 /**
    267239 * Calculates the new dimensions for a downsampled image.
    268240 *
     
    394366
    395367/**
    396  * Scale down an image to fit a particular size and save a new copy of the image.
    397  *
    398  * The PNG transparency will be preserved using the function, as well as the
    399  * image type. If the file going in is PNG, then the resized image is going to
    400  * be PNG. The only supported image types are PNG, GIF, and JPEG.
    401  *
    402  * Some functionality requires API to exist, so some PHP version may lose out
    403  * support. This is not the fault of WordPress (where functionality is
    404  * downgraded, not actual defects), but of your PHP version.
    405  *
    406  * @since 2.5.0
    407  *
    408  * @param string $file Image file path.
    409  * @param int $max_w Maximum width to resize to.
    410  * @param int $max_h Maximum height to resize to.
    411  * @param bool $crop Optional. Whether to crop image or resize.
    412  * @param string $suffix Optional. File suffix.
    413  * @param string $dest_path Optional. New image file path.
    414  * @param int $jpeg_quality Optional, default is 90. Image quality percentage.
    415  * @return mixed WP_Error on failure. String with new destination path.
    416  */
    417 function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
    418 
    419     $image = wp_load_image( $file );
    420     if ( !is_resource( $image ) )
    421         return new WP_Error( 'error_loading_image', $image, $file );
    422 
    423     $size = @getimagesize( $file );
    424     if ( !$size )
    425         return new WP_Error('invalid_image', __('Could not read image size'), $file);
    426     list($orig_w, $orig_h, $orig_type) = $size;
    427 
    428     $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
    429     if ( !$dims )
    430         return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') );
    431     list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
    432 
    433     $newimage = wp_imagecreatetruecolor( $dst_w, $dst_h );
    434 
    435     imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
    436 
    437     // convert from full colors to index colors, like original PNG.
    438     if ( IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor( $image ) )
    439         imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) );
    440 
    441     // we don't need the original in memory anymore
    442     imagedestroy( $image );
    443 
    444     // $suffix will be appended to the destination filename, just before the extension
    445     if ( !$suffix )
    446         $suffix = "{$dst_w}x{$dst_h}";
    447 
    448     $info = pathinfo($file);
    449     $dir = $info['dirname'];
    450     $ext = $info['extension'];
    451     $name = wp_basename($file, ".$ext");
    452 
    453     if ( !is_null($dest_path) and $_dest_path = realpath($dest_path) )
    454         $dir = $_dest_path;
    455     $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
    456 
    457     if ( IMAGETYPE_GIF == $orig_type ) {
    458         if ( !imagegif( $newimage, $destfilename ) )
    459             return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
    460     } elseif ( IMAGETYPE_PNG == $orig_type ) {
    461         if ( !imagepng( $newimage, $destfilename ) )
    462             return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
    463     } else {
    464         // all other formats are converted to jpg
    465         if ( 'jpg' != $ext && 'jpeg' != $ext )
    466             $destfilename = "{$dir}/{$name}-{$suffix}.jpg";
    467         if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )
    468             return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
    469     }
    470 
    471     imagedestroy( $newimage );
    472 
    473     // Set correct file permissions
    474     $stat = stat( dirname( $destfilename ));
    475     $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
    476     @ chmod( $destfilename, $perms );
    477 
    478     return $destfilename;
    479 }
    480 
    481 /**
    482368 * Resize an image to make a thumbnail or intermediate size.
    483369 *
     
    494380 * @return bool|array False, if no image was created. Metadata array on success.
    495381 */
    496 function image_make_intermediate_size($file, $width, $height, $crop=false) {
     382function image_make_intermediate_size( $file, $width, $height, $crop = false ) {
    497383    if ( $width || $height ) {
    498         $resized_file = image_resize($file, $width, $height, $crop);
    499         if ( !is_wp_error($resized_file) && $resized_file && $info = getimagesize($resized_file) ) {
    500             $resized_file = apply_filters('image_make_intermediate_size', $resized_file);
    501             return array(
    502                 'file' => wp_basename( $resized_file ),
    503                 'width' => $info[0],
    504                 'height' => $info[1],
    505             );
     384        $editor = WP_Image_Editor::get_instance( $file );
     385
     386        if ( is_wp_error( $editor->resize( $width, $height, $crop ) ) );
     387            return false;
     388
     389        $resized_file = $editor->save();
     390
     391        if ( ! is_wp_error( $resized_file ) && $resized_file ) {
     392            unset( $resized_file['path'] );
     393            return $resized_file;
    506394        }
    507395    }
     
    1048936/**
    1049937 * Create new GD image resource with transparency support
     938 * @TODO: Deprecate if possible.
    1050939 *
    1051940 * @since 2.9.0
  • trunk/wp-settings.php

    r21999 r22094  
    144144require( ABSPATH . WPINC . '/admin-bar.php' );
    145145
     146require( ABSPATH . WPINC . '/class-wp-image-editor.php' );
     147require( ABSPATH . WPINC . '/class-wp-image-editor-gd.php' );
     148require( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' );
     149
    146150// Load multisite-specific files.
    147151if ( is_multisite() ) {
Note: See TracChangeset for help on using the changeset viewer.