Skip to content

WebP file overwritten if filename has the same name with extension of jpeg and jpg #358

Closed

Description

@pixel-paul

Bug Description

This may be a misconfiguration issue, but at the moment if a file is uploaded with the same filename but the extension is jpeg or jpg, the corresponding webp file generated is overwritten by the last file uploaded.

Steps to reproduce

  1. Create 2 files, for example, Paul-Test.jpeg and Paul-Test.jpg
  2. Go to Media Gallery
  3. Click on Add New
  4. Upload Paul-Test.jpeg
  5. Check ~/wp-content/uploads/2022/06/Paul-Test.jpeg
  6. Check ~/wp-content/uploads/2022/06/Paul-Test.webp
  7. Upload Paul-Test.jpg
  8. Check ~/wp-content/uploads/2022/06/Paul-Test.jpg
  9. Check ~/wp-content/uploads/2022/06/Paul-Test.webp
  10. The file Paul-Test.webp created from Paul-Test.jpeg is overwritten with the webp generated from Paul-Test.jpg

Additional Context

I have added this to functions.php:

/* Image conversion attributes */
add_filter('webp_uploads_upload_image_mime_transforms', function () {

    return [
        'image/jpeg' =>  [ 'image/jpeg', 'image/webp' ],
        'image/png' =>  [ 'image/png', 'image/webp' ],
        'image/webp' =>  [ 'image/webp', 'image/jpeg' ],
    ];
});
  • PHP Version: 7.4
  • OS: RHEL 8.6
  • Browser: Firefox
  • Plugin Version: 1.1.0

To note, PNGs with the same filename do not overwrite the webp image (an image is not created at all).

Activity

  1. mxbclang commented on Jun 8, 2022

    @mxbclang
    Contributor

    Thanks for reporting this, @pixel-paul! We'll take a look.

  2. mukeshpanchal27 commented on Jun 9, 2022

    @mukeshpanchal27
    Member

    Thanks for reporting the issue.

    I reproduce the same issue in my setup and check the internal process of how it generates the WebP for the images.

    WordPress core store image/jpeg mime type for all three image formats .jpg, .jpeg and jpe.

    The Performance plugin checks the attachment image mime type for validation when it tries to generate a WebP image in the function webp_uploads_create_sources_property() with below code snippets. This function allows .jpg to formate images for the WebP generation because WP stores it as image/jpeg and it will enable this format for WebP.

    $mime_type = get_post_mime_type( $attachment_id );
    if ( ! isset( $valid_mime_transforms[ $mime_type ] ) ) {
    	return $metadata;
    }
    

    WordPress allows the same image name with different image types like testimage.jpg and testimage.jpeg but when it comes to the Performace plugin we get both images with testimage.jpeg so it overrides previous WebP images.

    Check the attached $metadata result array for both types of images.

    JPG image:
    jpg image

    JPEG image:
    jpeg image

    Here we have two options for skipping the duplicate WebP image.

    1. Before generating any new WebP image we have to check if the same file already exists on the folder and then skip the project so it will generate a WebP image for the first image only and the second image will be skipped.

    2. Along with the mime type checking we also check image format if we find .jpg or .jpe will skip it.

  3. mxbclang commented on Jun 9, 2022

    @mxbclang
    Contributor

    Thanks for taking a look, @mukeshpanchal27. @felixarntz Assigning this to you to confirm next steps based upon the options that Mukesh has outlined in his comment.

  4. felixarntz commented on Jun 9, 2022

    @felixarntz
    Member

    Great catch @pixel-paul!

    @mukeshpanchal27 Since this is a bit of an edge-case, I'd argue as a first stop gap to this bug we can go with your suggested approach 1. That way we will miss out on WebP for such uploads, but at least not break anything.

    I would eventually suggest to find a cleaner solution where we can generate WebP for both uploads, e.g. appending something like -2 to the file name or so. But that's more complicated, and given this is not going to happen a ton, I think it doesn't need to be a launch blocker. We definitely need to avoid the overwrite from happening though.

    I think we can move ahead with approach 1 and implement a PR to ensure WebP images are not overwritten in this situation.

  5. removed their assignment
    on Jun 9, 2022
  6. added
    Needs DevAnything that requires development (e.g. a pull request)
    on Jun 9, 2022
  7. added this to the 1.2.0 milestone on Jun 9, 2022
  8. pixel-paul commented on Jun 9, 2022

    @pixel-paul
    Author

    Thanks @bethanylang, @mukeshpanchal27 and @felixarntz for looking into this.

    Adding some context to our issue, we have had 3 reported cases where this bug has happened (since we implemented usage of WebP with this plugin, which was around 3 days ago).

    Daily customer upload averages around 200 - 400 photos, taken with a phone or digital camera - we are occasionally seeing files being uploaded with the same name but different extensions. As an example, we have in the uploads/2022/06 folder:

    1. 263 jpg files uploaded
    2. 726 jpeg files uploaded
    3. 126 png files uploaded
    

    The issue appears to mainly be with users uploading files with names such as:

    1.jpg 1.jpeg 2.jpg 2.jpeg Picture-19.jpg Picture-19.jpeg

    Considering the solutions mentioned above, perhaps another would be where the original file extension is prepended to the WebP file extension. For example:

    Paul-Test.jpeg --> Paul-Test.jpeg.webp
    Paul-Test.jpg --> Paul-Test.jpg.webp

    This would also work in the case of PNGs which have the same name but different content:

    Paul-Test.png --> Paul-Test.png.webp

  9. added and removed
    no milestonePRs that do not have a defined milestone for release
    Needs DevAnything that requires development (e.g. a pull request)
    on Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions