Remove the extension "-jpg" from the generated file name of the full sized webp version
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:
- Whether the image size is
full - Whether the filter above is
true
Checklist
- [x] PR has either
[Focus]orInfrastructurelabel. - [x] PR has a
[Type]label. - [x] PR has a milestone or the
no milestonelabel.
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: @reteinformatica, @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: reteinformatica, kenny-nt, ddur, jamesozzie.
Co-authored-by: rilwis <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>
Co-authored-by: adamsilverstein <[email protected]>
Co-authored-by: felixarntz <[email protected]>
Co-authored-by: thelovekesh <[email protected]>
Co-authored-by: phanduynam <[email protected]>
Co-authored-by: PaulSchiretz <[email protected]>
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.
@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.
@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.
@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 getimage.webp.
- For example, if you upload
- However, if a file of the same name already exists, add the original extension to prevent a conflict.
- For example, if you upload
image.jpegafter uploadingimage.jpg, you getimage-jpeg.webpin addition toimage.webp.
- For example, if you upload
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).
@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.
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
@mukeshpanchal27 Tests are updated. There's also a test for uploading image.jpeg and image.jpg.
@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.jpegafter uploadingimage.jpg, you'll getimage-1.jpegautomatically. And with our code, it will be converted toimage-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.
@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 Done.
This is confusing because there is no error in the GHA-run tests.
@westonruter what environment are you using for this?
@thelovekesh I'm testing locally with wp-env which is supposed to be the same as on GHA.
Has this issue been resolved?
Has this issue been resolved?
NO.
I think we should focus on solving this problem because it has existed for a long time.
please update avif image format also