Make WordPress Core


Ignore:
Timestamp:
09/30/2009 08:38:32 AM (16 years ago)
Author:
azaozz
Message:

Image Editor: add load and save image filters, fix backup sizes meta, see #10528

File:
1 edited

Legend:

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

    r11973 r11984  
    1111    $meta = wp_get_attachment_metadata($post_id);
    1212    $thumb = image_get_intermediate_size($post_id, 'thumbnail');
     13    $sub_sizes = isset($meta['sizes']) && is_array($meta['sizes']);
    1314    $note = '';
    1415
     
    1617        $big = max( $meta['width'], $meta['height'] );
    1718    else
    18         wp_die( __('Image data does not exist. Please re-upload the image.') );
     19        die( __('Image data does not exist. Please re-upload the image.') );
    1920
    2021    $sizer = $big > 400 ? 400 / $big : 1;
    2122
    2223    $backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
    23     $can_restore = !empty($backup_sizes) && isset($backup_sizes['full-orig']);
     24    $can_restore = !empty($backup_sizes) && isset($backup_sizes['full-orig'])
     25        && $backup_sizes['full-orig']['file'] != basename($meta['file']);
    2426
    2527
    2628    // temp convert backup sizes
    27     if ( isset($meta['sizes']) && is_array($meta['sizes']) ) {
     29    if ( $sub_sizes ) {
    2830        $update = false;
    2931        foreach ( $meta['sizes'] as $name => $val ) {
     
    4648    }
    4749    // end temp
    48 
    4950
    5051    if ( $msg ) {
     
    169170    </div>
    170171
    171     <?php if ( $thumb ) {
     172    <?php if ( $thumb && $sub_sizes ) {
    172173        $thumb_img = wp_constrain_dimensions( $thumb['width'], $thumb['height'], 160, 120 );
    173174    ?>
     
    212213}
    213214
    214 function load_image_to_edit($post, $size = 'full') {
    215     $filename = get_attached_file($post->ID);
    216 
    217     if ( 'full' != $size && ( $data = image_get_intermediate_size($post->ID, $size) ) )
    218         $filename = path_join( dirname($filename), $data['file'] );
    219 
    220     switch ( $post->post_mime_type ) {
     215function load_image_to_edit($post_id, $mime_type, $size = 'full') {
     216    $filepath = get_attached_file($post_id);
     217
     218    if ( $filepath && file_exists($filepath) ) {
     219        if ( 'full' != $size && ( $data = image_get_intermediate_size($post_id, $size) ) )
     220            $filepath = path_join( dirname($filepath), $data['file'] );
     221    } elseif ( function_exists('fopen') ) {
     222        $filepath = wp_get_attachment_url($post_id);
     223    }
     224
     225    $filepath = apply_filters('load_image_to_edit_path', $filepath, $post_id, $size);
     226    if ( empty($filepath) )
     227        return false;
     228
     229    switch ( $mime_type ) {
    221230        case 'image/jpeg':
    222             $image = imagecreatefromjpeg($filename);
     231            $image = imagecreatefromjpeg($filepath);
    223232            break;
    224233        case 'image/png':
    225             $image = imagecreatefrompng($filename);
     234            $image = imagecreatefrompng($filepath);
    226235            break;
    227236        case 'image/gif':
    228             $image = imagecreatefromgif($filename);
     237            $image = imagecreatefromgif($filepath);
    229238            break;
    230239        default:
     
    233242    }
    234243    if ( is_resource($image) ) {
    235         $image = apply_filters('load_image_to_edit', $image, $post->ID, $size);
     244        $image = apply_filters('load_image_to_edit', $image, $post_id, $size);
    236245        if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
    237246            imagealphablending($image, false);
     
    242251}
    243252
    244 function wp_stream_image($image, $mime_type, $post_id = 0, $intermediate_size = '') {
    245     $image = apply_filters('image_save_pre', $image, $post->ID, $intermediate_size);
     253function wp_stream_image($image, $mime_type, $post_id) {
     254    $image = apply_filters('image_save_pre', $image, $post_id);
    246255
    247256    switch ( $mime_type ) {
     
    260269}
    261270
    262 function wp_save_image_file($filename, $image, $mime_type, $post_id = 0, $intermediate_size = '') {
    263     $image = apply_filters('image_save_pre', $image, $post->ID, $intermediate_size);
     271function wp_save_image_file($filename, $image, $mime_type, $post_id) {
     272    $image = apply_filters('image_save_pre', $image, $post_id);
     273    $saved = apply_filters('wp_save_image_file', null, $filename, $image, $mime_type, $post_id);
     274    if ( null !== $saved )
     275        return $saved;
    264276
    265277    switch ( $mime_type ) {
     
    395407    $post = get_post($post_id);
    396408    @ini_set('memory_limit', '256M');
    397     $img = load_image_to_edit( $post, array(400, 400) );
     409    $img = load_image_to_edit( $post_id, $post->post_mime_type, array(400, 400) );
    398410
    399411    if ( !is_resource($img) )
     
    440452            $data = $backup_sizes["$default_size-orig"];
    441453            if ( 'full' == $default_size ) {
    442                 $backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $parts['basename']);
     454                if ( $parts['basename'] != $data['file'] )
     455                    $backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $parts['basename']);
    443456
    444457                $meta['file'] = path_join($parts['dirname'], $data['file']);
     
    450463                $restored = update_attached_file($post_id, $meta['file']);
    451464            } else {
    452                 if ( isset($meta['sizes'][$default_size]) )
     465                if ( isset($meta['sizes'][$default_size]) && $meta['sizes'][$default_size]['file'] != $data['file'] )
    453466                    $backup_sizes["$default_size-{$suffix}"] = $meta['sizes'][$default_size];
    454467
    455468                $meta['sizes'][$default_size] = $data;
    456469            }
     470        } else {
     471            unset($meta['sizes'][$default_size]);
    457472        }
    458473    }
     
    476491    $post = get_post($post_id);
    477492    @ini_set('memory_limit', '256M');
    478     $img = load_image_to_edit($post);
     493    $img = load_image_to_edit($post_id, $post->post_mime_type);
    479494
    480495    if ( !is_resource($img) ) {
     
    550565    }
    551566
    552     if ( 'nothumb' == $target || 'all' == $target || $scaled ) {
    553         $tag = !isset($backup_sizes['full-orig']) ? 'full-orig' : "full-$suffix";
    554         $backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']);
     567    if ( 'nothumb' == $target || 'all' == $target || 'full' == $target || $scaled ) {
     568        $tag = false;
     569        if ( isset($backup_sizes['full-orig']) ) {
     570            if ( $backup_sizes['full-orig']['file'] != $path_parts['basename'] )
     571                $tag = "full-$suffix";
     572        } else {
     573            $tag = 'full-orig';
     574        }
     575
     576        if ( $tag )
     577            $backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']);
    555578
    556579        $success = update_attached_file($post_id, $new_path);
     
    577600    if ( isset($sizes) ) {
    578601        foreach ( $sizes as $size ) {
     602            $tag = false;
    579603            if ( isset($meta['sizes'][$size]) ) {
    580                 $tag = !isset($backup_sizes["$size-orig"]) ? "$size-orig" : "$size-$suffix";
    581                 $backup_sizes[$tag] = $meta['sizes'][$size];
     604                if ( isset($backup_sizes["$size-orig"]) ) {
     605                    if ( $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] )
     606                        $tag = "$size-$suffix";
     607                } else {
     608                    $tag = "$size-orig";
     609                }
     610
     611                if ( $tag )
     612                    $backup_sizes[$tag] = $meta['sizes'][$size];
    582613            }
    583614
     
    596627        update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes);
    597628
    598         if ( $target == 'thumbnail' || $target == 'all' ) {
     629        if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' ) {
    599630            if ( $thumb = $meta['sizes']['thumbnail'] ) {
    600631                $file_url = wp_get_attachment_url($post_id);
Note: See TracChangeset for help on using the changeset viewer.