WordPress.org

Make WordPress Core

Ticket #44867: fullpatch-v5-44867-image-editor-save-modified-image-as-a-new-copy.patch

File fullpatch-v5-44867-image-editor-save-modified-image-as-a-new-copy.patch, 2.8 KB (added by drzraf, 11 months ago)
  • wp-admin/includes/image-edit.php

    diff --git a/wp-admin/includes/image-edit.php b/wp-admin/includes/image-edit.php
    index 87108d2c17..2003dab761 100644
    a b function wp_image_editor( $post_id, $msg = false ) { 
    181181                        <label for="imgedit-target-thumbnail"><?php _e( 'Thumbnail' ); ?></label>
    182182                </span>
    183183
     184
     185                <label class="imgedit-label">
     186                <input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="new" />
     187                <?php _e('A newly created image'); ?></label>
    184188                <span class="imgedit-label">
    185189                        <input type="radio" id="imgedit-target-nothumb" name="imgedit-target-<?php echo $post_id; ?>" value="nothumb" />
    186190                        <label for="imgedit-target-nothumb"><?php _e( 'All sizes except thumbnail' ); ?></label>
    function wp_save_image( $post_id ) { 
    847851                }
    848852        }
    849853
     854        if ( 'new' == $target ) {
     855                $original_post_id = $post_id;
     856                $post_id = NULL;
     857        }
     858
    850859        // Save the full-size file, also needed to create sub-sizes.
    851860        if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) {
    852861                $return->error = esc_js( __( 'Unable to save the image.' ) );
    function wp_save_image( $post_id ) { 
    892901                $success = true;
    893902                $delete  = true;
    894903                $nocrop  = true;
     904        } elseif ( 'new' == $target ) {
     905                $size = $img->get_size();
     906                $meta['width'] = $size['width']; // cropped and saved as a copy
     907                $meta['height'] = $size['height'];
     908
     909                $new_post = (array)$post;
     910                unset( $new_post['ID'], $new_post['post_date'], $new_post['post_date_gmt'], $new_post['post_modified'], $new_post['post_modified_gmt'] );
     911                $new_post['post_title'] = preg_match('/\.[a-z]{3}$/', $new_post['post_title']) ? preg_replace('/(\.[a-z]{3})/', '-media-copy\1', $new_post['post_title']) : $new_post['post_title'] . '-mediacopy';
     912
     913                $wp_upload_dir = wp_upload_dir();
     914                $new_post['guid'] = $wp_upload_dir['url'] . '/' . basename( $new_path );
     915                $post_id = wp_insert_attachment( $new_post, $new_path, 0, TRUE );
     916                if ( is_wp_error( $post_id ) ) {
     917                        $return->error = esc_js( __('Unable to create a new atachment.') );
     918                        return $return;
     919                }
     920                $return->refresh = TRUE;
     921                $return->debug = sprintf( __( 'New attachment created: %d.'), $post_id );
     922                $success = true;
     923
     924                update_attached_file( $post_id, $new_path );
     925                update_post_meta( $post_id, '_wp_attachment_derive_from', $original_post_id );
     926
     927                $sizes = get_intermediate_image_sizes();
     928                $delete = $nocrop = false;
    895929        }
    896930
    897931        /*
  • wp-admin/js/image-edit.js

    diff --git a/wp-admin/js/image-edit.js b/wp-admin/js/image-edit.js
    index 756d2e4da1..0c7e85b0bd 100644
    a b function(r) { 
    521521                        }
    522522
    523523                        if ( self._view ) {
     524                                if ( ret.refresh ) { /* ToDo: refresh media view when going back. */ }
    524525                                self._view.save();
    525526                        } else {
    526527                                imageEdit.close(postid);