Skip to content
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

Set the default mapping for jpeg->webp. #35

Conversation

adamsilverstein
Copy link
Owner

@adamsilverstein adamsilverstein commented Aug 31, 2022

eg. cat.jpeg, cat.jpg and cat.jpe (all valid jpegs) generate the same file names before this change, eg. cat-150x150.webp. adding the original file’s extension as a suffix on the generated filename neatly avoids this.
Copy link

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

@adamsilverstein My main feedback here is what I mentioned earlier, I think it would be more efficient to handle the output format in a separate function hooked into the filter.

In addition to that, we should then also add tests to this PR:

  • For the new filter callback function.
  • For the re-introduced logic to ensure a unique file name (we can probably reuse a test from the reverted commit for that).

src/wp-includes/class-wp-image-editor.php Outdated Show resolved Hide resolved
@adamsilverstein
Copy link
Owner Author

Added a test for wp_default_image_output_mapping, the name change functionality is covered by changes to test_generate_filename

@@ -67,6 +67,7 @@ public function testCropImageThumbnail() {
*/
public function testImageEditOverwriteConstant() {
define( 'IMAGE_EDIT_OVERWRITE', true );
$files_that_shouldnt_exist = array();
Copy link
Owner Author

Choose a reason for hiding this comment

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

this test was failing with $files_that_shouldnt_exist undefined below (https://github.com/adamsilverstein/wordpress-develop-fork/pull/35/files#diff-7e3d8ea591299762db2be94e68100b267611327378a1e94a8fbe0cc1666538adL106), I'm going to debug why a little further

Copy link

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

@adamsilverstein A bit more feedback about the filter callback function, mostly minor stuff. We should have a bit more test coverage though.

src/wp-admin/includes/media.php Show resolved Hide resolved
src/wp-admin/includes/media.php Outdated Show resolved Hide resolved
src/wp-admin/includes/media.php Outdated Show resolved Hide resolved
tests/phpunit/tests/image/editor.php Show resolved Hide resolved
tests/phpunit/tests/image/editor.php Show resolved Hide resolved
tests/phpunit/tests/media.php Outdated Show resolved Hide resolved
tests/phpunit/tests/media.php Show resolved Hide resolved
tests/phpunit/tests/post/attachments.php Outdated Show resolved Hide resolved
@adamsilverstein
Copy link
Owner Author

I figured out why the testImageEditOverwriteConstant test was failing: a bug in wp_save_image - when the mime type was transformed during save, the meta incorrectly stored the un-transformed file name. this commit fixes it: a1af747 (#35). I removed the change from the test, should pass now.

Copy link

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

@adamsilverstein Almost good to go now, except one tiny comment below, plus more tests like I mentioned in https://github.com/adamsilverstein/wordpress-develop-fork/pull/35/files#r962033794 still need to be added.

src/wp-admin/includes/image-edit.php Show resolved Hide resolved
tests/phpunit/tests/image/editor.php Show resolved Hide resolved
@adamsilverstein
Copy link
Owner Author

Updates:

  • Test that wp_default_image_output_mapping doesn't overwrite existing mappings
  • Test that the image editor default output for JPEGs is WebP
  • Add missing tear_down
  • cleanup

Tests pass locally, rerunning across envs in WordPress#3162

Copy link

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

@adamsilverstein LGTM, except for a few comments on the one test.

tests/phpunit/tests/media.php Outdated Show resolved Hide resolved
tests/phpunit/tests/media.php Outdated Show resolved Hide resolved

if ( $editor->supports_mime_type( 'image/webp' ) ) {
$this->assertSame( 'image/webp', $saved['mime-type'] );
$this->assertSame( 'canola-100x75.webp', $saved['file'] );

Choose a reason for hiding this comment

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

Shouldn't this result in canola-100x75-jpg.webp? Why is that extra original extension suffix missing here? Is that a potential problem we need to fix?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I looked into this a little. I think it is because of the way the test is structured with generate_filename called with no arguments. Our code in this function relies on $extension being passed, which is what happens when core generates subsized images. I'll see if we can improve this and also test the core calls to create the -scaled and -rotated names which may not be adding the additional suffix, but probably should.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Turns out this was happening because of the way the test was structured. By first generating the filename, then passing that to save the code was bypassing the for altered extension - that only happens when you don't pass a filename to save, see https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/class-wp-image-editor-imagick.php#L717-L719

Fixed this in c0367e1 by altering the test slightly, calling save without first generating the filename. Essentially if you pass a filename to save, it uses that filename, that is a requirement for being able to overwrite files.

adamsilverstein and others added 4 commits September 6, 2022 12:11
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
@adamsilverstein
Copy link
Owner Author

Once tests pass I'm going to commit so the feature lands before beta. we can address any edge case issues raised around file naming or add more tests during the beta.

Copy link

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

@adamsilverstein Awesome work!

@adamsilverstein
Copy link
Owner Author

merged, closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants