WordPress.org

Make WordPress Core

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

File fullpatch-v4-44867-image-editor-save-modified-image-as-a-new-copy.patch, 2.8 KB (added by drzraf, 3 months ago)

v4 compatible with 5.3.2

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

    diff --git a/wp-admin/includes/image-edit.php b/wp-admin/includes/image-edit.php
    index 4605aa4..a719adc 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 ) { 
    852856                $post_id = NULL;
    853857        }
    854858
     859        if ( 'new' == $target ) {
     860                $original_post_id = $post_id;
     861                $post_id = NULL;
     862        }
     863
    855864        // Save the full-size file, also needed to create sub-sizes.
    856865        if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) {
    857866                $return->error = esc_js( __( 'Unable to save the image.' ) );
    function wp_save_image( $post_id ) { 
    897906                $success = true;
    898907                $delete  = true;
    899908                $nocrop  = true;
     909        } elseif ( 'new' == $target ) {
     910                $size = $img->get_size();
     911                $meta['width'] = $size['width']; // cropped and saved as a copy
     912                $meta['height'] = $size['height'];
     913
     914                $new_post = (array)$post;
     915                unset( $new_post['ID'], $new_post['post_date'], $new_post['post_date_gmt'], $new_post['post_modified'], $new_post['post_modified_gmt'] );
     916                $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';
     917
     918                $wp_upload_dir = wp_upload_dir();
     919                $new_post['guid'] = $wp_upload_dir['url'] . '/' . basename( $new_path );
     920                $post_id = wp_insert_attachment( $new_post, $new_path, 0, TRUE );
     921                if ( is_wp_error( $post_id ) ) {
     922                        $return->error = esc_js( __('Unable to create a new atachment.') );
     923                        return $return;
     924                }
     925                $return->refresh = TRUE;
     926                $return->debug = sprintf( __( 'New attachment created: %d.'), $post_id );
     927                $success = true;
     928
     929                update_attached_file( $post_id, $new_path );
     930                update_post_meta( $post_id, '_wp_attachment_derive_from', $original_post_id );
     931
     932                $sizes = get_intermediate_image_sizes();
     933                $delete = $nocrop = false;
    900934        }
    901935
    902936        /*
  • wp-admin/js/image-edit.js

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