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

Remove the extension "-jpg" from the generated file name of the full sized webp version #1037

Open
wants to merge 13 commits into
base: trunk
Choose a base branch
from

Conversation

rilwis
Copy link

@rilwis rilwis commented Mar 7, 2024

Summary

In #545, a problem when uploading a image.jpg file is that the plugin will create the full sized version of the image with the name image-jpg.webp. This PR changes this behavior by not automatically append -jpg to the file name. The file name is only appended when there's an existing image with the same name.

Fixes #545

Relevant technical choices

Previously, the plugin relies on $editor->generate_filename. But this function always consider a suffix and add -{$suffix} to the file name. There's no way to eliminate this. So I have to create a helper function webp_uploads_generate_filename to handle this.

There is 2 things that this function consider:

  1. Whether the image size is full
  2. Whether the filter above is true

Checklist

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

Copy link

github-actions bot commented Mar 7, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @kenny-nt, @ddur, @jamesozzie.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: kenny-nt, ddur, jamesozzie.

Co-authored-by: rilwis <rilwis@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: mukeshpanchal27 <mukesh27@git.wordpress.org>
Co-authored-by: felixarntz <flixos90@git.wordpress.org>
Co-authored-by: thelovekesh <thelovekesh@git.wordpress.org>
Co-authored-by: PaulSchiretz <paulschiretz@git.wordpress.org>
Co-authored-by: adamsilverstein <adamsilverstein@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@westonruter westonruter added [Focus] Images Issues related to the Images focus area [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Bug An existing feature is broken labels Mar 7, 2024
@westonruter westonruter added this to the PL Plugin 3.0.0 milestone Mar 7, 2024
plugins/webp-uploads/helper.php Outdated Show resolved Hide resolved
plugins/webp-uploads/helper.php Outdated Show resolved Hide resolved
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
@felixarntz
Copy link
Member

@rilwis Thank you for the PR.

However, I'm not sure we should apply this change. The reason the extension is added is to prevent conflicts. This was a conscious decision which actually came in response to bugs reported. For example, if a user uploads image.jpg and image.jpeg, they would both become image.webp and thus cause a conflict.

I realize this is a very specific scenario, but it is a bug that actually happened and was reported. So I'm not sure we should allow removing the original file extension suffix just for better looking file names - unless there is another reason that the suffix is a problem.

@rilwis
Copy link
Author

rilwis commented Mar 8, 2024

@felixarntz Thanks for your comment. This PR doesn't change the default behavior that you described. When people making the change via the filter, they have to do that with a clear intention. So it is the user's problem when they upload images with the same file name like image.jpg and image.jpeg.

More details are discussed in the issue #545 and I think @mukeshpanchal27 can provide more info, because he suggested the filter.

@felixarntz
Copy link
Member

felixarntz commented Mar 8, 2024

@rilwis Having a filter for that seems okay, but I don't find it a great solution. Filters can only be set by developers, but then users need to live with the consequences. If you're the developer and administrator of a site, that makes sense. But many sites are not like that, the people that use the site and the people that develop the site are different.

I think a better solution would be to handle it automatically, in a smart way that only applies the suffix when needed:

  • By default, don't add a suffix for the original file extension.
    • For example, if you upload image.jpg, you get image.webp.
  • However, if a file of the same name already exists, add the original extension to prevent a conflict.
    • For example, if you upload image.jpeg after uploading image.jpg, you get image-jpeg.webp in addition to image.webp.

This way, it just works, no developer intervention required, and satisfies both issues:

  • There is no chance for conflicts.
  • The file names look nicer (unless there's a conflict).

@rilwis
Copy link
Author

rilwis commented Mar 8, 2024

@felixarntz After checking the source code of WordPress, I found that WordPress already has a mechanism to prevent duplicated file names:

https://github.com/WordPress/WordPress/blob/7ae05b77532b5902c0f67a5791abaec45220b7ca/wp-includes/functions.php#L2577

This function checks only the file name without extension and append numbers if neccessary.

So when a user uploads image.jpeg after uploading image.jpg, you'll get image-1.jpeg automatically. And with our code, it will be converted to image-1.webp. I've tested and saw it works that way.

I've updated the code below. Please take a look.

@mukeshpanchal27
Copy link
Member

mukeshpanchal27 commented Mar 11, 2024

Thanks, @rilwis, for working on the PR. I appreciate the way you implemented the changes. However, as @felixarntz mentioned, we should go with the prefix to avoid conflicts. Since the PR generates image names like WP did, I'm happy to proceed with it, but we need to get Felix's feedback on it. Once we agree, we'll need to address the unit tests as some of them are failing now. Thanks! cc @adamsilverstein

@rilwis
Copy link
Author

rilwis commented Mar 11, 2024

@mukeshpanchal27 Tests are updated. There's also a test for uploading image.jpeg and image.jpg.

@felixarntz
Copy link
Member

felixarntz commented Mar 11, 2024

@rilwis

https://github.com/WordPress/WordPress/blob/7ae05b77532b5902c0f67a5791abaec45220b7ca/wp-includes/functions.php#L2577

This function checks only the file name without extension and append numbers if neccessary.

So when a user uploads image.jpeg after uploading image.jpg, you'll get image-1.jpeg automatically. And with our code, it will be converted to image-1.webp. I've tested and saw it works that way.

I'm aware of the code that does this, but as far as I remember, that function doesn't consider image.jpeg and image.jpg the same file name - because they aren't. So it only becomes a problem when you change both of their file extensions to .webp, and that happens in a completely different place in the code which currently doesn't check for duplicate file names.

But maybe I'm missing something. If so, can you point me to where that function detects those files to be the same file name even though their file extensions are different?

When you tested this, did you test it in both configurations? See Settings > Media, it needs to work both when only generating WebP as well as when generating JPEG and WebP.

Copy link
Author

@rilwis rilwis left a comment

Choose a reason for hiding this comment

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

@felixarntz: You're right. I looked at the wrong place. The code I refered above was about checking the image with suffix "-scaled" and rename if it already exists.

I tested again with the "Generate JPEG files in addition to WebP" settings enabled and saw if image.jpeg is uploaded after image.jpg, then they generate the same image.webp.

If the settings "Generate JPEG files in addition to WebP" is disabled (unchecked), then WordPress auto appends -1 to the webp versions.

I've updated the PR with a check for file_exists to rename the file if needed. Please take a look.

@felixarntz felixarntz added the no milestone PRs that do not have a defined milestone for release label Mar 25, 2024
@felixarntz felixarntz removed this from the performance-lab 3.0.0 milestone Mar 25, 2024
@westonruter
Copy link
Member

@rilwis Hi! Could you update the PR description to reflect the latest state of this PR? That will help us get new reviewers so we can get this merged for the next release.

@westonruter westonruter added this to the webp-uploads n.e.x.t milestone May 3, 2024
@westonruter westonruter removed the no milestone PRs that do not have a defined milestone for release label May 3, 2024
@rilwis
Copy link
Author

rilwis commented May 6, 2024

@westonruter Done.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Running unit tests for this locally, I'm getting a single failure:

There was 1 failure:

1) WebP_Uploads_Helper_Tests::it_should_add_original_image_extension_to_the_webp_file_name_to_ensure_it_is_unique with data set #0 ('/var/www/html/wp-content/plug...e.jpeg', '/var/www/html/wp-content/plug...ge.jpg')
Failed asserting that 'image-35-300x300.webp' ends with "-1-300x300.webp".

/var/www/html/wp-content/plugins/performance/tests/plugins/webp-uploads/helper-tests.php:576

This is confusing because there is no error in the GHA-run tests.

@adamsilverstein @thelovekesh This seems somehow related to WebP and/or env issues that I seem to recall you mentioning?

function webp_uploads_generate_filename( WP_Image_Editor $editor, string $file, string $size, string $extension ): string {
$ext = pathinfo( $file, PATHINFO_EXTENSION );
if ( $extension === $ext ) {
return $file;
Copy link
Member

Choose a reason for hiding this comment

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

Could test coverage be added for this?

// Add "-{$ext}" to the filename if an image with the same name already exists.
$file = "{$dir}{$name}{$suffix}.{$extension}";
if ( file_exists( $file ) ) {
$file = "{$dir}{$name}{$suffix}-{$ext}.{$extension}";
Copy link
Member

Choose a reason for hiding this comment

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

Could test coverage be added for this as well?

@thelovekesh
Copy link
Member

This is confusing because there is no error in the GHA-run tests.

@westonruter what environment are you using for this?

@westonruter
Copy link
Member

@thelovekesh I'm testing locally with wp-env which is supposed to be the same as on GHA.

@westonruter westonruter changed the title Add a filter to remove the extension "-jpg" from the generated file name of the full sized webp version Remove the extension "-jpg" from the generated file name of the full sized webp version May 8, 2024
@westonruter westonruter removed this from the webp-uploads n.e.x.t milestone May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Focus] Images Issues related to the Images focus area [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Bug An existing feature is broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow for modification or removal of the -jpg suffix added to webp images, generated from jpg files
5 participants