WordPress.org

Make WordPress Core

Changeset 20769


Ignore:
Timestamp:
05/11/12 16:15:15 (2 years ago)
Author:
ryan
Message:

Check wp_crop_image() for a false return value. Don't delete original image if crop not sucessful. Don't delete the original image when wp_crop_image() returns it untouched. Prevents deletion of header image when no cropping is done to the originally uploaded image. Props SergeyBiryukov, westi. fixes #20657

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/custom-header.php

    r20768 r20769  
    753753            $oitar = $width / $max_width; 
    754754            $image = wp_crop_image($id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file)); 
    755             if ( is_wp_error( $image ) ) 
     755            if ( ! $image || is_wp_error( $image ) ) 
    756756                wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); 
    757757 
     
    869869 
    870870        $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height ); 
    871         if ( is_wp_error( $cropped ) ) 
     871        if ( ! $cropped || is_wp_error( $cropped ) ) 
    872872            wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); 
    873873 
    874874        $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id); // For replication 
     875        $is_cropped = ( get_attached_file( $attachment_id ) != $cropped ); 
    875876 
    876877        $parent = get_post($attachment_id); 
    877878        $parent_url = $parent->guid; 
    878         $url = str_replace(basename($parent_url), basename($cropped), $parent_url); 
     879        $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url ); 
    879880 
    880881        $size = @getimagesize( $cropped ); 
     
    890891            'context' => 'custom-header' 
    891892        ); 
    892         if ( isset( $_POST['new-attachment'] ) && $_POST['new-attachment'] ) 
    893             unset($object['ID']); 
     893        if ( ! empty( $_POST['new-attachment'] ) ) 
     894            unset( $object['ID'] ); 
    894895 
    895896        // Update the attachment 
    896897        $attachment_id = wp_insert_attachment( $object, $cropped ); 
    897898        wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) ); 
    898         update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) ); 
     899        update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_option( 'stylesheet' ) ); 
    899900 
    900901        set_theme_mod('header_image', $url); 
     
    910911 
    911912        // cleanup 
    912         $medium = str_replace(basename($original), 'midsize-'.basename($original), $original); 
     913        $medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original ); 
    913914        if ( file_exists( $medium ) ) 
    914915            @unlink( apply_filters( 'wp_delete_file', $medium ) ); 
    915         if ( empty ( $_POST['new-attachment'] ) ) 
     916        if ( empty( $_POST['new-attachment'] ) && $is_cropped ) 
    916917            @unlink( apply_filters( 'wp_delete_file', $original ) ); 
    917918 
Note: See TracChangeset for help on using the changeset viewer.