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

silumantu

Member
Oct 9, 2019
169
23
18
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?
 

moksit

Well-known member
Jul 12, 2020
209
298
63
You dont need to convert as jpeg and jpg are same. You can rename while the extension while uploading to server.
 
  • Like
Reactions: silumantu

silumantu

Member
Oct 9, 2019
169
23
18
I know both are same. I am just asking that is there any way that jpeg will become jpg after uploading to wordpress.
 

silumantu

Member
Oct 9, 2019
169
23
18
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 .
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me

moksit

Well-known member
Jul 12, 2020
209
298
63
You have to check the media upload code and add rename function or you can use plugin mentioned above.
 
  • Like
Reactions: silumantu

silumantu

Member
Oct 9, 2019
169
23
18
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://babiato.tech/threads/media-file-renamer-pro.13128/post-966248
 

silumantu

Member
Oct 9, 2019
169
23
18
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.
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
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: silumantu

silumantu

Member
Oct 9, 2019
169
23
18
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?
 

sitis

New member
May 11, 2022
17
5
3
Bucharest, ROMANIA
sitis.ucoz.com
  • Like
Reactions: silumantu

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
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: silumantu

silumantu

Member
Oct 9, 2019
169
23
18
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.
 

Latest posts

Forum statistics

Threads
69,408
Messages
909,598
Members
239,279
Latest member
jeslewisParis

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu