From a6894d0b59e231598db32622c0d04c09c66dd45b Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 16 May 2012 17:47:55 +0000 Subject: [PATCH] Create a new attachment and make a copy of the image when selecting an image from the image library. This prevents orphaning the header if the original attachment is deleted. This also prevents stomping of meta. Add a button to skip cropping. Props SergeyBiryukov Fixes #20657 #20667 git-svn-id: http://core.svn.wordpress.org/trunk@20806 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/custom-header.php | 21 ++++++--- wp-admin/includes/image.php | 85 +++++++++++++++++++++++++++++-------- 2 files changed, 83 insertions(+), 23 deletions(-) diff --git a/wp-admin/custom-header.php b/wp-admin/custom-header.php index a3d1360070..862d1023ab 100644 --- a/wp-admin/custom-header.php +++ b/wp-admin/custom-header.php @@ -784,11 +784,15 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?> - + - +

@@ -867,12 +871,17 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?> else $dst_width = get_theme_support( 'custom-header', 'width' ); - $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height ); + if ( empty( $_POST['skip-cropping'] ) ) + $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height ); + elseif ( ! empty( $_POST['create-new-attachment'] ) ) + $cropped = _copy_image_file( $attachment_id ); + else + $cropped = get_attached_file( $attachment_id ); + if ( ! $cropped || is_wp_error( $cropped ) ) wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id); // For replication - $is_cropped = ( get_attached_file( $attachment_id ) != $cropped ); $parent = get_post($attachment_id); $parent_url = $parent->guid; @@ -890,7 +899,7 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?> 'guid' => $url, 'context' => 'custom-header' ); - if ( ! empty( $_POST['new-attachment'] ) ) + if ( ! empty( $_POST['create-new-attachment'] ) ) unset( $object['ID'] ); // Update the attachment @@ -913,7 +922,7 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?> $medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original ); if ( file_exists( $medium ) ) @unlink( apply_filters( 'wp_delete_file', $medium ) ); - if ( empty( $_POST['new-attachment'] ) && $is_cropped ) + if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) @unlink( apply_filters( 'wp_delete_file', $original ) ); return $this->finished(); diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php index 5c90e2b892..006fd4eb55 100644 --- a/wp-admin/includes/image.php +++ b/wp-admin/includes/image.php @@ -44,9 +44,6 @@ function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) { * @return string|WP_Error|false New filepath on success, WP_Error or false on failure. */ function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) { - if ( 0 == $src_x && 0 == $src_y && $src_w == $dst_w && $src_h == $dst_h ) - return ( is_numeric( $src ) ) ? get_attached_file( $src ) : $src; - if ( is_numeric( $src ) ) { // Handle int as attachment ID $src_file = get_attached_file( $src ); if ( ! file_exists( $src_file ) ) { @@ -366,19 +363,19 @@ function file_is_displayable_image($path) { return apply_filters('file_is_displayable_image', $result, $path); } -function load_image_to_edit($post_id, $mime_type, $size = 'full') { - $filepath = get_attached_file($post_id); - - if ( $filepath && file_exists($filepath) ) { - if ( 'full' != $size && ( $data = image_get_intermediate_size($post_id, $size) ) ) { - $filepath = apply_filters('load_image_to_edit_filesystempath', path_join( dirname($filepath), $data['file'] ), $post_id, $size); - } - } elseif ( function_exists('fopen') && function_exists('ini_get') && true == ini_get('allow_url_fopen') ) { - $filepath = apply_filters('load_image_to_edit_attachmenturl', wp_get_attachment_url($post_id) , $post_id, $size); - } - - $filepath = apply_filters('load_image_to_edit_path', $filepath, $post_id, $size); - if ( empty($filepath) ) +/** + * Load an image resource for editing. + * + * @since 2.9.0 + * + * @param string $attachment_id Attachment ID. + * @param string $mime_type Image mime type. + * @param string $size Optional. Image size, defaults to 'full'. + * @return resource|false The resulting image resource on success, false on failure. + */ +function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { + $filepath = _load_image_to_edit_path( $attachment_id, $size ); + if ( empty( $filepath ) ) return false; switch ( $mime_type ) { @@ -396,7 +393,7 @@ function load_image_to_edit($post_id, $mime_type, $size = 'full') { break; } if ( is_resource($image) ) { - $image = apply_filters('load_image_to_edit', $image, $post_id, $size); + $image = apply_filters('load_image_to_edit', $image, $attachment_id, $size); if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) { imagealphablending($image, false); imagesavealpha($image, true); @@ -404,3 +401,57 @@ function load_image_to_edit($post_id, $mime_type, $size = 'full') { } return $image; } + +/** + * Retrieve the path or url of an attachemnt's attached file. + * + * If the attached file is not present on the local filesystem (usually due to replication plugins), + * then the url of the file is returned if url fopen is supported. + * + * @since 3.4.0 + * @access private + * + * @param string $attachment_id Attachment ID. + * @param string $size Optional. Image size, defaults to 'full'. + * @return string|false File path or url on success, false on failure. + */ +function _load_image_to_edit_path( $attachment_id, $size = 'full' ) { + $filepath = get_attached_file( $attachment_id ); + + if ( $filepath && file_exists( $filepath ) ) { + if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) { + $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size ); + } + } elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) { + $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size ); + } + + return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size ); +} + +/** + * Copy an existing image file. + * + * @since 3.4.0 + * @access private + * + * @param string $attachment_id Attachment ID. + * @return string|false New file path on success, false on failure. + */ +function _copy_image_file( $attachment_id ) { + debug_log( 'copyin' ); + $dst_file = $src_file = get_attached_file( $attachment_id ); + if ( ! file_exists( $src_file ) ) + $src_file = _load_image_to_edit_path( $attachment_id ); + + if ( $src_file ) { + $dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file ); + $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); + if ( ! @copy( $src_file, $dst_file ) ) + $dst_file = false; + } else { + $dst_file = false; + } + + return $dst_file; +}