Make WordPress Core

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

File 0001-44867-image-editor-save-modified-image-as-a-new-copy.patch, 3.6 KB (added by drzraf, 6 years ago)

patch v1

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

    From f3335b02c80046d11574dceaf5cd5fedf7d8be72 Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?Rapha=C3=ABl=20Droz?= <raphael.droz+floss@gmail.com>
    Date: Thu, 30 Aug 2018 11:33:39 -0300
    Subject: [PATCH] #44867 image editor: save modified image as a new copy
    
    ---
     wp-admin/includes/image-edit.php | 32 +++++++++++++++++++++++++++++++-
     wp-admin/js/image-edit.js        |  1 +
     2 files changed, 32 insertions(+), 1 deletion(-)
    
    diff --git a/wp-admin/includes/image-edit.php b/wp-admin/includes/image-edit.php
    index efa9865..773f3b9 100644
    a b function wp_image_editor($post_id, $msg = false) { 
    171171                <label class="imgedit-label">
    172172                <input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="nothumb" />
    173173                <?php _e('All sizes except thumbnail'); ?></label>
     174
     175                <label class="imgedit-label">
     176                <input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="new" />
     177                <?php _e('A newly created image'); ?></label>
    174178        </fieldset>
    175179        </div>
    176180        </div>
    function wp_save_image( $post_id ) { 
    792796                }
    793797        }
    794798
     799        if ( 'new' == $target ) {
     800                $original_post_id = $post_id;
     801                $post_id = NULL;
     802        }
     803
    795804        // Save the full-size file, also needed to create sub-sizes.
    796805        if ( !wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id) ) {
    797806                $return->error = esc_js( __('Unable to save the image.') );
    function wp_save_image( $post_id ) { 
    830839        } elseif ( 'thumbnail' == $target ) {
    831840                $sizes = array( 'thumbnail' );
    832841                $success = $delete = $nocrop = true;
     842        } elseif ( 'new' == $target ) {
     843                $new_post = (array)$post;
     844                unset( $new_post['ID'], $new_post['post_date'], $new_post['post_date_gmt'], $new_post['post_modified'], $new_post['post_modified_gmt'] );
     845                $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';
     846
     847                $wp_upload_dir = wp_upload_dir();
     848                $new_post['guid'] = $wp_upload_dir['url'] . '/' . basename( $new_path );
     849                $post_id = wp_insert_attachment( $new_post, $new_path, 0, TRUE );
     850                if ( is_wp_error( $post_id ) ) {
     851                        $return->error = esc_js( __('Unable to create a new atachment.') );
     852                        return $return;
     853                }
     854                $return->refresh = TRUE;
     855                $return->debug = sprintf( __( 'New attachment created: %d.'), $post_id );
     856                $success = true;
     857
     858                update_attached_file( $post_id, $new_path );
     859                update_post_meta( $post_id, '_wp_attachment_derive_from', $original_post_id );
     860
     861                $sizes = get_intermediate_image_sizes();
     862                $delete = $nocrop = false;
    833863        }
    834864
    835865        /*
    function wp_save_image( $post_id ) { 
    886916                wp_update_attachment_metadata( $post_id, $meta );
    887917                update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes);
    888918
    889                 if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' ) {
     919                if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' || $target == 'new' ) {
    890920                        // Check if it's an image edit from attachment edit screen
    891921                        if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' == $_REQUEST['context'] ) {
    892922                                $thumb_url = wp_get_attachment_image_src( $post_id, array( 900, 600 ), true );
  • wp-admin/js/image-edit.js

    diff --git a/wp-admin/js/image-edit.js b/wp-admin/js/image-edit.js
    index 14af25e..7c23631 100644
    a b function(r) { 
    496496                        }
    497497
    498498                        if ( self._view ) {
     499                                if ( ret.refresh ) { /* ToDo: refresh media view when going back. */ }
    499500                                self._view.save();
    500501                        } else {
    501502                                imageEdit.close(postid);