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

Issue fork webp-3281606

Command icon 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

ovenko created an issue. See original summary.

pedrop’s picture

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

nathan tsai’s picture

Ran into this problem this week.

Referenced this issue in #3186383: Access Denied on WebP images with Image Optimize API

immaculatexavier made their first commit to this issue’s fork.

ni.pineau@gmail.com’s picture

Status: Active » Needs review
StatusFileSize
new420 bytes

We 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

luksak’s picture

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

DuaelFr made their first commit to this issue’s fork.

duaelfr’s picture

Version: 8.x-1.0-beta6 » 8.x-1.x-dev
Related issues: +#3328040: Review webp filename logic

I 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.
frouco’s picture

The 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

scott_euser’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, 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.

xavier.masson made their first commit to this issue’s fork.

xavier.masson changed the visibility of the branch 3281606-rendering-duplicate-images-and-dont-create-derivative to hidden.

xavier.masson changed the visibility of the branch 3281606-rendering-duplicate-images-and-dont-create-derivative to active.

jrglasgow’s picture

this fixed it for me

pookmish’s picture

StatusFileSize
new7.01 KB
new751 bytes

Using 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.php

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

mandclu’s picture

Status: Reviewed & tested by the community » Needs review

Moving this back to NR for the latest patch. Does anyone have feedback on the performance concern raised in #10?

berliner’s picture

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

porchlight made their first commit to this issue’s fork.

dagmar’s picture

Status: Needs review » Reviewed & tested by the community

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

beerendlauwers’s picture

Status: Reviewed & tested by the community » Needs work

The patch also needs to include a change to the webp_flush_webp_derivatives function in webp.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_derivatives to this makes it consistent again:

$derivative_webp_uri = \Drupal::service('webp.webp')->getWebpDestination($derivative_uri, '@directory@filename.@extension.webp');

pookmish’s picture

Status: Needs work » Needs review

I've updated the image style derivative flush.

brandonlira’s picture

StatusFileSize
new7.96 KB

This 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!

scott_euser’s picture

Status: Needs review » Reviewed & tested by the community

This continues to work well for us, would be great if this one can get merged in, Thank you!

stefan.korn made their first commit to this issue’s fork.

stefan.korn’s picture

should 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:

$image_uri = preg_replace('/\.[^.]*\.webp$/', '.webp', $image_uri);

It fixes an issue with this line if direct .webp upload is allowed:

$image_uri = preg_replace('/\.webp$/', '', $image_uri);

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.

scott_euser changed the visibility of the branch 3281606-rendering-duplicate-images to hidden.

scott_euser’s picture

scott_euser’s picture

Assigned: Unassigned » scott_euser
Status: Reviewed & tested by the community » Needs work

I 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)

scott_euser’s picture

Assigned: scott_euser » Unassigned
Status: Needs work » Needs review

Tried 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