Skip to content
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
75bf9d1
#563 - set wp image editor quality for mime types according to wp ver…
mehulkaklotar Nov 1, 2022
a2c34d0
Merge branch 'trunk' into enhancement/563-image-editor-quality
mehulkaklotar Nov 1, 2022
52b8ede
#563 - fix older tests to accommodate new changes
mehulkaklotar Nov 1, 2022
59a3b4c
Merge branch 'trunk' into enhancement/563-image-editor-quality
mehulkaklotar Nov 1, 2022
0ddc6d9
Merge remote-tracking branch 'origin/enhancement/563-image-editor-qua…
mehulkaklotar Nov 1, 2022
b4e342d
Merge branch 'trunk' into enhancement/563-image-editor-quality
mehulkaklotar Nov 7, 2022
3625323
Merge branch 'trunk' into enhancement/563-image-editor-quality
mehulkaklotar Nov 8, 2022
c3b625f
return early for non image mimes and other fixes
mehulkaklotar Nov 8, 2022
264ae36
unit test added to check filter adds the quality or not
mehulkaklotar Nov 8, 2022
80c5381
fixes for wp version added
mehulkaklotar Nov 8, 2022
e05b4ec
fixes for wp version added tests
mehulkaklotar Nov 8, 2022
0df8f1a
Merge branch 'trunk' into enhancement/563-image-editor-quality
mehulkaklotar Nov 8, 2022
f4f022f
fixes for wp version added tests
mehulkaklotar Nov 8, 2022
8cd2490
fix for 6.1 and lower for webp and universal quality added
mehulkaklotar Nov 9, 2022
eeace67
fix test for 6.1 and lower for webp and universal quality added
mehulkaklotar Nov 9, 2022
929ba07
Update modules/images/webp-uploads/load.php
mehulkaklotar Nov 9, 2022
4af5bc9
Update tests/modules/images/webp-uploads/load-tests.php
mehulkaklotar Nov 9, 2022
039d484
fix tests for the images with quality
mehulkaklotar Nov 9, 2022
07917e3
Update modules/images/webp-uploads/load.php
mehulkaklotar Nov 10, 2022
0d75cfb
Update modules/images/webp-uploads/load.php
mehulkaklotar Nov 10, 2022
8d3bcc7
Update modules/images/webp-uploads/load.php
mehulkaklotar Nov 10, 2022
7cfcd72
Update modules/images/webp-uploads/load.php
mehulkaklotar Nov 10, 2022
58b5e9b
Update modules/images/webp-uploads/load.php
mehulkaklotar Nov 10, 2022
af18717
fix tests
mehulkaklotar Nov 11, 2022
7dd81d7
fix tests
mehulkaklotar Nov 15, 2022
aab1208
Merge branch 'trunk' into enhancement/563-image-editor-quality
mehulkaklotar Nov 15, 2022
0fb1e19
fix tests
mehulkaklotar Nov 16, 2022
b1f3778
Merge remote-tracking branch 'origin/enhancement/563-image-editor-qua…
mehulkaklotar Nov 16, 2022
d3f97eb
Revert "fix tests"
mehulkaklotar Nov 16, 2022
f268b54
fix tests with tear down method
mehulkaklotar Nov 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,30 @@ function webp_uploads_get_image_sizes_additional_mime_type_support() {

return $allowed_sizes;
}

/**
* Updates the quality of WebP image sizes generated by WordPress to 82.
*
* Prior to WordPress 6.1, the MIME type was not correctly provided to this filter, so for those versions this function
* simply returns 82 universally (since all other MIME types already use 82 anyway).
*
* @since n.e.x.t
*
* @global string $wp_version The WordPress version string.
*
* @param int $quality Quality level between 1 (low) and 100 (high).
* @param string $mime_type Image mime type.
* @return int The updated quality for mime types.
*/
function webp_uploads_modify_webp_quality( $quality, $mime_type ) {
global $wp_version;

// Below WP 6.1 or for WebP images, always return 82 (other MIME types were already using 82 by default anyway).
if ( version_compare( $wp_version, '6.1', '<' ) || 'image/webp' === $mime_type ) {
return 82;
}

// Return default quality for non-WebP images in WP 6.1+.
return $quality;
}
add_filter( 'wp_editor_set_quality', 'webp_uploads_modify_webp_quality', 10, 2 );
61 changes: 61 additions & 0 deletions tests/modules/images/webp-uploads/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ public function it_should_create_webp_for_full_size_which_is_smaller_in_webp_for
$this->opt_in_to_jpeg_and_webp();

add_filter( 'webp_uploads_discard_larger_generated_images', '__return_true' );
add_filter( 'wp_editor_set_quality', array( $this, 'return_webp_image_quality' ), PHP_INT_MAX, 2 );

// Look for an image that contains only full size mime type images.
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' );
Expand All @@ -789,6 +790,8 @@ public function it_should_create_webp_for_full_size_which_is_smaller_in_webp_for
$this->assertImageNotHasSizeSource( $attachment_id, $size, 'image/webp' );
}
$this->assertNotSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );

remove_filter( 'wp_editor_set_quality', array( $this, 'return_webp_image_quality' ), PHP_INT_MAX );
}

