Make WordPress Core

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

File fullpatch-v6-44867-image-editor-save-modified-image-as-a-new-copy.patch, 2.9 KB (added by drzraf, 2 years ago)

reroll WP 6.0

  • wp-admin/includes/image-edit.php

    diff --git a/wp-admin/includes/image-edit.php b/wp-admin/includes/image-edit.php
    index 4cb0dc23d7..2eaf336673 100644
    a b function wp_image_editor( $post_id, $msg = false ) { 
    243243                        <label for="imgedit-target-thumbnail"><?php _e( 'Thumbnail' ); ?></label>
    244244                </span>
    245245
     246
     247                <label class="imgedit-label">
     248                <input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="new" />
     249                <?php _e('A newly created image'); ?></label>
    246250                <span class="imgedit-label">
    247251                        <input type="radio" id="imgedit-target-nothumb" name="imgedit-target-<?php echo $post_id; ?>" value="nothumb" />
    248252                        <label for="imgedit-target-nothumb"><?php _e( 'All sizes except thumbnail' ); ?></label>
    function wp_save_image( $post_id ) { 
    902906                }
    903907        }
    904908
     909        if ( 'new' == $target ) {
     910                $original_post_id = $post_id;
     911                $post_id = NULL;
     912        }
     913
    905914        // Save the full-size file, also needed to create sub-sizes.
    906915        if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) {
    907916                $return->error = esc_js( __( 'Unable to save the image.' ) );
    function wp_save_image( $post_id ) { 
    947956                $return->fw = $meta['width'];
    948957                $return->fh = $meta['height'];
    949958        } elseif ( 'thumbnail' === $target ) {
     959        } elseif ( 'new' == $target ) {
     960                $size = $img->get_size();
     961                $meta['width'] = $size['width']; // cropped and saved as a copy
     962                $meta['height'] = $size['height'];
     963
     964                $new_post = (array)$post;
     965                unset( $new_post['ID'], $new_post['post_date'], $new_post['post_date_gmt'], $new_post['post_modified'], $new_post['post_modified_gmt'] );
     966                $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';
     967
     968                $wp_upload_dir = wp_upload_dir();
     969                $new_post['guid'] = $wp_upload_dir['url'] . '/' . basename( $new_path );
     970                $post_id = wp_insert_attachment( $new_post, $new_path, 0, TRUE );
     971                if ( is_wp_error( $post_id ) ) {
     972                        $return->error = esc_js( __('Unable to create a new atachment.') );
     973                        return $return;
     974                }
     975                $return->refresh = TRUE;
     976                $return->debug = sprintf( __( 'New attachment created: %d.'), $post_id );
     977                $success = true;
     978
     979                update_attached_file( $post_id, $new_path );
     980                update_post_meta( $post_id, '_wp_attachment_derive_from', $original_post_id );
     981
     982                $sizes = get_intermediate_image_sizes();
     983                $delete = $nocrop = false;
    950984                $sizes   = array( 'thumbnail' );
    951985                $success = true;
    952986                $delete  = true;
  • wp-admin/js/image-edit.js

    diff --git a/wp-admin/js/image-edit.js b/wp-admin/js/image-edit.js
    index 18d3099f62..4fc90544e3 100644
    a b function( response ) { 
    561561                        }
    562562
    563563                        if ( self._view ) {
     564                                if ( ret.refresh ) { /* ToDo: refresh media view when going back. */ }
    564565                                self._view.save();
    565566                        } else {
    566567                                imageEdit.close(postid);