WordPress.org

Make WordPress Core

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

File fullpatch-v3-44867-image-editor-save-modified-image-as-a-new-copy.patch, 3.3 KB (added by drzraf, 7 months ago)

updated for WP5

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

    diff --git a/wp-admin/includes/image-edit.php b/wp-admin/includes/image-edit.php
    index 740405d..186dde8 100644
    a b function wp_image_editor( $post_id, $msg = false ) { 
    181181                <label class="imgedit-label">
    182182                <input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="nothumb" />
    183183                <?php _e( 'All sizes except thumbnail' ); ?></label>
     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        </fieldset>
    185189        </div>
    186190        </div>
    function wp_save_image( $post_id ) { 
    835839                }
    836840        }
    837841
     842        if ( 'new' == $target ) {
     843                $original_post_id = $post_id;
     844                $post_id = NULL;
     845        }
     846
    838847        // Save the full-size file, also needed to create sub-sizes.
    839848        if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) {
    840849                $return->error = esc_js( __( 'Unable to save the image.' ) );
    function wp_save_image( $post_id ) { 
    878887        } elseif ( 'thumbnail' == $target ) {
    879888                $sizes   = array( 'thumbnail' );
    880889                $success = $delete = $nocrop = true;
     890        } elseif ( 'new' == $target ) {
     891                $size = $img->get_size();
     892                $meta['width'] = $size['width']; // cropped and saved as a copy
     893                $meta['height'] = $size['height'];
     894
     895                $new_post = (array)$post;
     896                unset( $new_post['ID'], $new_post['post_date'], $new_post['post_date_gmt'], $new_post['post_modified'], $new_post['post_modified_gmt'] );
     897                $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';
     898
     899                $wp_upload_dir = wp_upload_dir();
     900                $new_post['guid'] = $wp_upload_dir['url'] . '/' . basename( $new_path );
     901                $post_id = wp_insert_attachment( $new_post, $new_path, 0, TRUE );
     902                if ( is_wp_error( $post_id ) ) {
     903                        $return->error = esc_js( __('Unable to create a new atachment.') );
     904                        return $return;
     905                }
     906                $return->refresh = TRUE;
     907                $return->debug = sprintf( __( 'New attachment created: %d.'), $post_id );
     908                $success = true;
     909
     910                update_attached_file( $post_id, $new_path );
     911                update_post_meta( $post_id, '_wp_attachment_derive_from', $original_post_id );
     912
     913                $sizes = get_intermediate_image_sizes();
     914                $delete = $nocrop = false;
    881915        }
    882916
    883917        /*
    function wp_save_image( $post_id ) { 
    940974                wp_update_attachment_metadata( $post_id, $meta );
    941975                update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes );
    942976
    943                 if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' ) {
     977                if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' || $target == 'new' ) {
    944978                        // Check if it's an image edit from attachment edit screen
    945979                        if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' == $_REQUEST['context'] ) {
    946980                                $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 100f26e..d664912 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);