-
Notifications
You must be signed in to change notification settings - Fork 130
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
Conversation
…lity' into enhancement/563-image-editor-quality
There was a problem hiding this 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.
There was a problem hiding this 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
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
unlink( $file ); | ||
unset( $editor ); |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
unlink( $file ); | ||
unset( $editor ); |
There was a problem hiding this comment.
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.
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>
There was a problem hiding this 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 ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 );
}
);
}
}
There was a problem hiding this 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 ); |
There was a problem hiding this comment.
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.
…lity' into enhancement/563-image-editor-quality
This reverts commit 0fb1e19.
@felixarntz I have used the tear down method instead to unlink the file now. Please have a look. |
There was a problem hiding this 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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
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
[Focus]
orInfrastructure
label.[Type]
label.no milestone
label.