Error on files with same filenames.
-
I am seeing errors when the plugin attempts to convert files that have the same filenames, even when those files are in different directories. Example:
/2022/06/Screenshot.png
/2026/04/Screenshot.pngThe plugin will not convert either file and will throw a message saying the server is not powerful enough to process the request.
-
Upon further inspection & also crawling the site for 404’s we found more problems…
Both can exist as different PNG files, both have the same basename, example: feature-2.png.
When the first image is converted to JPG the plugin does a broad DB replacement: /feature-2.png → /feature-2.jpg
That can change both database URLs, even if a JPG was created in only one of the two directories. The directory remains visible in the URL, but the extension is changed without confirming that the corresponding JPG exists there. The crawler tries to load /wp-content/uploads/2022/06/feature-2.png and it’s not there because it hasn’t been created.
We encountered lots of these 404s with duplicate basenames:
/wp-content/uploads/2021/09/feature-2.jpg /wp-content/uploads/2022/06/feature-2.jpg /wp-content/uploads/2023/10/Screen-Shot-2023-10-31-at-3.02.31-PM.jpg /wp-content/uploads/2023/11/Screen-Shot-2023-10-31-at-3.02.31-PM.jpg /wp-content/uploads/2023/10/Screen-Shot-2023-10-31-at-3.53.44-PM.jpg /wp-content/uploads/2023/11/Screen-Shot-2023-10-31-at-3.53.44-PM.jpg /wp-content/uploads/2022/10/image.jpg /wp-content/uploads/2025/05/image.jpgHello,
I am seeing errors when the plugin attempts to convert files that have the same filenames, even when those files are in different directories
that is not possible – plugin works with attachment IDs and absolute paths, so it will never try to convert some PNG to JPG just because of the same filename
The plugin will not convert either file and will throw a message saying the server is not powerful enough to process the request.
yes, it’s possible that the image is very big and you don’t have enough resources ( CPU, GPU, RAM, HDD ) on your hosting to process this image… in this case you can use some payed external services where they will process your images on their servers with their resources, but this is just a free plugin trying to do it locally on your server
Both can exist as different PNG files, both have the same basename, example: feature-2.png.
Ah, maybe I understand now what you mean, so it’s not about converting the file, but about replacing file extension in the database, right? That’s true, this can be a problem and I have it in my todo notes.
Problem is that there are ~100K different plugins and there is no standard way how to store attachments in the database, so some plugins like Revolution slider or Smart Slider have/had some crazy database records where they store directory path in one database record and filename in another one, so it would require to write very specific rules to make it compatible with this mechanism… and imagine doing this with hundreds or thousands other plugins to make it compatible…
So I’m working on a completely different solution where nothing will be replaced in the database and only .htaccess rule will load content from a different file, so it will still say image.png in your HTML source code, but actual loaded file content will be from image.jpg that will be stored in a separate directory structure to make sure there will be no conflict with other existing image.jpg
It still requires some testing, but hopefully I will release a new version soon
Appreciate your quick responses.
I have done some further troubleshooting and confirmed that when we have files that use the same filename and convert a single file from PNG to JPG, the plugin is changing the file extension of all the filenames to end in .jpg in the Media Library.
Here’s an example:
/wp-content/uploads/2023/11/Screen-Shot-2023-10-31-at-3.53.44-PM.png
/wp-content/uploads/2023/10/Screen-Shot-2023-10-31-at-3.53.44-PM.png
/wp-content/uploads/2017/07/Screen-Shot-2023-10-31-at-3.53.44-PM.pngThose are the 3 original files. They all exist in different directories, they all have unique IDs. It’s a bit of a mess because this client is essentially uploading the same image with same filename (a mess we are attempting to clean up – and your plugin has come in very handy btw).
But when I only convert one file, say:
/wp-content/uploads/2023/11/Screen-Shot-2023-10-31-at-3.53.44-PM.png
That will convert all of the files to have the .jpg extension when viewing the Media Library. And this is resulting in some 404’s because I believe the plugin is actually only converting one file to JPG (as it should). But it seems to be changing the URLs in the DB for all of the images, pointing to .jpg files that don’t exist for 2 of the images.
In regard to the issue where the plugin would display the ‘server is not powerful enough’ message – I knew this was not directly related to server resources. I can throw an abundance of resources at the server and still get the same results. I think this is happening when files are corrupted or have a .png extension and may not actually be PNG files. I’m still looking into this further and it’s possible the message may occur for various reasons. I’ll keep looking at this & checking the logs to see what I find. I believe it was just coincidence that when I originally ran into the issue it was only on these files that had the same filename.
-
This reply was modified 5 days, 23 hours ago by
Ben Cary.
I also forgot to add… When I’m seeing the image 404’s in the posts after only converting a single PNG file to JPG – and all of the files with the same filenames end up with a .jpg extension – these are just standard post images. They aren’t stored in any special database tables by page builders or anything like that. These are just core WP image blocks within standard posts.
Taking a closer look at your plugin code.
convert_single_image()Properly receives one attachment ID. And it seems to work properly, it converts the single physical file from PNG to JPG as expected.update_image_data()Seems to discard the directory. It begins to only identify the file by the basename:<?php
$old_name = basename( $this->image['link'] );
$new_name = basename( $this->image['new_url'] );Then it performs the global replacements:
<?php
WHERE meta_value LIKE '%/filename.png%'
REPLACE( meta_value, '/filename.png', '/filename.jpg' )It looks like this would impact:
- Other attachments’
_wp_attached_filemetadata - Other attachments’ serialized image metadata
- Post content and excerpts
- Options and third-party plugin tables
- Any URL ending with the same filename, regardless of directory
So one physical PNG file is converted and then several unrelated files are changed to .jpg resulting in 404’s.
I tested some changes to the plugin on my local. I’ve only done some very limited tests but it seems to have worked.
Original Code:
<?php
$old_name = basename( $this->image['link'] );
$old_name_clean = substr( $old_name, 0, -4 );
$new_name = basename( $this->image['new_url'] );
$new_name_clean = substr( $new_name, 0, -4 );
$replaces = array( $old_name => $new_name );<?php
$replaces[ $img['file'] ] = $new_thumb;Updated Code For Testing:
<?php
$upload_dir = wp_get_upload_dir();
$old_name = ltrim( str_replace( trailingslashit( $upload_dir['baseurl'] ), '', $this->image['link'] ), '/' );
$old_name_clean = pathinfo( basename( $old_name ), PATHINFO_FILENAME );
$new_name = ltrim( str_replace( trailingslashit( $upload_dir['baseurl'] ), '', $this->image['new_url'] ), '/' );
$new_name_clean = pathinfo( basename( $new_name ), PATHINFO_FILENAME );
$replaces = array( $old_name => $new_name );<?php
$replaces[ dirname( $old_name ) . '/' . $img['file'] ] = dirname( $new_name ) . '/' . $new_thumb;Will need to test much further but I’m out of time for the weekend. Hopefully on the right track though.
-
This reply was modified 5 days, 23 hours ago by
You must be logged in to reply to this topic.