<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[20806] trunk/wp-admin: Create a new attachment and make a copy of the image when selecting an image from the image library.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/20806">20806</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2012-05-16 17:47:55 +0000 (Wed, 16 May 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>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 <a href="http://core.trac.wordpress.org/ticket/20657">#20657</a> <a href="http://core.trac.wordpress.org/ticket/20667">#20667</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadmincustomheaderphp">trunk/wp-admin/custom-header.php</a></li>
<li><a href="#trunkwpadminincludesimagephp">trunk/wp-admin/includes/image.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadmincustomheaderphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/custom-header.php (20805 => 20806)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/custom-header.php        2012-05-16 07:58:24 UTC (rev 20805)
+++ trunk/wp-admin/custom-header.php        2012-05-16 17:47:55 UTC (rev 20806)
</span><span class="lines">@@ -784,11 +784,15 @@
</span><span class="cx">         &lt;input type=&quot;hidden&quot; name=&quot;attachment_id&quot; id=&quot;attachment_id&quot; value=&quot;&lt;?php echo esc_attr( $id ); ?&gt;&quot; /&gt;
</span><span class="cx">         &lt;input type=&quot;hidden&quot; name=&quot;oitar&quot; id=&quot;oitar&quot; value=&quot;&lt;?php echo esc_attr( $oitar ); ?&gt;&quot; /&gt;
</span><span class="cx">         &lt;?php if ( empty( $_POST ) &amp;&amp; isset( $_GET['file'] ) ) { ?&gt;
</span><del>-        &lt;input type=&quot;hidden&quot; name=&quot;new-attachment&quot; value=&quot;true&quot; /&gt;
</del><ins>+        &lt;input type=&quot;hidden&quot; name=&quot;create-new-attachment&quot; value=&quot;true&quot; /&gt;
</ins><span class="cx">         &lt;?php } ?&gt;
</span><span class="cx">         &lt;?php wp_nonce_field( 'custom-header-crop-image' ) ?&gt;
</span><span class="cx"> 
</span><del>-        &lt;?php submit_button( __( 'Crop and Publish' ) ); ?&gt;
</del><ins>+        &lt;p class=&quot;submit&quot;&gt;&lt;?php
+        submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false );
+        if ( isset( $oitar ) &amp;&amp; 1 == $oitar )
+                submit_button( __( 'Skip Cropping, Use Image as Is' ), 'primary', 'skip-cropping', false );
+        ?&gt;
</ins><span class="cx">         &lt;/p&gt;
</span><span class="cx"> &lt;/form&gt;
</span><span class="cx"> &lt;/div&gt;
</span><span class="lines">@@ -867,12 +871,17 @@
</span><span class="cx">                 else
</span><span class="cx">                         $dst_width = get_theme_support( 'custom-header', 'width' );
</span><span class="cx"> 
</span><del>-                $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height );
</del><ins>+                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 );
+
</ins><span class="cx">                 if ( ! $cropped || is_wp_error( $cropped ) )
</span><span class="cx">                         wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
</span><span class="cx"> 
</span><span class="cx">                 $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id); // For replication
</span><del>-                $is_cropped = ( get_attached_file( $attachment_id ) != $cropped );
</del><span class="cx"> 
</span><span class="cx">                 $parent = get_post($attachment_id);
</span><span class="cx">                 $parent_url = $parent-&gt;guid;
</span><span class="lines">@@ -890,7 +899,7 @@
</span><span class="cx">                         'guid' =&gt; $url,
</span><span class="cx">                         'context' =&gt; 'custom-header'
</span><span class="cx">                 );
</span><del>-                if ( ! empty( $_POST['new-attachment'] ) )
</del><ins>+                if ( ! empty( $_POST['create-new-attachment'] ) )
</ins><span class="cx">                         unset( $object['ID'] );
</span><span class="cx"> 
</span><span class="cx">                 // Update the attachment
</span><span class="lines">@@ -913,7 +922,7 @@
</span><span class="cx">                 $medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original );
</span><span class="cx">                 if ( file_exists( $medium ) )
</span><span class="cx">                         @unlink( apply_filters( 'wp_delete_file', $medium ) );
</span><del>-                if ( empty( $_POST['new-attachment'] ) &amp;&amp; $is_cropped )
</del><ins>+                if ( empty( $_POST['create-new-attachment'] ) &amp;&amp; empty( $_POST['skip-cropping'] ) )
</ins><span class="cx">                         @unlink( apply_filters( 'wp_delete_file', $original ) );
</span><span class="cx"> 
</span><span class="cx">                 return $this-&gt;finished();
</span></span></pre></div>
<a id="trunkwpadminincludesimagephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/image.php (20805 => 20806)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/image.php        2012-05-16 07:58:24 UTC (rev 20805)
+++ trunk/wp-admin/includes/image.php        2012-05-16 17:47:55 UTC (rev 20806)
</span><span class="lines">@@ -44,9 +44,6 @@
</span><span class="cx">  * @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
</span><span class="cx">  */
</span><span class="cx"> function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
</span><del>-        if ( 0 == $src_x &amp;&amp; 0 == $src_y &amp;&amp; $src_w == $dst_w &amp;&amp; $src_h == $dst_h )
-                return ( is_numeric( $src ) ) ? get_attached_file( $src ) : $src;
-
</del><span class="cx">         if ( is_numeric( $src ) ) { // Handle int as attachment ID
</span><span class="cx">                 $src_file = get_attached_file( $src );
</span><span class="cx">                 if ( ! file_exists( $src_file ) ) {
</span><span class="lines">@@ -366,19 +363,19 @@
</span><span class="cx">         return apply_filters('file_is_displayable_image', $result, $path);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-function load_image_to_edit($post_id, $mime_type, $size = 'full') {
-        $filepath = get_attached_file($post_id);
-
-        if ( $filepath &amp;&amp; file_exists($filepath) ) {
-                if ( 'full' != $size &amp;&amp; ( $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') &amp;&amp; function_exists('ini_get') &amp;&amp; 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) )
</del><ins>+/**
+ * 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 ) )
</ins><span class="cx">                 return false;
</span><span class="cx"> 
</span><span class="cx">         switch ( $mime_type ) {
</span><span class="lines">@@ -396,7 +393,7 @@
</span><span class="cx">                         break;
</span><span class="cx">         }
</span><span class="cx">         if ( is_resource($image) ) {
</span><del>-                $image = apply_filters('load_image_to_edit', $image, $post_id, $size);
</del><ins>+                $image = apply_filters('load_image_to_edit', $image, $attachment_id, $size);
</ins><span class="cx">                 if ( function_exists('imagealphablending') &amp;&amp; function_exists('imagesavealpha') ) {
</span><span class="cx">                         imagealphablending($image, false);
</span><span class="cx">                         imagesavealpha($image, true);
</span><span class="lines">@@ -404,3 +401,57 @@
</span><span class="cx">         }
</span><span class="cx">         return $image;
</span><span class="cx"> }
</span><ins>+
+/**
+ * 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 &amp;&amp; file_exists( $filepath ) ) {
+                if ( 'full' != $size &amp;&amp; ( $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' ) &amp;&amp; function_exists( 'ini_get' ) &amp;&amp; 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;
+}
</ins></span></pre>
</div>
</div>

</body>
</html>