<!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>[46076] trunk/src: Media: Add handling for "BIG" images.</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 { white-space: pre-line; 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 { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { 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" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/46076">46076</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/46076","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>azaozz</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-09-07 01:06:49 +0000 (Sat, 07 Sep 2019)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Media: Add handling for "BIG" images. When the users upload a big image, typically a photo, scale it down to make it suitable for web use. Then use the scaled image as the "full" size, and keep the originally uploaded image for creating high quality sub-sizes in the future and in case the users want to download it later.

Introduces `wp_get_original_image_path()` that retrieves the path to the originally uploaded image in all cases, and `big_image_size_threshold` filter to set the pixel value above which images will be scaled. The same value is used as max-width and max-height when scaling.

See <a href="https://core.trac.wordpress.org/ticket/47873">#47873</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesimagephp">trunk/src/wp-admin/includes/image.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesimagephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/image.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/image.php     2019-09-06 23:47:05 UTC (rev 46075)
+++ trunk/src/wp-admin/includes/image.php       2019-09-07 01:06:49 UTC (rev 46076)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -136,13 +136,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_update_image_subsizes( $attachment_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">        $image_meta = wp_get_attachment_metadata( $attachment_id );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $image_file = get_attached_file( $attachment_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $image_file = wp_get_original_image_path( $attachment_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                // Previously failed upload?
</span><span class="cx" style="display: block; padding: 0 10px">                // If there is an uploaded file, make all sub-sizes and generate all of the attachment meta.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $image_file ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        return wp_create_image_subsizes( $image_file, array(), $attachment_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 return wp_create_image_subsizes( $image_file, $attachment_id );
</ins><span class="cx" style="display: block; padding: 0 10px">                 } else {
</span><span class="cx" style="display: block; padding: 0 10px">                        return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -167,26 +167,87 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.3.0
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $file          Full path to the image file.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param array  $image_meta    The attachment meta data array.
</del><span class="cx" style="display: block; padding: 0 10px">  * @param int    $attachment_id Attachment Id to process.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return array The attachment metadata with updated `sizes` array. Includes an array of errors encountered while resizing.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return array The image attachment meta data.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_create_image_subsizes( $file, $image_meta, $attachment_id ) {
-       if ( empty( $image_meta ) || ! isset( $image_meta['width'], $image_meta['height'] ) ) {
-               // New uploaded image.
-               $imagesize            = @getimagesize( $file );
-               $image_meta['width']  = $imagesize[0];
-               $image_meta['height'] = $imagesize[1];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_create_image_subsizes( $file, $attachment_id ) {
+       $imagesize = @getimagesize( $file );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Make the file path relative to the upload dir.
-               $image_meta['file'] = _wp_relative_upload_path( $file );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $imagesize ) ) {
+               // File is not an image.
+               return array();
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Fetch additional metadata from EXIF/IPTC.
-               $exif_meta = wp_read_image_metadata( $file );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Default image meta
+       $image_meta = array(
+               'width'  => $imagesize[0],
+               'height' => $imagesize[1],
+               'file'   => _wp_relative_upload_path( $file ),
+               'sizes'  => array(),
+       );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( $exif_meta ) {
-                       $image_meta['image_meta'] = $exif_meta;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Fetch additional metadata from EXIF/IPTC.
+       $exif_meta = wp_read_image_metadata( $file );
+
+       if ( $exif_meta ) {
+               $image_meta['image_meta'] = $exif_meta;
+       }
+
+       /**
+        * Filters the "BIG image" threshold value.
+        *
+        * If the original image width or height is above the threshold, it will be scaled down. The threshold is
+        * used as max width and max height. The scaled down image will be used as the largest available size, including
+        * the `_wp_attached_file` post meta value.
+        *
+        * Returning `false` from the filter callback will disable the scaling.
+        *
+        * @since 5.3.0
+        *
+        * @param array  $imagesize     Indexed array of the image width and height (in that order).
+        * @param string $file          Full path to the uploaded image file.
+        * @param int    $attachment_id Attachment post ID.
+        */
+       $threshold = (int) apply_filters( 'big_image_size_threshold', 2560, $imagesize, $file, $attachment_id );
+
+       // If the original image's dimensions are over the threshold, scale the image
+       // and use it as the "full" size.
+       if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) {
+               $editor = wp_get_image_editor( $file );
+
+               if ( is_wp_error( $editor ) ) {
+                       // This image cannot be edited.
+                       return $image_meta;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               // Resize the image
+               $resized = $editor->resize( $threshold, $threshold );
+
+               if ( ! is_wp_error( $resized ) ) {
+                       // TODO: EXIF rotate here.
+                       // By default the editor will append `{width}x{height}` to the file name of the resized image.
+                       // Better to append the threshold size instead so the image file name would be like "my-image-2560.jpg"
+                       // and not look like a "regular" sub-size.
+                       // This doesn't affect the sub-sizes names as they are generated from the original image (for best quality).
+                       $saved = $editor->save( $editor->generate_filename( $threshold ) );
+
+                       if ( ! is_wp_error( $saved ) ) {
+                               $new_file = $saved['path'];
+
+                               // Update the attached file meta.
+                               update_attached_file( $attachment_id, $new_file );
+
+                               // Width and height of the new image.
+                               $image_meta['width']  = $saved['width'];
+                               $image_meta['height'] = $saved['height'];
+
+                               // Make the file path relative to the upload dir.
+                               $image_meta['file'] = _wp_relative_upload_path( $new_file );
+
+                               // Store the original image file name in image_meta.
+                               $image_meta['original_image'] = wp_basename( $file );
+                       }
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $new_sizes = wp_get_registered_image_subsizes();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -223,6 +284,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @return array The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
+               // Not an image attachment.
+               return array();
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Check if any of the new sizes already exist.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( isset( $image_meta['sizes'] ) && is_array( $image_meta['sizes'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $image_meta['sizes'] as $size_name => $size_meta ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -237,74 +303,80 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $image_meta['sizes'] = array();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! empty( $new_sizes ) ) {
-               // Sort the image sub-sizes in order of priority when creating them.
-               // This ensures there is an appropriate sub-size the user can access immediately
-               // even when there was an error and not all sub-sizes were created.
-               $priority = array(
-                       'medium'       => null,
-                       'large'        => null,
-                       'thumbnail'    => null,
-                       'medium_large' => null,
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $new_sizes ) ) {
+               // Nothing to do...
+               return $image_meta;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $new_sizes = array_filter( array_merge( $priority, $new_sizes ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Sort the image sub-sizes in order of priority when creating them.
+       // This ensures there is an appropriate sub-size the user can access immediately
+       // even when there was an error and not all sub-sizes were created.
+       $priority = array(
+               'medium'       => null,
+               'large'        => null,
+               'thumbnail'    => null,
+               'medium_large' => null,
+       );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $editor = wp_get_image_editor( $file );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $new_sizes = array_filter( array_merge( $priority, $new_sizes ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! is_wp_error( $editor ) ) {
-                       if ( method_exists( $editor, 'make_subsize' ) ) {
-                               foreach ( $new_sizes as $new_size_name => $new_size_data ) {
-                                       $new_size_meta = $editor->make_subsize( $new_size_data );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $editor = wp_get_image_editor( $file );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        if ( is_wp_error( $new_size_meta ) ) {
-                                               $error_code = $new_size_meta->get_error_code();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_wp_error( $editor ) ) {
+               // The image cannot be edited.
+               return $image_meta;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                if ( $error_code === 'error_getting_dimensions' ) {
-                                                       // Ignore errors when `image_resize_dimensions()` returns false.
-                                                       // They mean that the requested size is larger than the original image and should be skipped.
-                                                       continue;
-                                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( method_exists( $editor, 'make_subsize' ) ) {
+               foreach ( $new_sizes as $new_size_name => $new_size_data ) {
+                       $new_size_meta = $editor->make_subsize( $new_size_data );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                if ( empty( $image_meta['subsize_errors'] ) ) {
-                                                       $image_meta['subsize_errors'] = array();
-                                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( is_wp_error( $new_size_meta ) ) {
+                               $error_code = $new_size_meta->get_error_code();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                $error = array(
-                                                       'error_code'    => $error_code,
-                                                       'error_message' => $new_size_meta->get_error_message(),
-                                               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( $error_code === 'error_getting_dimensions' ) {
+                                       // Ignore errors when `image_resize_dimensions()` returns false.
+                                       // They mean that the requested size is larger than the original image and should be skipped.
+                                       continue;
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                // Store the error code and error message for displaying in the UI.
-                                               $image_meta['subsize_errors'][ $new_size_name ] = $error;
-                                       } else {
-                                               // The sub-size was created successfully.
-                                               // Clear out previous errors in creating this subsize.
-                                               if ( ! empty( $image_meta['subsize_errors'][ $new_size_name ] ) ) {
-                                                       unset( $image_meta['subsize_errors'][ $new_size_name ] );
-                                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( empty( $image_meta['subsize_errors'] ) ) {
+                                       $image_meta['subsize_errors'] = array();
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                if ( empty( $image_meta['subsize_errors'] ) ) {
-                                                       unset( $image_meta['subsize_errors'] );
-                                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $error = array(
+                                       'error_code'    => $error_code,
+                                       'error_message' => $new_size_meta->get_error_message(),
+                               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                // Save the size meta value.
-                                               $image_meta['sizes'][ $new_size_name ] = $new_size_meta;
-                                       }
-
-                                       wp_update_attachment_metadata( $attachment_id, $image_meta );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         // Store the error code and error message for displaying in the UI.
+                               $image_meta['subsize_errors'][ $new_size_name ] = $error;
+                       } else {
+                               // The sub-size was created successfully.
+                               // Clear out previous errors in creating this subsize.
+                               if ( ! empty( $image_meta['subsize_errors'][ $new_size_name ] ) ) {
+                                       unset( $image_meta['subsize_errors'][ $new_size_name ] );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        } else {
-                               // Fall back to `$editor->multi_resize()`.
-                               $created_sizes = $editor->multi_resize( $new_sizes );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( ! empty( $created_sizes ) ) {
-                                       $image_meta['sizes'] = array_merge( $image_meta['sizes'], $created_sizes );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( empty( $image_meta['subsize_errors'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                         unset( $image_meta['subsize_errors'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        wp_update_attachment_metadata( $attachment_id, $image_meta );
</del><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               // Save the size meta value.
+                               $image_meta['sizes'][ $new_size_name ] = $new_size_meta;
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       wp_update_attachment_metadata( $attachment_id, $image_meta );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        } else {
+               // Fall back to `$editor->multi_resize()`.
+               $created_sizes = $editor->multi_resize( $new_sizes );
+
+               if ( ! empty( $created_sizes ) ) {
+                       $image_meta['sizes'] = array_merge( $image_meta['sizes'], $created_sizes );
+                       unset( $image_meta['subsize_errors'] );
+                       wp_update_attachment_metadata( $attachment_id, $image_meta );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        return $image_meta;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -328,7 +400,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( preg_match( '!^image/!', $mime_type ) && file_is_displayable_image( $file ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                // Make thumbnails and other intermediate sizes.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $metadata = wp_create_image_subsizes( $file, $metadata, $attachment_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $metadata = wp_create_image_subsizes( $file, $attachment_id );
</ins><span class="cx" style="display: block; padding: 0 10px">         } elseif ( wp_attachment_is( 'video', $attachment ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $metadata = wp_read_video_metadata( $file );
</span><span class="cx" style="display: block; padding: 0 10px">                $support  = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -899,3 +971,40 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        return $dst_file;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Retrieves the path to an uploaded image.
+ *
+ * Similar to `get_attached_file()` however some images may have been
+ * processed after uploading to make them "web ready".
+ * In this case this function returns the path to the originally uploaded image file.
+ *
+ * @since 5.3.0
+ *
+ * @param int $attachment_id Attachment ID.
+ * @return string|false Path to the original image file or false if the attachment is not an image.
+ */
+function wp_get_original_image_path( $attachment_id ) {
+       if ( ! wp_attachment_is_image( $attachment_id ) ) {
+               return false;
+       }
+
+       $image_meta = wp_get_attachment_metadata( $attachment_id );
+       $image_file = get_attached_file( $attachment_id );
+
+       if ( empty( $image_meta['original_image'] ) ) {
+               $original_image = $image_file;
+       } else {
+               $original_image = path_join( dirname( $image_file ), $image_meta['original_image'] );
+       }
+
+       /**
+        * Filters the path to the original image.
+        *
+        * @since 5.3.0
+        *
+        * @param string $original_image Path to original image file.
+        * @param int    $attachment_id  Attachment ID.
+        */
+       return apply_filters( 'wp_get_original_image_path', $original_image, $attachment_id );
+}
</ins></span></pre></div>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php    2019-09-06 23:47:05 UTC (rev 46075)
+++ trunk/src/wp-includes/post.php      2019-09-07 01:06:49 UTC (rev 46076)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5638,6 +5638,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Don't delete the thumb if another attachment uses it.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id ) ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $thumbfile = str_replace( wp_basename( $file ), $meta['thumb'], $file );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( ! empty( $thumbfile ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
</span><span class="cx" style="display: block; padding: 0 10px">                                $thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5652,8 +5653,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">        // Remove intermediate and backup images if there are any.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                 foreach ( $meta['sizes'] as $size => $sizeinfo ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( ! empty( $intermediate_file ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5664,10 +5667,28 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( ! empty( $meta['original_image'] ) ) {
+               if ( empty( $intermediate_dir ) ) {
+                       $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
+               }
+
+               $original_image = str_replace( wp_basename( $file ), $meta['original_image'], $file );
+
+               if ( ! empty( $original_image ) ) {
+                       $original_image = path_join( $uploadpath['basedir'], $original_image );
+
+                       if ( ! wp_delete_file_from_directory( $original_image, $intermediate_dir ) ) {
+                               $deleted = false;
+                       }
+               }
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( is_array( $backup_sizes ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                 foreach ( $backup_sizes as $size ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( ! empty( $del_file ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $del_file = path_join( $uploadpath['basedir'], $del_file );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre>
</div>
</div>

</body>
</html>