WordPress.org

Make WordPress Core


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.