Problem/Motivation
We found this problem on a news portal where the redactors work with a huge amount of images. It comes up in the case when the content manager wants to upload two images with the same name but with different file extensions through an image upload field allowing to upload multiple images. E.g. image.jpg and image.jpeg (or .png etc.).
In this case the final result is that after saving a node the same image appears twice, but in reality there should be two different images.
The problem is that after uploading the first image the WebP module creates the image derivative with .webp extension. Then when the second image is uploaded with the same name, the module doesn’t create a second derivative image with a different name in the background. This causes Drupal to render the same image (the first uploaded image) for both images.
Instead it would be necessary to differentiate the images somehow in the background and render the right images in the front-end.
Steps to reproduce
Take one image with the following name and extension: image.jpg
Take second different image with the following name and extension: image.jpeg
Upload the image.jpg to the system through an image field. After that upload the second image image.jpeg and save the node. Check the rendered images. The first uploaded image will be shown duplicately.
Proposed resolution
The module should handle this situation in the background by keeping the original extension and adding .webp to the end like this:
image.jpg.webp
image.jpeg.webp
| Comment | File | Size | Author |
|---|
Issue fork webp-3281606
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
pedrop commentedWe found a related issue and patch, but that did not work for us: https://www.drupal.org/project/webp/issues/3189967. As its title focuses on another problem we decided to create this separate issue for proper extension handling.
Comment #3
nathan tsai commentedRan into this problem this week.
Referenced this issue in #3186383: Access Denied on WebP images with Image Optimize API
Comment #5
ni.pineau@gmail.com commentedWe faced this issue also.
Tried to propose a (rather naive) patch that seems to fix in our case.
We just changed webp uri to include original image file extension.
I guess it's not that simple but I do not have enough time to investigate further, hope someone with better knowledge could go through it
Comment #6
luksakThere is an issue regarding the compatibility between stage_file_proxy and webp, because stage_file_proxy doesn't know the original file extension and therefore doesn't know the file name of the original image. Adding the original extension to the file names would fix that issue and so the patch in #5 already does so.
Comment #9
duaelfrI merged #5 and some cleaned up code from #3328040: Review webp filename logic into a MR.
I don't know if this could be shipped in this major release or if it would be considered as a BC breaker. Maintainers will choose.
I'm closing #3328040: Review webp filename logic as duplicate, please credit @berliner for their work.
Comment #10
frouco commentedThe merge request works perfectly for me.
NOTE: Applying the #5 .patch fall into a performance issue. The .webp file is generated on each request due the stored file only has the .webp extension and not both .original.webp
Comment #11
scott_euser commentedThanks, the merge request changes in https://git.drupalcode.org/project/webp/-/merge_requests/30 solved it for us as well for multiple images with the same name, but different extensions, naming the images like image1.png.webp and image1.jpg.webp keeping them unique.
Comment #16
jrglasgow commentedthis fixed it for me
Comment #17
pookmish commentedUsing the MR33 I got a warning when the image is first uploaded:
Undefined variable $webp_wanted in Drupal\webp\Controller\ImageStyleDownloadController->deliver() (line 192 of modules/contrib/webp/src/Controller/ImageStyleDownloadController.phpI think it would be better to break out the "if" statement just a little. Since I'm faster with patch files at the moment, I've provided one with an interdiff.
Comment #18
mandclu commentedMoving this back to NR for the latest patch. Does anyone have feedback on the performance concern raised in #10?
Comment #19
berliner commentedThe performance concern raised in #10 was about the patch in #5, which only changed the urls inside the srcset without also adapting the logic in ImageStyleDownloadController, so I don't see how that would still be an issue with the current patch.
Comment #21
dagmarWe replicated the problem in one of our sites. A QA analyst verified the patch of Merge request 33 fixes the problem. I reviewed the code and looks good, concerns in #17 are addressed by #19 and #20.
Comment #22
beerendlauwers commentedThe patch also needs to include a change to the
webp_flush_webp_derivativesfunction inwebp.module, which was introduced by https://www.drupal.org/project/webp/issues/3153137.Currently, the derivative webp uri is generated like this:
$derivative_webp_uri = preg_replace('/\.(png|jpg|jpeg)$/i', '.webp', $derivative_uri);This corresponds to the code
$destination = $this->getWebpDestination($uri, '@directory@filename.webp');Because this patch changes the above to
$destination = $this->getWebpDestination($uri, '@directory@filename.@extension.webp');, it also needs to change there.Changing the line in
webp_flush_webp_derivativesto this makes it consistent again:$derivative_webp_uri = \Drupal::service('webp.webp')->getWebpDestination($derivative_uri, '@directory@filename.@extension.webp');Comment #23
pookmish commentedI've updated the image style derivative flush.
Comment #24
brandonlira commentedThis patch replaces the preg_replace() used to locate .webp derivative files with the getWebpDestination() method to ensure consistent file naming and allow proper deletion when the image is updated (e.g., after focal point change).
Cheers!
Comment #25
scott_euser commentedThis continues to work well for us, would be great if this one can get merged in, Thank you!
Comment #27
stefan.kornshould definitely find its way in the module code. MR 33 probably. I added a comment and aligned the service assignment webp_flush_webp_derivatives().
still i am bit unsure about this line:
It fixes an issue with this line if direct .webp upload is allowed:
But from my testing it seems that the whole line is not necessary. In the current approach it would just make a double ending like "png.webp" to ".webp", but seems that never gets triggered. Either it will be already removed with the previous line "Drop the webp extension" or if file_exists (that is image style exists), the deliver callback will not run.
Comment #29
scott_euser commentedComment #30
scott_euser commentedI think that #10 still applies and I agree with #27 as well. Working on it locally + adding further test coverage for #10 in particular (with corresponding fix)
Comment #31
scott_euser commentedTried to stick to the failing tests but there are wider problems with the module itself; suggest we start with merging #3507101: Set up GitLab CI, get the pipeline 'as is' green, then get this in (and there are a few other critical/major issues in the queue).
Bumping this to major as well per https://www.drupal.org/docs/develop/issues/fields-and-other-parts-of-an-... since there is no valid workaround