<!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>[54097] trunk: Media: Generate WebP only for certain registered image sizes.</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/54097">54097</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/54097","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-09-07 21:43:28 +0000 (Wed, 07 Sep 2022)</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: Generate WebP only for certain registered image sizes.
The existing filter `image_editor_output_format` receives an additional parameter `$size_name` which is populated whenever it controls the output format for a specific registered image size to create. Otherwise, it remains empty. In order to achieve this, a low level change has been added in bringing a new `$size_name` class property to the `WP_Image_Editor` base class, which is introduced in a backward compatible way that will not cause conflicts with custom implementations.
This parameter is then used in new logic inside the `wp_default_image_output_mapping()` callback function for the filter, controlling whether `image/jpeg` should map to `image/webp` output or not. By default, this is enabled for all WordPress core image sizes by default, and this list can be modified using a new `wp_image_sizes_with_additional_mime_type_support` filter, e.g. to remove core sizes or add custom sizes.
The customization per image size may be further enhanced by providing a more declarative API via a new parameter on the `add_image_size()` function.
Props eugenemanuilov, flixos90, adamsilverstein, joegrainger.
Fixes <a href="https://core.trac.wordpress.org/ticket/56526">#56526</a>.
See <a href="https://core.trac.wordpress.org/ticket/55443">#55443</a>, <a href="https://core.trac.wordpress.org/ticket/56288">#56288</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesimagephp">trunk/src/wp-admin/includes/image.php</a></li>
<li><a href="#trunksrcwpincludesclasswpimageeditorgdphp">trunk/src/wp-includes/class-wp-image-editor-gd.php</a></li>
<li><a href="#trunksrcwpincludesclasswpimageeditorimagickphp">trunk/src/wp-includes/class-wp-image-editor-imagick.php</a></li>
<li><a href="#trunksrcwpincludesclasswpimageeditorphp">trunk/src/wp-includes/class-wp-image-editor.php</a></li>
<li><a href="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunksrcwpincludesfunctionsphp">trunk/src/wp-includes/functions.php</a></li>
<li><a href="#trunksrcwpincludesmediaphp">trunk/src/wp-includes/media.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 2022-09-07 17:13:43 UTC (rev 54096)
+++ trunk/src/wp-admin/includes/image.php 2022-09-07 21:43:28 UTC (rev 54097)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -451,6 +451,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( method_exists( $editor, 'make_subsize' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $new_sizes as $new_size_name => $new_size_data ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Include size name in the data.
+ $new_size_data['name'] = $new_size_name;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $new_size_meta = $editor->make_subsize( $new_size_data );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $new_size_meta ) ) {
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpimageeditorgdphp"></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/class-wp-image-editor-gd.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-image-editor-gd.php 2022-09-07 17:13:43 UTC (rev 54096)
+++ trunk/src/wp-includes/class-wp-image-editor-gd.php 2022-09-07 21:43:28 UTC (rev 54097)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -227,7 +227,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $sizes {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * An array of image size data arrays.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Associative array of image size names and their data.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Either a height or width must be provided.
</span><span class="cx" style="display: block; padding: 0 10px"> * If one of the two is set to null, the resize will
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -247,6 +247,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $metadata = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $sizes as $size => $size_data ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Include size name in the data.
+ $size_data['name'] = $size;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $meta = $this->make_subsize( $size_data );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_wp_error( $meta ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -261,13 +264,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Create an image sub-size and return the image meta data value for it.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.3.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.1.0 The $sizes parameter may now include a $name key for each entry.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $size_data {
</span><span class="cx" style="display: block; padding: 0 10px"> * Array of size data.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @type int $width The maximum width in pixels.
- * @type int $height The maximum height in pixels.
- * @type bool $crop Whether to crop the image to exact dimensions.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type int $width The maximum width in pixels.
+ * @type int $height The maximum height in pixels.
+ * @type bool $crop Whether to crop the image to exact dimensions.
+ * @type string $name Image size name.
</ins><span class="cx" style="display: block; padding: 0 10px"> * }
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta,
</span><span class="cx" style="display: block; padding: 0 10px"> * WP_Error object on error.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -277,7 +282,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
</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">- $orig_size = $this->size;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $orig_size = $this->size;
+ $orig_size_name = $this->size_name;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $size_data['width'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $size_data['width'] = null;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -291,6 +297,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $size_data['crop'] = false;
</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 ( isset( $size_data['name'] ) ) {
+ $this->update_size_name( $size_data['name'] );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $resized ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -301,6 +311,7 @@
</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"> $this->size = $orig_size;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->size_name = $orig_size_name;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_wp_error( $saved ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $saved['path'] );
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpimageeditorimagickphp"></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/class-wp-image-editor-imagick.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-image-editor-imagick.php 2022-09-07 17:13:43 UTC (rev 54096)
+++ trunk/src/wp-includes/class-wp-image-editor-imagick.php 2022-09-07 21:43:28 UTC (rev 54097)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -438,7 +438,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $sizes {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * An array of image size data arrays.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Associative array of image size names and their data.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Either a height or width must be provided.
</span><span class="cx" style="display: block; padding: 0 10px"> * If one of the two is set to null, the resize will
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -458,6 +458,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $metadata = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $sizes as $size => $size_data ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Include size name in the data.
+ $size_data['name'] = $size;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $meta = $this->make_subsize( $size_data );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_wp_error( $meta ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -472,13 +475,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Create an image sub-size and return the image meta data value for it.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.3.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.1.0 The $sizes parameter may now include a $name key for each entry.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $size_data {
</span><span class="cx" style="display: block; padding: 0 10px"> * Array of size data.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @type int $width The maximum width in pixels.
- * @type int $height The maximum height in pixels.
- * @type bool $crop Whether to crop the image to exact dimensions.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type int $width The maximum width in pixels.
+ * @type int $height The maximum height in pixels.
+ * @type bool $crop Whether to crop the image to exact dimensions.
+ * @type string $name Image size name.
</ins><span class="cx" style="display: block; padding: 0 10px"> * }
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta,
</span><span class="cx" style="display: block; padding: 0 10px"> * WP_Error object on error.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -488,8 +493,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
</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">- $orig_size = $this->size;
- $orig_image = $this->image->getImage();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $orig_size = $this->size;
+ $orig_size_name = $this->size_name;
+ $orig_image = $this->image->getImage();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $size_data['width'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $size_data['width'] = null;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -503,6 +509,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $size_data['crop'] = false;
</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 ( isset( $size_data['name'] ) ) {
+ $this->update_size_name( $size_data['name'] );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $resized = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $resized ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -515,8 +525,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->image = null;
</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">- $this->size = $orig_size;
- $this->image = $orig_image;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->size = $orig_size;
+ $this->size_name = $orig_size_name;
+ $this->image = $orig_image;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_wp_error( $saved ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $saved['path'] );
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpimageeditorphp"></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/class-wp-image-editor.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-image-editor.php 2022-09-07 17:13:43 UTC (rev 54096)
+++ trunk/src/wp-includes/class-wp-image-editor.php 2022-09-07 21:43:28 UTC (rev 54097)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -14,6 +14,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> abstract class WP_Image_Editor {
</span><span class="cx" style="display: block; padding: 0 10px"> protected $file = null;
</span><span class="cx" style="display: block; padding: 0 10px"> protected $size = null;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected $size_name = '';
</ins><span class="cx" style="display: block; padding: 0 10px"> protected $mime_type = null;
</span><span class="cx" style="display: block; padding: 0 10px"> protected $output_mime_type = null;
</span><span class="cx" style="display: block; padding: 0 10px"> protected $default_mime_type = 'image/jpeg';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -117,7 +118,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @abstract
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $sizes {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * An array of image size arrays. Default sizes are 'small', 'medium', 'large'.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Associative array of image size names and their data. Default sizes are 'small', 'medium', 'large'.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @type array ...$0 {
</span><span class="cx" style="display: block; padding: 0 10px"> * @type int $width Image width.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -185,7 +186,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return int[] {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return array {
</ins><span class="cx" style="display: block; padding: 0 10px"> * Dimensions of the image.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @type int $width The image width.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -201,9 +202,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param int $width
- * @param int $height
- * @return true
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $width The image width.
+ * @param int $height The image height.
+ * @return true True on success, false on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function update_size( $width = null, $height = null ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->size = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -214,6 +215,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">+ * Gets the current image size name.
+ *
+ * @since 6.1.0
+ *
+ * @return string Image size name, or empty string if none set.
+ */
+ public function get_size_name() {
+ return $this->size_name;
+ }
+
+ /**
+ * Sets the current image size name.
+ *
+ * @since 6.1.0
+ *
+ * @param string $size_name The image size name.
+ */
+ protected function update_size_name( $size_name ) {
+ $this->size_name = (string) $size_name;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Gets the Image Compression quality on a 1-100% scale.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.0.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -364,6 +387,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @see WP_Image_Editor::get_output_format()
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.8.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.1.0 The $size_name parameter was added.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string[] $output_format {
</span><span class="cx" style="display: block; padding: 0 10px"> * An array of mime type mappings. Maps a source mime type to a new
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -373,8 +397,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * }
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $filename Path to the image.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $mime_type The source image mime type.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $size_name The image size name to create, or empty string if not set.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type, $this->size_name );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $output_format[ $mime_type ] )
</span><span class="cx" style="display: block; padding: 0 10px"> && $this->supports_mime_type( $output_format[ $mime_type ] )
</span></span></pre></div>
<a id="trunksrcwpincludesdefaultfiltersphp"></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/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2022-09-07 17:13:43 UTC (rev 54096)
+++ trunk/src/wp-includes/default-filters.php 2022-09-07 21:43:28 UTC (rev 54097)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -632,7 +632,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping', 10, 4 );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Embeds.
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'rest_api_init', 'wp_oembed_register_route' );
</span></span></pre></div>
<a id="trunksrcwpincludesfunctionsphp"></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/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/functions.php 2022-09-07 17:13:43 UTC (rev 54096)
+++ trunk/src/wp-includes/functions.php 2022-09-07 21:43:28 UTC (rev 54097)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2690,7 +2690,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $is_image ) {
</span><span class="cx" style="display: block; padding: 0 10px"> /** This filter is documented in wp-includes/class-wp-image-editor.php */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output_formats = apply_filters( 'image_editor_output_format', array(), $_dir . $filename, $mime_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $output_formats = apply_filters( 'image_editor_output_format', array(), $_dir . $filename, $mime_type, '' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $alt_types = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $output_formats[ $mime_type ] ) ) {
</span></span></pre></div>
<a id="trunksrcwpincludesmediaphp"></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/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/media.php 2022-09-07 17:13:43 UTC (rev 54096)
+++ trunk/src/wp-includes/media.php 2022-09-07 21:43:28 UTC (rev 54097)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3912,9 +3912,39 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.1.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $output_mapping Map of mime type to output format.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @retun array The adjusted default output mapping.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $filename Path to the image.
+ * @param string $mime_type The source image mime type.
+ * @param string $size_name Optional. The image size name to create, or empty string if not set. Default empty string.
+ * @return array The adjusted default output mapping.
</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_default_image_output_mapping( $output_mapping ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_default_image_output_mapping( $output_mapping, $filename, $mime_type, $size_name = '' ) {
+ // If size name is specified, check whether the size supports additional MIME types like WebP.
+ if ( $size_name ) {
+ // Include only the core sizes that do not rely on add_image_size(). Additional image sizes are opt-in.
+ $enabled_sizes = array(
+ 'thumbnail' => true,
+ 'medium' => true,
+ 'medium_large' => true,
+ 'large' => true,
+ 'post-thumbnail' => true,
+ );
+
+ /**
+ * Filters the sizes that support secondary mime type output. Developers can use this
+ * to control the generation of additional mime type sub-sized images.
+ *
+ * @since 6.1.0
+ *
+ * @param array $enabled_sizes Map of size names and whether they support secondary mime type output.
+ */
+ $enabled_sizes = apply_filters( 'wp_image_sizes_with_additional_mime_type_support', $enabled_sizes );
+
+ // Bail early if the size does not support additional MIME types.
+ if ( empty( $enabled_sizes[ $size_name ] ) ) {
+ return $output_mapping;
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $output_mapping['image/jpeg'] = 'image/webp';
</span><span class="cx" style="display: block; padding: 0 10px"> return $output_mapping;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</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 2022-09-07 17:13:43 UTC (rev 54096)
+++ trunk/tests/phpunit/tests/media.php 2022-09-07 21:43:28 UTC (rev 54097)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3626,7 +3626,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 55443
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_wp_default_image_output_mapping() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $mapping = wp_default_image_output_mapping( array() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', '' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3637,7 +3637,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_wp_default_image_output_mapping_existing() {
</span><span class="cx" style="display: block; padding: 0 10px"> $mapping = array( 'mime/png' => 'mime/webp' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $mapping = wp_default_image_output_mapping( $mapping );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $mapping = wp_default_image_output_mapping( $mapping, 'test.jpg', 'image/jpeg', '' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'mime/png' => 'mime/webp',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3672,6 +3672,78 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'canola-100x75.jpg', $saved['file'] );
</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">+
+ /**
+ * @ticket 56526
+ * @dataProvider data_wp_default_image_output_mapping_size_filter
+ */
+ public function test_wp_default_image_output_mapping_size_filter( $size_name, $filter_callback, $expects_webp ) {
+ remove_all_filters( 'wp_image_sizes_with_additional_mime_type_support' );
+ if ( $filter_callback ) {
+ add_filter( 'wp_image_sizes_with_additional_mime_type_support', $filter_callback );
+ }
+
+ $mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', $size_name );
+ if ( $expects_webp ) {
+ $this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
+ } else {
+ $this->assertSame( array(), $mapping );
+ }
+ }
+
+ public function data_wp_default_image_output_mapping_size_filter() {
+ return array(
+ 'default size thumbnail' => array(
+ 'thumbnail',
+ null,
+ true,
+ ),
+ 'default size medium' => array(
+ 'medium',
+ null,
+ true,
+ ),
+ 'default size medium_large' => array(
+ 'medium_large',
+ null,
+ true,
+ ),
+ 'default size large' => array(
+ 'large',
+ null,
+ true,
+ ),
+ 'default size unset' => array(
+ 'medium',
+ function( $enabled_sizes ) {
+ unset( $enabled_sizes['medium'] );
+ return $enabled_sizes;
+ },
+ false,
+ ),
+ 'default size set to false' => array(
+ 'medium',
+ function( $enabled_sizes ) {
+ $enabled_sizes['medium'] = false;
+ return $enabled_sizes;
+ },
+ false,
+ ),
+ 'custom size' => array(
+ 'custom',
+ null,
+ false,
+ ),
+ 'custom size opted in' => array(
+ 'custom',
+ function( $enabled_sizes ) {
+ $enabled_sizes['custom'] = true;
+ return $enabled_sizes;
+ },
+ true,
+ ),
+ );
+ }
</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"> /**
</span></span></pre>
</div>
</div>
</body>
</html>