Make WordPress Core


Ignore:
Timestamp:
09/24/2009 01:54:07 AM (15 years ago)
Author:
azaozz
Message:

Image Editor improvements, see #10528

File:
1 edited

Legend:

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

    r11930 r11965  
    77 */
    88
    9 function wp_image_editor($post_id) {
     9function wp_image_editor($post_id, $msg = false) {
    1010    $nonce = wp_create_nonce("image_editor-$post_id");
    11     $image_size_opt = "<option value='all'>" . __('all image sizes') . "</option>\n";
    12     $image_size_opt .= "<option value='full'>" . __('original image') . "</option>\n";
    13 
    1411    $meta = wp_get_attachment_metadata($post_id);
    15     if ( is_array($meta) && is_array($meta['sizes']) ) {
    16         $sizes = apply_filters('intermediate_image_sizes', array('thumbnail', 'medium', 'large'));
    17         $size_names = array(
    18             'thumbnail' => __('thumbnail'),
    19             'medium' => __('medium'),
    20             'large' => __('large')
    21         );
    22 
    23         foreach ( $sizes as $size ) {
    24             if ( array_key_exists($size, $meta['sizes']) ) {
    25                 $size_name = isset($size_names[$size]) ? $size_names[$size] : $size;
    26                 $image_size_opt .= "<option value='$size'>$size_name</option>\n";
     12    $thumb = image_get_intermediate_size($post_id, 'thumbnail');
     13    $note = '';
     14
     15    if ( is_array($meta) && isset($meta['width']) )
     16        $big = max( $meta['width'], $meta['height'] );
     17    else
     18        wp_die( __('Image data does not exist. Please re-upload the image.') );
     19
     20    $sizer = $big > 400 ? 400 / $big : 1;
     21
     22    $backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
     23    $can_restore = !empty($backup_sizes) && isset($backup_sizes['full-orig']);
     24
     25
     26    // temp convert backup sizes
     27    if ( isset($meta['sizes']) && is_array($meta['sizes']) ) {
     28        $update = false;
     29        foreach ( $meta['sizes'] as $name => $val ) {
     30            if ( strpos($name, 'backup-') === 0 ) {
     31                $m = array();
     32                preg_match('/backup-([0-9]+)-(.*)/', $name, $m);
     33                if ( !isset($backup_sizes["{$m[2]}-orig"]) )
     34                    $n = "{$m[2]}-orig";
     35                else
     36                    $n = "{$m[2]}-{$m[1]}";
     37                $backup_sizes[$n] = $val;
     38                unset($meta['sizes'][$name]);
     39                $update = true;
    2740            }
    2841        }
     42        if ( $update ) {
     43            wp_update_attachment_metadata( $post_id, $meta );
     44            update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes);
     45        }
     46    }
     47    // end temp
     48
     49
     50    if ( $msg ) {
     51        if ( isset($msg->error) )
     52            $note = "<div class='error'><p>$msg->error</p></div>";
     53        elseif ( isset($msg->msg) )
     54            $note = "<div class='updated'><p>$msg->msg</p></div>";
     55    }
     56
     57    ?>
     58    <div class="imgedit-wrap">
     59    <?php echo $note; ?>
     60    <table id="imgedit-panel-<?php echo $post_id; ?>"><tbody>
     61    <tr><td>
     62    <div class="imgedit-menu">
     63        <div onclick="imageEdit.crop(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-crop disabled" title="<?php echo esc_attr__( 'Crop' ); ?>"></div><?php
     64
     65    if ( function_exists('imagerotate') ) { ?>
     66
     67        <div onclick="imageEdit.rotate(90, <?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-rleft" title="<?php echo esc_attr__( 'Rotate couter-clockwise' ); ?>"></div>
     68        <div onclick="imageEdit.rotate(-90, <?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-rright" title="<?php echo esc_attr__( 'Rotate clockwise' ); ?>"></div><?php
     69
    2970    } ?>
    3071
    31     <div class="imgedit-wrap">
    32     <div id="imgedit-panel-<?php echo $post_id; ?>">
    33         <div class="imgedit-menu">
    34         <div onclick="imageEdit.crop(<?php echo "$post_id, '$nonce'"; ?>)" class="imgedit-crop" title="<?php echo esc_attr__( 'Crop' ); ?>"></div><?php
    35 
    36     if ( function_exists('imagerotate') ) { ?>
    37 
    38         <div onclick="imageEdit.rotate(90, <?php echo "$post_id, '$nonce'"; ?>)" class="imgedit-rleft" title="<?php echo esc_attr__( 'Rotate couter-clockwise' ); ?>"></div>
    39         <div onclick="imageEdit.rotate(-90, <?php echo "$post_id, '$nonce'"; ?>)" class="imgedit-rright" title="<?php echo esc_attr__( 'Rotate clockwise' ); ?>"></div><?php
    40 
    41     } ?>
    42 
    43         <div onclick="imageEdit.flip(1, <?php echo "$post_id, '$nonce'"; ?>)" class="imgedit-fliph" title="<?php echo esc_attr__( 'Flip horizontally' ); ?>"></div>
    44         <div onclick="imageEdit.flip(2, <?php echo "$post_id, '$nonce'"; ?>)" class="imgedit-flipv" title="<?php echo esc_attr__( 'Flip vertically' ); ?>"></div>
    45 
    46         <div id="image-undo-<?php echo $post_id; ?>" onclick="imageEdit.undo(<?php echo "$post_id, '$nonce'"; ?>)" class="imgedit-undo disabled" title="<?php echo esc_attr__( 'Undo' ); ?>"></div>
    47         <div id="image-redo-<?php echo $post_id; ?>" onclick="imageEdit.redo(<?php echo "$post_id, '$nonce'"; ?>)" class="imgedit-redo disabled" title="<?php echo esc_attr__( 'Redo' ); ?>"></div>
     72        <div onclick="imageEdit.flip(1, <?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-fliph" title="<?php echo esc_attr__( 'Flip horizontally' ); ?>"></div>
     73        <div onclick="imageEdit.flip(2, <?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-flipv" title="<?php echo esc_attr__( 'Flip vertically' ); ?>"></div>
     74
     75        <div id="image-undo-<?php echo $post_id; ?>" onclick="imageEdit.undo(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-undo disabled" title="<?php echo esc_attr__( 'Undo' ); ?>"></div>
     76        <div id="image-redo-<?php echo $post_id; ?>" onclick="imageEdit.redo(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-redo disabled" title="<?php echo esc_attr__( 'Redo' ); ?>"></div>
    4877        <br class="clear" />
     78    </div>
     79
     80    <input type="hidden" id="imgedit-sizer-<?php echo $post_id; ?>" value="<?php echo $sizer; ?>" />
     81    <input type="hidden" id="imgedit-minthumb-<?php echo $post_id; ?>" value="<?php echo ( get_option('thumbnail_size_w') . ':' . get_option('thumbnail_size_h') ); ?>" />
     82    <input type="hidden" id="imgedit-history-<?php echo $post_id; ?>" value="" />
     83    <input type="hidden" id="imgedit-undone-<?php echo $post_id; ?>" value="0" />
     84    <input type="hidden" id="imgedit-selection-<?php echo $post_id; ?>" value="" />
     85    <input type="hidden" id="imgedit-x-<?php echo $post_id; ?>" value="<?php echo $meta['width']; ?>" />
     86    <input type="hidden" id="imgedit-y-<?php echo $post_id; ?>" value="<?php echo $meta['height']; ?>" />
     87
     88    <div id="imgedit-crop-<?php echo $post_id; ?>" class="imgedit-crop-wrap">
     89    <img id="image-preview-<?php echo $post_id; ?>" onload="imageEdit.imgLoaded('<?php echo $post_id; ?>')" src="<?php echo admin_url('admin-ajax.php'); ?>?action=imgedit-preview&amp;_ajax_nonce=<?php echo $nonce; ?>&amp;postid=<?php echo $post_id; ?>&amp;rand=<?php echo rand(1, 99999); ?>" />
     90    </div>
     91
     92    <div class="imgedit-submit">
     93        <input type="button" onclick="imageEdit.close(<?php echo "$post_id, '$nonce'"; ?>)" class="button" value="<?php echo esc_attr__( 'Cancel' ); ?>" />
     94        <input type="button" onclick="imageEdit.save(<?php echo "$post_id, '$nonce'"; ?>)" class="button-primary imgedit-submit-btn" value="<?php echo esc_attr__( 'Save' ); ?>" />
     95    </div>
     96    </td>
     97
     98    <td class="imgedit-settings">
     99    <div class="imgedit-group">
     100    <div class="imgedit-group-top">
     101        <a class="imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;" href="#"><strong><?php _e('Scale Image'); ?></strong></a>
     102        <div class="imgedit-help">
     103        <p><?php _e('You can proportionally scale the original image. For best results the scaling should be done before performing any other operations on it like crop, rotate, etc. Note that if you make the image larger it may become fuzzy.'); ?></p>
     104        <p><?php printf( __('Original dimensions %s'), $meta['width'] . '&times;' . $meta['height'] ); ?></p>
     105        <div class="imgedit-submit">
     106        <span class="nowrap"><input type="text" id="imgedit-scale-width-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleChanged(<?php echo $post_id; ?>, 1)" onblur="imageEdit.scaleChanged(<?php echo $post_id; ?>, 1)" style="width:4em;" value="<?php echo $meta['width']; ?>" />&times;<input type="text" id="imgedit-scale-height-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleChanged(<?php echo $post_id; ?>, 0)" onblur="imageEdit.scaleChanged(<?php echo $post_id; ?>, 0)" style="width:4em;" value="<?php echo $meta['height']; ?>" />
     107        <span class="imgedit-scale-warn" id="imgedit-scale-warn-<?php echo $post_id; ?>">!</span></span>
     108        <input type="button" onclick="imageEdit.action(<?php echo "$post_id, '$nonce'"; ?>, 'scale')" class="button-primary" value="<?php echo esc_attr__( 'Scale' ); ?>" />
    49109        </div>
    50 
    51         <p>
    52         <span id="imgedit-scale-<?php echo $post_id; ?>">
    53             <input type="checkbox" onchange="imageEdit.scaleSwitched(<?php echo $post_id; ?>)" id="imgedit-scale-switch-<?php echo $post_id; ?>" /><label for="imgedit-scale-switch-<?php echo $post_id; ?>">Scale full size image:</label>
    54             <span id="imgedit-scale-values-<?php echo $post_id; ?>">
    55                 <input type="text" id="imgedit-scale-width-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleWidthChanged(<?php echo $post_id; ?>)" style="width:4em;" />
    56                 &times;
    57                 <input type="text" id="imgedit-scale-height-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleHeightChanged(<?php echo $post_id; ?>)" style="width:4em;" />
    58             </span>
     110        </div>
     111    </div>
     112
     113<?php if ( $can_restore ) { ?>
     114
     115    <div class="imgedit-group-top">
     116        <a class="imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;" href="#"><strong><?php _e('Restore Original Image'); ?></strong></a>
     117        <div class="imgedit-help">
     118        <p><?php _e('Discard any changes and restore the original image. Previously edited copies of the image will not be deleted.'); ?></p>
     119        <div class="imgedit-submit">
     120        <input type="button" onclick="imageEdit.action(<?php echo "$post_id, '$nonce'"; ?>, 'restore')" class="button-primary" value="<?php echo esc_attr__( 'Restore image' ); ?>" <?php echo $can_restore; ?> />
     121        </div>
     122        </div>
     123    </div>
     124
     125<?php } ?>
     126
     127    </div>
     128
     129    <div class="imgedit-group">
     130    <div class="imgedit-group-top">
     131        <strong><?php _e('Image Crop'); ?></strong>
     132        <a class="imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;" href="#"><?php _e('(help)'); ?></a>
     133        <div class="imgedit-help">
     134        <p><?php _e('The image can be cropped by clicking on it and dragging to select the desired part. While dragging the dimensions of the selection are displayed below.'); ?></p>
     135        <strong><?php _e('Keyboard shortcuts'); ?></strong>
     136        <ul>
     137        <li><?php _e('Arrow: move by 10px'); ?></li>
     138        <li><?php _e('Shift + arrow: move by 1px'); ?></li>
     139        <li><?php _e('Ctrl + arrow: resize by 10px'); ?></li>
     140        <li><?php _e('Ctrl + Shift + arrow: resize by 1px'); ?></li>
     141        <li><?php _e('Shift + drag: lock aspect ratio'); ?></li>
     142        </ul>
     143
     144        <p><strong><?php _e('Crop Aspect Ratio'); ?></strong><br />
     145        <?php _e('You can specify the crop selection aspect ratio then hold down the Shift key while dragging to lock it. The values can be 1:1 (square), 4:3, 16:9, etc. If there is a selection, specifying aspect ratio will set it immediately.'); ?></p>
     146
     147        <p><strong><?php _e('Crop Selection'); ?></strong><br />
     148        <?php _e('Once started, the selection can be adjusted by entering new values (in pixels). Note that these values are scaled to approximately match the original image dimensions. The minimum selection size equals the thumbnail size as set in the Media settings.'); ?></p>
     149        </div>
     150    </div>
     151
     152    <p>
     153        <?php _e('Aspect ratio:'); ?>
     154        <span  class="nowrap">
     155        <input type="text" id="imgedit-crop-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 0, this)" style="width:3em;" />
     156        :
     157        <input type="text" id="imgedit-crop-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 1, this)" style="width:3em;" />
    59158        </span>
    60         </p>
    61 
    62         <input type="hidden" id="imgedit-history-<?php echo $post_id; ?>" value="" />
    63         <input type="hidden" id="imgedit-undone-<?php echo $post_id; ?>" value="0" />
    64         <input type="hidden" id="imgedit-selection-<?php echo $post_id; ?>" value="" />
    65         <input type="hidden" id="imgedit-aspect-x-<?php echo $post_id; ?>" value="" />
    66         <input type="hidden" id="imgedit-aspect-y-<?php echo $post_id; ?>" value="" />
    67 
    68         <h4><?php _e('Preview Image:'); ?></h4>
    69         <div id="imgedit-crop-<?php echo $post_id; ?>" style="position:relative;">
    70         <img src="<?php echo admin_url('admin-ajax.php') . "?action=load-preview-image&amp;_ajax_nonce={$nonce}&amp;postid={$post_id}&amp;ver=" . rand(1, 99999); ?>" id="image-preview-<?php echo $post_id; ?>" />
    71         </div>
    72 
    73         <p>
    74         <?php _e('Apply to:'); ?>
    75         <select id="imgedit-save-target-<?php echo $post_id; ?>" onchange="imageEdit.targetChanged(<?php echo $post_id; ?>)">
    76         <?php echo $image_size_opt; ?>
    77         </select>
    78         </p>
    79 
    80         <p>
    81         <input type="button" onclick="imageEdit.close(<?php echo "$post_id, '$nonce'"; ?>)" class="button" value="<?php echo esc_attr__( 'Close' ); ?>" />
    82         <input type="button" onclick="imageEdit.save(<?php echo "$post_id, '$nonce'"; ?>)" class="button-primary" value="<?php echo esc_attr__( 'Save' ); ?>" />
    83         </p>
    84         <script type="text/javascript">imageEdit.targetChanged(<?php echo $post_id; ?>);</script>
    85     </div>
     159    </p>
     160
     161    <p id="imgedit-crop-sel-<?php echo $post_id; ?>">
     162        <?php _e('Selection:'); ?>
     163        <span  class="nowrap">
     164        <input type="text" id="imgedit-sel-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>)" style="width:4em;" />
     165        :
     166        <input type="text" id="imgedit-sel-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>)" style="width:4em;" />
     167        </span>
     168    </p>
     169    </div>
     170
     171    <?php if ( $thumb ) {
     172        $thumb_img = wp_constrain_dimensions( $thumb['width'], $thumb['height'], 160, 120 );
     173    ?>
     174
     175    <div class="imgedit-group imgedit-applyto">
     176    <div class="imgedit-group-top">
     177        <strong><?php _e('Thumbnail Settings'); ?></strong>
     178        <a class="imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;" href="#"><?php _e('(help)'); ?></a>
     179        <p class="imgedit-help"><?php _e('The thumbnail image can be cropped differently. For example it can be square or contain only a portion of the original image to showcase it better. Here you can select whether to apply changes to all image sizes or make the thumbnail different.'); ?></p>
     180    </div>
     181
     182    <p>
     183        <img src="<?php echo $thumb['url']; ?>" width="<?php echo $thumb_img[0]; ?>" height="<?php echo $thumb_img[1]; ?>" class="imgedit-size-preview" alt="" /><br /><?php _e('Current thumbnail'); ?>
     184    </p>
     185
     186    <p id="imgedit-save-target-<?php echo $post_id; ?>">
     187        <strong><?php _e('Apply changes to:'); ?></strong><br />
     188
     189        <label class="imgedit-label">
     190        <input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="all" checked="checked" />
     191        <?php _e('All image sizes'); ?></label>
     192
     193        <label class="imgedit-label">
     194        <input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="thumbnail" />
     195        <?php _e('Thumbnail'); ?></label>
     196
     197        <label class="imgedit-label">
     198        <input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="nothumb" />
     199        <?php _e('All sizes except thumbnail'); ?></label>
     200    </p>
     201    </div>
     202
     203    <?php } ?>
     204
     205    </td></tr>
     206    </tbody></table>
    86207    <div class="imgedit-wait" id="imgedit-wait-<?php echo $post_id; ?>"></div>
     208    <script type="text/javascript">imageEdit.init(<?php echo $post_id; ?>);</script>
     209    <div class="hidden" id="imgedit-leaving-<?php echo $post_id; ?>"><?php _e("There are unsaved changes that will be lost.  'OK' to continue, 'Cancel' to return to the Image Editor."); ?></div>
    87210    </div>
    88211<?php
     
    110233    }
    111234    if ( is_resource($image) ) {
    112         $image = apply_filters('load_image_to_edit', $image, $post->ID); // allows plugins to remove a watermark
     235        $image = apply_filters('load_image_to_edit', $image, $post->ID);
    113236        if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
    114237            imagealphablending($image, false);
     
    269392}
    270393
    271 
    272394function stream_preview_image($post_id) {
    273395    $post = get_post($post_id);
     
    298420}
    299421
     422function wp_restore_image($post_id) {
     423    $meta = wp_get_attachment_metadata($post_id);
     424    $backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
     425    $restored = false;
     426    $msg = '';
     427
     428    if ( !is_array($backup_sizes) ) {
     429        $msg->error = __('Cannot load image metadata.');
     430        return $msg;
     431    }
     432
     433    $parts = pathinfo($meta['file']);
     434    $suffix = time() . rand(100, 999);
     435    $default_sizes = apply_filters( 'intermediate_image_sizes', array('large', 'medium', 'thumbnail') );
     436    $default_sizes[] = 'full';
     437
     438    foreach ( $default_sizes as $default_size ) {
     439        if ( isset($backup_sizes["$default_size-orig"]) ) {
     440            $data = $backup_sizes["$default_size-orig"];
     441            if ( 'full' == $default_size ) {
     442                $backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $parts['basename']);
     443
     444                $meta['file'] = path_join($parts['dirname'], $data['file']);
     445                $meta['width'] = $data['width'];
     446                $meta['height'] = $data['height'];
     447
     448                list ( $uwidth, $uheight ) = wp_shrink_dimensions($meta['width'], $meta['height']);
     449                $meta['hwstring_small'] = "height='$uheight' width='$uwidth'";
     450                $restored = update_attached_file($post_id, $meta['file']);
     451            } else {
     452                if ( isset($meta['sizes'][$default_size]) )
     453                    $backup_sizes["$default_size-{$suffix}"] = $meta['sizes'][$default_size];
     454
     455                $meta['sizes'][$default_size] = $data;
     456            }
     457        }
     458    }
     459
     460    if ( !wp_update_attachment_metadata($post_id, $meta) || !update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes) ) {
     461        $msg->error = __('Cannot save image metadata.');
     462        return $msg;
     463    }
     464
     465    if ( !$restored )
     466        $msg->error = __('Image metadata is inconsistent.');
     467    else
     468        $msg->msg = __('Image restored successfully.');
     469
     470    return $msg;
     471}
    300472
    301473function wp_save_image($post_id) {
    302     $msg = '';
    303     $success = $delete = $full_resized = false;
     474    $return = '';
     475    $success = $delete = $scaled = $nocrop = false;
    304476    $post = get_post($post_id);
    305477    @ini_set('memory_limit', '256M');
    306478    $img = load_image_to_edit($post);
    307479
    308     if ( !is_resource($img) )
    309         return 'error=' . __('Unable to create new image.');
     480    if ( !is_resource($img) ) {
     481        $return->error = esc_js( __('Unable to create new image.') );
     482        return $return;
     483    }
    310484
    311485    $fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0;
    312486    $fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0;
    313487    $target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : '';
    314 
    315     if ( !empty($_REQUEST['history']) ) {
     488    $scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do'];
     489
     490    if ( $scale && $fwidth > 0 && $fheight > 0 ) {
     491        $sX = imagesx($img);
     492        $sY = imagesy($img);
     493
     494        // check if it has roughly the same w / h ratio
     495        if ( round($sX / $sY, 2) == round($fwidth / $fheight, 2) ) {
     496            // scale the full size image
     497            $dst = wp_imagecreatetruecolor($fwidth, $fheight);
     498            if ( imagecopyresampled( $dst, $img, 0, 0, 0, 0, $fwidth, $fheight, $sX, $sY ) ) {
     499                imagedestroy($img);
     500                $img = $dst;
     501                $scaled = true;
     502            }
     503        }
     504
     505        if ( !$scaled ) {
     506            $return->error = esc_js( __('Error while saving the scaled image. Please reload the page and try again.') );
     507            return $return;
     508        }
     509    } elseif ( !empty($_REQUEST['history']) ) {
    316510        $changes = json_decode( stripslashes($_REQUEST['history']) );
    317511        if ( $changes )
    318512            $img = image_edit_apply_changes($img, $changes);
    319     }
    320 
    321     if ( $fwidth > 0 && $fheight > 0 ) {
    322         // scale the full size image
    323         $dst = wp_imagecreatetruecolor($fwidth, $fheight);
    324         if ( imagecopyresampled( $dst, $img, 0, 0, 0, 0, $fwidth, $fheight, imagesx($img), imagesy($img) ) ) {
    325             imagedestroy($img);
    326             $img = $dst;
    327             $full_resized = true;
    328         }
    329     }
    330 
    331     if ( !$changes && !$full_resized )
    332         return 'error=' . __('Nothing to save, the image is not changed.');
    333 
    334     $meta = wp_get_attachment_metadata($post_id, false, false);
    335     if ( !is_array($meta) )
    336         $meta = array();
    337 
    338     if ( !isset($meta['sizes']) || !is_array($meta['sizes']) )
    339         $meta['sizes'] = array();
     513    } else {
     514        $return->error = esc_js( __('Nothing to save, the image has not changed.') );
     515        return $return;
     516    }
     517
     518    $meta = wp_get_attachment_metadata($post_id);
     519    $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
     520
     521    if ( !is_array($meta) ) {
     522        $return->error = esc_js( __('Image data does not exist. Please re-upload the image.') );
     523        return $return;
     524    }
     525
     526    if ( !is_array($backup_sizes) )
     527        $backup_sizes = array();
    340528
    341529    // generate new filename
     
    357545
    358546    // save the full-size file, also needed to create sub-sizes
    359     if ( !wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id) )
    360         return 'error=' . __('Unable to save the image.');
    361 
    362     if ( 'full' == $target || 'all' == $target || $full_resized ) {
    363         $meta['sizes']["backup-{$suffix}-full"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']);
     547    if ( !wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id) ) {
     548        $return->error = esc_js( __('Unable to save the image.') );
     549        return $return;
     550    }
     551
     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']);
    364555
    365556        $success = update_attached_file($post_id, $new_path);
     
    371562        $meta['hwstring_small'] = "height='$uheight' width='$uwidth'";
    372563
    373         if ( $success && $target == 'all' )
     564        if ( $success && ('nothumb' == $target || 'all' == $target) ) {
    374565            $sizes = apply_filters( 'intermediate_image_sizes', array('large', 'medium', 'thumbnail') );
    375 
    376         $msg .= "full={$meta['width']}x{$meta['height']}!";
    377     } elseif ( array_key_exists($target, $meta['sizes']) ) {
    378         $sizes = array( $target );
    379         $success = $delete = true;
     566            if ( 'nothumb' == $target )
     567                $sizes = array_diff( $sizes, array('thumbnail') );
     568        }
     569
     570        $return->fw = $meta['width'];
     571        $return->fh = $meta['height'];
     572    } elseif ( 'thumbnail' == $target ) {
     573        $sizes = array( 'thumbnail' );
     574        $success = $delete = $nocrop = true;
    380575    }
    381576
    382577    if ( isset($sizes) ) {
    383578        foreach ( $sizes as $size ) {
    384             if ( isset($meta['sizes'][$size]) )
    385                 $meta['sizes']["backup-{$suffix}-$size"] = $meta['sizes'][$size];
    386 
    387             $resized = image_make_intermediate_size($new_path, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") );
     579            if ( isset($meta['sizes'][$size]) ) {
     580                $tag = !isset($backup_sizes["$size-orig"]) ? "$size-orig" : "$size-$suffix";
     581                $backup_sizes[$tag] = $meta['sizes'][$size];
     582            }
     583
     584            $crop = $nocrop ? false : get_option("{$size}_crop");
     585            $resized = image_make_intermediate_size($new_path, get_option("{$size}_size_w"), get_option("{$size}_size_h"), $crop );
    388586
    389587            if ( $resized )
     
    396594    if ( $success ) {
    397595        wp_update_attachment_metadata($post_id, $meta);
    398 
    399         if ( $target == 'thumbnail' || $target == 'all' || ( $target == 'full' && !array_key_exists('thumbnail', $meta['sizes']) ) ) {
    400             if ( $thumb_url = get_attachment_icon_src($post_id) )
    401                 $msg .= "thumbnail={$thumb_url[0]}";
     596        update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes);
     597
     598        if ( $target == 'thumbnail' || $target == 'all' ) {
     599            if ( $thumb = $meta['sizes']['thumbnail'] ) {
     600                $file_url = wp_get_attachment_url($post_id);
     601                $return->thumbnail = path_join( dirname($file_url), $thumb['file'] );
     602            }
    402603        }
    403604    } else {
     
    411612
    412613    imagedestroy($img);
    413     return $msg;
    414 }
    415 
     614
     615    $return->msg = esc_js( __('Image saved') );
     616    return $return;
     617}
     618
Note: See TracChangeset for help on using the changeset viewer.