Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
30 changes: 30 additions & 0 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,33 @@ function webp_uploads_get_image_sizes_additional_mime_type_support() {

return $allowed_sizes;
}

/**
* Updates the quality of images used in the wp editor.
* For WP version less than 6.2 returns 82,
* Otherwise returns default quality.
*
* @since n.e.x.t
*
* @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_set_wp_image_editor_quality( $quality, $mime_type ) {
global $wp_version;

$valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms();

// Return early for non image or invalid mime types.
if ( ! isset( $valid_mime_transforms[ $mime_type ] ) ) {
return $quality;
}

if ( version_compare( '6.2', $wp_version, '>=' ) ) {
return $quality;
}

// Default quality set to 82 for all mimes for WP version less than 6.2.
return 82;
}
add_filter( 'wp_editor_set_quality', 'webp_uploads_set_wp_image_editor_quality', 10, 2 );
54 changes: 54 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' );
remove_all_filters( 'wp_editor_set_quality' );

// 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 @@ -801,6 +802,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' );
remove_all_filters( 'wp_editor_set_quality' );

// 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 Down Expand Up @@ -918,11 +920,63 @@ 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 test_set_quality_with_image_conversion() {
global $wp_version;
// Set conversions for uploaded images.
add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
$expected_webp = 82;
if ( version_compare( '6.2', $wp_version ) ) {
$expected_webp = 86;
}

$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.' );

// A PNG image will be converted to WEBP whose quality should be 82 for wp version 6.2 and above.
$editor->save();
$this->assertSame( $expected_webp, $editor->get_quality(), 'Output image format is WEBP. Quality setting for it should be 82 for WP version 6.2 and above.' );

unset( $editor );

$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 for wp version 6.2 and above.
$editor->save();
$this->assertSame( $expected_webp, $editor->get_quality(), 'Output image format is WEBP. Quality setting for it should be 82 for WP version 6.2 and above.' );

unset( $editor );
remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
}

/**
* 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' );
}

/**
* Changes the output format when editing images. PNG and JPEG files
* will be converted to WEBP (if the image editor in PHP supports it).
*
* @param array $formats Output formats.
*
* @return array
*/
public function image_editor_output_formats( $formats ) {
$formats['image/png'] = 'image/webp';
$formats['image/jpeg'] = 'image/webp';
return $formats;
}
}
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