/**
Expand All @@ -801,6 +804,7 @@ public function it_should_create_webp_for_some_sizes_which_are_smaller_in_webp_f
$this->opt_in_to_jpeg_and_webp();

add_filter( 'webp_uploads_discard_larger_generated_images', '__return_true' );
add_filter( 'wp_editor_set_quality', array( $this, 'return_webp_image_quality' ), PHP_INT_MAX, 2 );

// Look for an image that contains all of the additional mime type images.
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/balloons.webp' );
Expand All @@ -821,6 +825,8 @@ public function it_should_create_webp_for_some_sizes_which_are_smaller_in_webp_f

$this->assertNotSame( $tag, $updated_tag );
$this->assertSame( $expected_tag, $updated_tag );

remove_filter( 'wp_editor_set_quality', array( $this, 'return_webp_image_quality' ), PHP_INT_MAX );
}

/**
Expand Down Expand Up @@ -918,11 +924,66 @@ public function it_should_create_mime_types_for_allowed_sizes_only_via_global_va
$this->assertImageNotHasSizeSource( $attachment_id, 'not_allowed_size_200x150', 'image/webp' );
}

/**
* Test image quality for image conversion.
*
* @test
*/
public function it_should_set_quality_with_image_conversion() {
$editor = wp_get_image_editor( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/dice.png', array( 'mime_type' => 'image/png' ) );

// Quality setting for the source image. For PNG the fallback default of 82 is used.
$this->assertSame( 82, $editor->get_quality(), 'Default quality setting for PNG is 82.' );

// Temporary file.
$file = wp_tempnam();

// A PNG image will be converted to WebP whose quality should be 82 universally.
$editor->save( $file, 'image/webp' );
$this->assertSame( 82, $editor->get_quality(), 'Output image format is WebP. Quality setting for it should be 82 universally.' );

$editor = wp_get_image_editor( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' );

// Quality setting for the source image. For JPG the fallback default of 82 is used.
$this->assertSame( 82, $editor->get_quality(), 'Default quality setting for JPG is 82.' );

// A JPG image will be converted to WebP whose quality should be 82 universally.
$editor->save( $file, 'image/webp' );
$this->assertSame( 82, $editor->get_quality(), 'Output image format is WebP. Quality setting for it should be 82 universally.' );

// Delete the temporary file.
unlink( $file );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code will not run if any of the above assertion fails. See my comment above though, I think we should not use a temporary file anyway, so I think the code here should be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my comment above about test files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mehulkaklotar I understand now why you're using wp_tempnam(), that makes sense. However, the comment here still needs to be addressed: If an assertion fails, this line is never reached, which means it would leave the file in place.

We need to change this as follows:

  • Assign the results from any method calls from assertions here to variables (before the unlink call).
  • Run all assertions only after the unlink call.

This way, when an assertion fails, at least the file was still deleted as usual.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felixarntz Another alternative could be to store the file name in a property and clean it up in a tear down.

Example class
class TestCase { // could also be implemented as a trait

	protected $to_unlink = [];
	
	public function test_something_with_a_file() {
		$filename = $this->temp_filename();
		
		file_put_contents( $filename, 'foobar' );
		
		// $filename will always be deleted after the test
	}

	protected function temp_filename() {
		$filename = wp_tempnam();

		$this->to_unlink[] = $filename;

		return $filename;
	}

	public function tear_down() {
		$this->to_unlink = array_filter(
			$this->to_unlink,
			function ( $filename ) {
				return unlink( $filename );
			}
		);
	}

}

}

/**
* Test webp_uploads_modify_webp_quality function for image quality.
*
* @covers ::webp_uploads_modify_webp_quality()
*
* @test
*/
public function it_should_return_correct_quality_for_mime_types() {
$this->assertSame( 82, webp_uploads_modify_webp_quality( 90, 'image/webp' ), 'WebP image quality should always be 82.' );
$this->assertSame( 82, webp_uploads_modify_webp_quality( 82, 'image/webp' ), 'WebP image quality should always be 82.' );
$this->assertSame( 80, webp_uploads_modify_webp_quality( 80, 'image/jpeg' ), 'JPEG image quality should return default quality provided from WP filter wp_editor_set_quality.' );
$this->assertNotSame( 50, webp_uploads_modify_webp_quality( 40, 'image/jpeg' ), 'JPEG image quality should return default quality provided from WP filter wp_editor_set_quality.' );
}

/**
* Runs (empty) hooks to satisfy webp_uploads_in_frontend_body() conditions.
*/
private function mock_frontend_body_hooks() {
remove_all_actions( 'template_redirect' );
do_action( 'template_redirect' );
}

/**
* Return WebP image quality 86 for testing.
*/
public function return_webp_image_quality( $quality, $mime_type ) {
if ( 'image/webp' === $mime_type ) {
return 86;
}
return $quality;
}
}
2 changes: 1 addition & 1 deletion tests/utils/TestCase/DominantColorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function provider_get_dominant_color() {
),
'green_jpg' => array(
'image_path' => TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/dominant-color/green.jpg',
'expected_color' => array( '00ff00', '00ff01' ),
'expected_color' => array( '00ff00', '00ff01', '02ff01' ),
'expected_transparency' => false,
),
'white_jpg' => array(
Expand Down