<!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>[59379] trunk: Media: Remove dimension suffix from full size converted HEIC 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/59379">59379</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/59379","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>peterwilsoncc</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-11-10 21:40:00 +0000 (Sun, 10 Nov 2024)</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: Remove dimension suffix from full size converted HEIC images.

Removes the dimension suffix, eg `-1000x1000` from the file name of full size images automatically converted from HEIC to JPEGs by WordPress. Introduces unit tests for the default conversion of images and customized conversion settings via the `image_editor_output_format` filter.

Follow up to <a href="https://core.trac.wordpress.org/changeset/58849">[58849]</a>, <a href="https://core.trac.wordpress.org/changeset/58942">[58942]</a>, <a href="https://core.trac.wordpress.org/changeset/59317">[59317]</a>, <a href="https://core.trac.wordpress.org/changeset/59346">[59346]</a>, <a href="https://core.trac.wordpress.org/changeset/59366">[59366]</a>

Props mukesh27, peterwilsoncc, azaozz, apermo, flixos90, ironprogrammer.
Fixes <a href="https://core.trac.wordpress.org/ticket/62359">#62359</a>.
See <a href="https://core.trac.wordpress.org/ticket/53645">#53645</a>, <a href="https://core.trac.wordpress.org/ticket/62305">#62305</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesimagephp">trunk/src/wp-admin/includes/image.php</a></li>
<li><a href="#trunktestsphpunittestsfunctionsphp">trunk/tests/phpunit/tests/functions.php</a></li>
<li><a href="#trunktestsphpunittestsmediaphp">trunk/tests/phpunit/tests/media.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     2024-11-10 21:34:29 UTC (rev 59378)
+++ trunk/src/wp-admin/includes/image.php       2024-11-10 21:40:00 UTC (rev 59379)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -341,6 +341,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                 */
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( $scale_down ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $saved = $editor->save( $editor->generate_filename( 'scaled' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                } elseif ( $convert ) {
+                                       /*
+                                        * Generate a new file name for the converted image.
+                                        *
+                                        * As the image file name will be unique due to the changed file extension,
+                                        * it does not need a suffix to be unique. However, the generate_filename method
+                                        * does not allow for an empty suffix, so the "-converted" suffix is required to
+                                        * be added and subsequently removed.
+                                        */
+                                       $converted_file_name = $editor->generate_filename( 'converted' );
+                                       $converted_file_name = preg_replace( '/(-converted\.)([a-z0-9]+)$/i', '.$2', $converted_file_name );
+                                       $saved               = $editor->save( $converted_file_name );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $saved = $editor->save();
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span></span></pre></div>
<a id="trunktestsphpunittestsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/functions.php   2024-11-10 21:34:29 UTC (rev 59378)
+++ trunk/tests/phpunit/tests/functions.php     2024-11-10 21:40:00 UTC (rev 59379)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1564,10 +1564,10 @@
</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">                $expected = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        50,
-                       50,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 1180,
+                       1180,
</ins><span class="cx" style="display: block; padding: 0 10px">                         IMAGETYPE_HEIC,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'width="50" height="50"',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'width="1180" height="1180"',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'mime' => 'image/heic',
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">                $result   = wp_getimagesize( $file );
</span></span></pre></div>
<a id="trunktestsphpunittestsmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media.php       2024-11-10 21:34:29 UTC (rev 59378)
+++ trunk/tests/phpunit/tests/media.php 2024-11-10 21:40:00 UTC (rev 59379)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6446,6 +6446,105 @@
</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">+         * Ensure an HEIC image is converted to a JPEG.
+        *
+        * @ticket 62305
+        * @ticket 62359
+        *
+        * @dataProvider data_image_converted_to_other_format_has_correct_filename
+        *
+        * @param bool $apply_big_image_size_threshold True if filter needs to apply, otherwise false.
+        */
+       public function test_heic_image_upload_is_converted_to_jpeg( bool $apply_big_image_size_threshold ) {
+               $temp_dir      = get_temp_dir();
+               $file          = $temp_dir . '/test-image.heic';
+               $scaled_suffix = $apply_big_image_size_threshold ? '-scaled' : '';
+               copy( DIR_TESTDATA . '/images/test-image.heic', $file );
+
+               $editor = wp_get_image_editor( $file );
+
+               // Skip if the editor does not support HEIC.
+               if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/heic' ) ) {
+                       $this->markTestSkipped( 'HEIC is not supported by the selected image editor.' );
+               }
+
+               $attachment_id = self::factory()->attachment->create_object(
+                       array(
+                               'post_mime_type' => 'image/heic',
+                               'file'           => $file,
+                       )
+               );
+
+               if ( $apply_big_image_size_threshold ) {
+                       add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
+               }
+
+               $image_meta = wp_generate_attachment_metadata( $attachment_id, $file );
+
+               $this->assertStringEndsNotWith( '.heic', $image_meta['file'], 'The file extension is expected to change.' );
+               $this->assertSame( "test-image{$scaled_suffix}.jpg", basename( $image_meta['file'] ), "The file name is expected to be test-image{$scaled_suffix}.jpg" );
+               $this->assertSame( 'test-image.heic', $image_meta['original_image'], 'The original image name is expected to be stored in the meta data.' );
+               $this->assertSame( 'image/jpeg', wp_get_image_mime( $image_meta['file'] ), 'The image mime type is expected to be image/jpeg.' );
+       }
+
+       /**
+        * Ensure a JPEG is converted to WebP when applied via a filter.
+        *
+        * @ticket 62305
+        * @ticket 62359
+        *
+        * @dataProvider data_image_converted_to_other_format_has_correct_filename
+        *
+        * @param bool $apply_big_image_size_threshold True if filter needs to apply, otherwise false.
+        */
+       public function test_jpeg_image_converts_to_webp_when_filtered( bool $apply_big_image_size_threshold ) {
+               $temp_dir      = get_temp_dir();
+               $file          = $temp_dir . '/33772.jpg';
+               $scaled_suffix = $apply_big_image_size_threshold ? '-scaled' : '';
+               copy( DIR_TESTDATA . '/images/33772.jpg', $file );
+
+               $editor = wp_get_image_editor( $file );
+
+               // Skip if the editor does not support WebP.
+               if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/webp' ) ) {
+                       $this->markTestSkipped( 'WebP is not supported by the selected image editor.' );
+               }
+
+               $attachment_id = self::factory()->attachment->create_object(
+                       array(
+                               'post_mime_type' => 'image/jpeg',
+                               'file'           => $file,
+                       )
+               );
+
+               if ( $apply_big_image_size_threshold ) {
+                       add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
+               }
+
+               // Generate all sizes as WebP.
+               add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) );
+
+               $image_meta = wp_generate_attachment_metadata( $attachment_id, $file );
+
+               $this->assertStringEndsNotWith( '.jpg', $image_meta['file'], 'The file extension is expected to change.' );
+               $this->assertSame( "33772{$scaled_suffix}.webp", basename( $image_meta['file'] ), "The file name is expected to be 33772{$scaled_suffix}.webp." );
+               $this->assertSame( '33772.jpg', $image_meta['original_image'], 'The original image name is expected to be stored in the meta data.' );
+               $this->assertSame( 'image/webp', wp_get_image_mime( $image_meta['file'] ), 'The image mime type is expected to be image/webp.' );
+       }
+
+       /**
+        * Data provider for test_image_converted_to_other_format_has_correct_filename().
+        *
+        * @return array[]
+        */
+       public function data_image_converted_to_other_format_has_correct_filename() {
+               return array(
+                       'do not scale image' => array( false ),
+                       'scale image'        => array( true ),
+               );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Helper method to keep track of the last context returned by the 'wp_get_attachment_image_context' filter.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * The method parameter is passed by reference and therefore will always contain the last context value.
</span></span></pre>
</div>
</div>

</body>
</html>