Skip to content

Change WP Image editor quality for mime types #571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 30 commits into from
Nov 16, 2022

Conversation

mehulkaklotar
Copy link
Member

@mehulkaklotar mehulkaklotar commented Nov 1, 2022

Summary

Fixes #563

Relevant technical choices

For WP 6.1 below, all mime types for images, quality is set to 82
For WP 6.1+, all mime types for images should be default as per this ticket and PR and WebP to 82

Checklist

  • PR has either [Focus] or Infrastructure label.
  • PR has a [Type] label.
  • PR has a milestone or the no milestone label.

@mehulkaklotar mehulkaklotar added [Type] Enhancement A suggestion for improvement of an existing feature [Focus] Images [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) labels Nov 1, 2022
@mehulkaklotar mehulkaklotar added this to the 1.7.0 milestone Nov 1, 2022
@mehulkaklotar mehulkaklotar self-assigned this Nov 1, 2022
@mehulkaklotar mehulkaklotar marked this pull request as ready for review November 8, 2022 14:41
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@mehulkaklotar Unfortunately it looks like this PR does not address the issue correctly. Please see my comments below.

Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

@mehulkaklotar Left some minor feedbacks

Comment on lines 940 to 941
unlink( $file );
unset( $editor );

Choose a reason for hiding this comment

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

Cleanup like this should happen in a tearDown method as it will not run if an assertion fails before it as part of the test body. These references can be stored in a property that is processed later in the tear down method. You might also consider splitting into separate test methods if such cleanup is needed before each "inner test".

Copy link
Member

Choose a reason for hiding this comment

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

Why is this unsetting of variables even needed? They are just rewritten right below. It seems to me that these two lines could simply be removed.

Choose a reason for hiding this comment

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

The unset for $file shouldn't be needed, but my guess regarding the $editor was to invoke \WP_Image_Editor_Imagick::__destruct or another classes' destructor. I'm not sure if this would be called immediately otherwise just by overwriting the variable. Again, I don't think it would be necessary with separate tests but if it were for some reason, it should get a comment to explain that, otherwise it doesn't make much sense 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

unset was not required there. Unlink is still required for temporary file that is being generated with image editor save, but that is also needed at the end of test case. I have removed the in between unset and unlink code now.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@mehulkaklotar This looks good now in terms of production code. Left a few additional suggestions on wording and tests.

Comment on lines 940 to 941
unlink( $file );
unset( $editor );
Copy link
Member

Choose a reason for hiding this comment

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

Why is this unsetting of variables even needed? They are just rewritten right below. It seems to me that these two lines could simply be removed.

mehulkaklotar and others added 6 commits November 10, 2022 14:12
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@mehulkaklotar Left a few follow up comments. Once these have been addressed, this should be good to go.

$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 );
			}
		);
	}

}

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@mehulkaklotar One last thing here, then this should be good to go.

$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.

@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.

@mehulkaklotar
Copy link
Member Author

@felixarntz I have used the tear down method instead to unlink the file now. Please have a look.

@mehulkaklotar mehulkaklotar requested review from felixarntz and removed request for aaemnnosttv November 16, 2022 11:14
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@mehulkaklotar Thank you, LGTM!

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

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

Nice work!

@felixarntz felixarntz merged commit c22ccfc into trunk Nov 16, 2022
@felixarntz felixarntz deleted the enhancement/563-image-editor-quality branch November 16, 2022 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change default WebP quality to 82
6 participants