How to convert image format types eg JPG/GIF image to PNG ?


If you're searching for this question, it means you have been in my shoes some day, now its good I was on this shoes earlier.

The first solution will be to simply edit the file name and change the extension for example if it was 'image.png', you just rename it to 'image.jpeg' or 'image.gif' or something else. While this will work on many instances it could be required, the real contents of the image will still be as it was originally, so if you're a developer and perhaps you needed the original content as same as the extension, this wont be a solution. So what?

You can head over here and a quick conversion using our tool that changes the content as well as the output.

You could use any language but I'll illustrate with PHP:

Based on what kind of image it is you could select the correct function to open the file:

$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); switch ($extension) { case 'jpg': case 'jpeg': $image = imagecreatefromjpeg($filename); break; case 'gif': $image = imagecreatefromgif($filename); break; case 'png': $image = imagecreatefrompng($filename); break; }

Then you just save the file using:

imagepng($image, $new_filename, $quality);

It might be worth noting that this will only parse the actual extension and not really validate so the file is in the specific format. For instance, you could take a jpg image and just change the extension to png. So a better approach is to use the exif_imagetype() which will return the type of image not depending on the actual extension.

There are no comments yet

Add Comment

Note: We sanitize our comments to keep them clean!

Share this Page