• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

How to convert jpeg image to jpg image while uploading in wordpress?

Iron-Man

Active member
Babiato Lover
Oct 9, 2019
304
87
28
29
India
Is there any way to convert jpeg image to jpg image upon upload using any snippet or something else. There is no such plugins available in wordpress plugin directory. I know that both jpeg and jpg are almost same. But I want all jpeg converted to jpg. Is this possible in any way?
 
You dont need to convert as jpeg and jpg are same. You can rename while the extension while uploading to server.
 
  • Like
Reactions: Iron-Man
I know both are same. I am just asking that is there any way that jpeg will become jpg after uploading to wordpress.
 
You can use php rename() function in your upload script.
Can you explain me how to do this? I do not use any script while uploading. I uploading normally using media library. Is there any snipped or code change in wp core files; I can do it .
 
You have to check the media upload code and add rename function or you can use plugin mentioned above.
 
  • Like
Reactions: Iron-Man
The plugin 'File Renaming on Upload' has that as an option in their PRO version, which can be found here on Babiato:


However, it's a bit overkill to install that plugin just for that function, I guess, but I'm sure the code needed can be found within that plugin.
Thank you very much bro; Finally I have found something which can convert jpeg to jpg. I was really frustrated to find a solution. I have not tried this plugin yet, but will update if found it works.

Note: This answer may help solve my problem, which I was facing with the media file renamer pro plugin. You can find my problem in that plugin's discussion thread.: https://babia.to/threads/media-file-renamer-pro.13128/post-966248
 
You have to check the media upload code and add rename function or you can use plugin mentioned above.
I would like to know how this rename function works in the media upload code & then try to change it. Any article or KB will be helpful. Thanks.
 
After a bit of experimenting, adding the following code to functions.php works for me:

PHP:
function wp_rename_jpeg_to_jpg( $file ){
   $img = getimagesize($file['tmp_name']);
   $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
    if ($ext === 'jpeg') {
     $file['name'] = substr($file['name'], 0, strrpos($file['name'], ".")) . '.jpg';
    }
   return $file;
}
add_filter('wp_handle_upload_prefilter', 'wp_rename_jpeg_to_jpg', 1, 1);
 
  • Love
Reactions: Iron-Man
After a bit of experimenting, adding the following code to functions.php works for me:

PHP:
function wp_rename_jpeg_to_jpg( $file ){
   $img = getimagesize($file['tmp_name']);
   $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
    if ($ext === 'jpeg') {
     $file['name'] = substr($file['name'], 0, strrpos($file['name'], ".")) . '.jpg';
    }
   return $file;
}
add_filter('wp_handle_upload_prefilter', 'wp_rename_jpeg_to_jpg', 1, 1);
Wow it works like charm. Many many thanks brother frizzel.

Is there any such snippet available which can convert png to jpg plus/or compress them? If yes, then there would not need to use any plugin for image compression/conversion plugin.

Any idea bro?
 
  • Like
Reactions: Iron-Man
I know there are tons of plugin bro. I am asking about any snippet to do the job. I do not think it can be possible. But if possible, then it will be very good.
My snippet above (for jpeg to jpg) simply replaces the file extension, nothing else. Converting from png to jpg is a whole different ballgame, as it's about changing the image format.

Why re-invent the wheel when there's already a perfect and lightweight plug-in doing the job? The first mentioned PNG to JPG:


It basically is only one php file, in other words a 'snippet' but including a few handy options.
 
  • Like
Reactions: Iron-Man
My snippet above (for jpeg to jpg) simply replaces the file extension, nothing else. Converting from png to jpg is a whole different ballgame, as it's about changing the image format.

Why re-invent the wheel when there's already a perfect and lightweight plug-in doing the job? The first mentioned PNG to JPG:


It basically is only one php file, in other words a 'snippet' but including a few handy options.
Hm. You are right brother. Anyway, thanks for the snippet.
 
Converting JPEG to JPG in WordPress? It's a bit tricky since they're essentially the same format. But if you're set on it, you might need to get your hands dirty with some code.
One workaround could be to use a script that renames the file upon upload, replacing ".jpeg" with ".jpg". It's a bit of a hack, but it could get the job done.
Oh, and speaking of digital stuff, did you know you can actually compress JPEG files to save space without losing quality? It's a handy trick for managing your image files, whether they're JPEG or JPG.
 
Last edited:
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock