Make WordPress Core

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

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

patch v2

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

    diff --git a/wp-admin/includes/image-edit.php b/wp-admin/includes/image-edit.php
    index efa9865..c026876 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                $size = $img->get_size();
     844                $meta['width'] = $size['width']; // cropped and saved as a copy
     845                $meta['height'] = $size['height'];
     846
     847                $new_post = (array)$post;
     848                unset( $new_post['ID'], $new_post['post_date'], $new_post['post_date_gmt'], $new_post['post_modified'], $new_post['post_modified_gmt'] );
     849                $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';
     850
     851                $wp_upload_dir = wp_upload_dir();
     852                $new_post['guid'] = $wp_upload_dir['url'] . '/' . basename( $new_path );
     853                $post_id = wp_insert_attachment( $new_post, $new_path, 0, TRUE );
     854                if ( is_wp_error( $post_id ) ) {
     855                        $return->error = esc_js( __('Unable to create a new atachment.') );
     856                        return $return;
     857                }
     858                $return->refresh = TRUE;
     859                $return->debug = sprintf( __( 'New attachment created: %d.'), $post_id );
     860                $success = true;
     861
     862                update_attached_file( $post_id, $new_path );
     863                update_post_meta( $post_id, '_wp_attachment_derive_from', $original_post_id );
     864
     865                $sizes = get_intermediate_image_sizes();
     866                $delete = $nocrop = false;
    833867        }
    834868
    835869        /*
    function wp_save_image( $post_id ) { 
    886920                wp_update_attachment_metadata( $post_id, $meta );
    887921                update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes);
    888922
    889                 if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' ) {
     923                if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' || $target == 'new' ) {
    890924                        // Check if it's an image edit from attachment edit screen
    891925                        if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' == $_REQUEST['context'] ) {
    892926                                $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);