Skip to content

Commit 0671dfd

Browse files
Media: improve filter to enable setting output quality by image size.
Add a new $size parameter to the wp_editor_set_quality filter. $size is an array with 'width' and 'height' keys. Developers can use this information to set image quality based on the image size. Props adamsilverstein, joemcgill, Mte90, codekraft, birgire, azaozz, sppramodh. Fixes #54648. git-svn-id: https://develop.svn.wordpress.org/trunk@59473 602fd350-edb4-49c9-b593-d223f7449a82
1 parent dfcdeaf commit 0671dfd

File tree

4 files changed

+85
-10
lines changed

4 files changed

+85
-10
lines changed

src/wp-includes/class-wp-image-editor-gd.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ protected function _resize( $max_w, $max_h, $crop = false ) {
221221

222222
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
223223

224+
$this->set_quality(
225+
null,
226+
array(
227+
'width' => $dst_w,
228+
'height' => $dst_h,
229+
)
230+
);
231+
224232
$resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
225233
imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
226234

@@ -568,12 +576,14 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
568576
* Sets Image Compression quality on a 1-100% scale. Handles WebP lossless images.
569577
*
570578
* @since 6.7.0
579+
* @since 6.8.0 The `$dims` parameter was added.
571580
*
572-
* @param int $quality Compression Quality. Range: [1,100]
581+
* @param int $quality Compression Quality. Range: [1,100]
582+
* @param array $dims Optional. Image dimensions array with 'width' and 'height' keys.
573583
* @return true|WP_Error True if set successfully; WP_Error on failure.
574584
*/
575-
public function set_quality( $quality = null ) {
576-
$quality_result = parent::set_quality( $quality );
585+
public function set_quality( $quality = null, $dims = array() ) {
586+
$quality_result = parent::set_quality( $quality, $dims );
577587
if ( is_wp_error( $quality_result ) ) {
578588
return $quality_result;
579589
} else {
@@ -586,7 +596,7 @@ public function set_quality( $quality = null ) {
586596
$webp_info = wp_get_webp_info( $this->file );
587597
if ( ! empty( $webp_info['type'] ) && 'lossless' === $webp_info['type'] ) {
588598
$quality = IMG_WEBP_LOSSLESS;
589-
parent::set_quality( $quality );
599+
parent::set_quality( $quality, $dims );
590600
}
591601
}
592602
} catch ( Exception $e ) {

src/wp-includes/class-wp-image-editor-imagick.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,14 @@ public function load() {
190190
* Sets Image Compression quality on a 1-100% scale.
191191
*
192192
* @since 3.5.0
193+
* @since 6.8.0 The `$dims` parameter was added.
193194
*
194-
* @param int $quality Compression Quality. Range: [1,100]
195+
* @param int $quality Compression Quality. Range: [1,100]
196+
* @param array $dims Optional. Image dimensions array with 'width' and 'height' keys.
195197
* @return true|WP_Error True if set successfully; WP_Error on failure.
196198
*/
197-
public function set_quality( $quality = null ) {
198-
$quality_result = parent::set_quality( $quality );
199+
public function set_quality( $quality = null, $dims = array() ) {
200+
$quality_result = parent::set_quality( $quality, $dims );
199201
if ( is_wp_error( $quality_result ) ) {
200202
return $quality_result;
201203
} else {
@@ -367,6 +369,14 @@ public function resize( $max_w, $max_h, $crop = false ) {
367369
return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h );
368370
}
369371

372+
$this->set_quality(
373+
null,
374+
array(
375+
'width' => $dst_w,
376+
'height' => $dst_h,
377+
)
378+
);
379+
370380
// Execute the resize.
371381
$thumb_result = $this->thumbnail_image( $dst_w, $dst_h );
372382
if ( is_wp_error( $thumb_result ) ) {

src/wp-includes/class-wp-image-editor.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,14 @@ public function get_quality() {
240240
* Sets Image Compression quality on a 1-100% scale.
241241
*
242242
* @since 3.5.0
243+
* @since 6.8.0 The `$dims` parameter was added.
243244
*
244-
* @param int $quality Compression Quality. Range: [1,100]
245+
* @param int $quality Compression Quality. Range: [1,100]
246+
* @param array $dims Optional. Image dimensions array with 'width' and 'height' keys.
245247
* @return true|WP_Error True if set successfully; WP_Error on failure.
248+
246249
*/
247-
public function set_quality( $quality = null ) {
250+
public function set_quality( $quality = null, $dims = array() ) {
248251
// Use the output mime type if present. If not, fall back to the input/initial mime type.
249252
$mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type;
250253
// Get the default quality setting for the mime type.
@@ -260,11 +263,18 @@ public function set_quality( $quality = null ) {
260263
* The WP_Image_Editor::set_quality() method has priority over the filter.
261264
*
262265
* @since 3.5.0
266+
* @since 6.8.0 Added the size parameter.
263267
*
264268
* @param int $quality Quality level between 1 (low) and 100 (high).
265269
* @param string $mime_type Image mime type.
270+
* @param array $size {
271+
* Dimensions of the image.
272+
*
273+
* @type int $width The image width.
274+
* @type int $height The image height.
275+
* }
266276
*/
267-
$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type );
277+
$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type, $dims ? $dims : $this->size );
268278

269279
if ( 'image/jpeg' === $mime_type ) {
270280
/**

tests/phpunit/tests/media.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5433,6 +5433,51 @@ public function test_quality_with_avif_conversion_file_sizes() {
54335433
}
54345434
}
54355435

5436+
/**
5437+
* Test that the `wp_editor_set_quality` filter includes the dimensions in the `$dims` parameter.
5438+
*
5439+
* @ticket 54648
5440+
*/
5441+
public function test_wp_editor_set_quality_includes_dimensions() {
5442+
// Before loading an image, set up the callback filter with the assertions.
5443+
add_filter( 'wp_editor_set_quality', array( $this, 'assert_dimensions_in_wp_editor_set_quality' ), 10, 3 );
5444+
5445+
$temp_dir = get_temp_dir();
5446+
$file = $temp_dir . '/33772.jpg';
5447+
copy( DIR_TESTDATA . '/images/33772.jpg', $file );
5448+
5449+
$editor = wp_get_image_editor( $file );
5450+
5451+
$attachment_id = self::factory()->attachment->create_object(
5452+
array(
5453+
'post_mime_type' => 'image/jpeg',
5454+
'file' => $file,
5455+
)
5456+
);
5457+
5458+
// Generate all sizes.
5459+
wp_generate_attachment_metadata( $attachment_id, $file );
5460+
5461+
// Clean up the filter.
5462+
remove_filter( 'wp_editor_set_quality', array( $this, 'assert_dimensions_in_wp_editor_set_quality' ), 10, 3 );
5463+
}
5464+
5465+
/**
5466+
* Helper callback to assert that the dimensions are included in the `$dims` parameter.
5467+
*
5468+
* @param int $quality The quality level.
5469+
* @param array $dims The dimensions array.
5470+
*/
5471+
public function assert_dimensions_in_wp_editor_set_quality( $quality, $mime_type, $dims ) {
5472+
// Assert that the array has non empty width and height values.
5473+
$this->assertArrayHasKey( 'width', $dims );
5474+
$this->assertArrayHasKey( 'height', $dims );
5475+
$this->assertGreaterThan( 0, $dims['width'] );
5476+
$this->assertGreaterThan( 0, $dims['height'] );
5477+
5478+
return $quality;
5479+
}
5480+
54365481
/**
54375482
* Test that an image size isn't generated if it matches the original image size.
54385483
*

0 commit comments

Comments
 (0)