In my web app users are allowed to upload images as their photos. How can I convert different image extensions to JPG? Input files are JPG, PNG or GIF.
Asked
Active
Viewed 1.0k times
2
-
possible duplicate of [Convert jpg image to gif, png & bmp format using PHP](http://stackoverflow.com/questions/755781/convert-jpg-image-to-gif-png-bmp-format-using-php) – Pekka Oct 06 '10 at 13:07
4 Answers
3
With php, you can convert any image to an other using imagepng, imagejpeg, imagegif :
imagepng(imagecreatefromstring(file_get_contents($input)), 'output.png');
In this example, it will save the uploaded image in png with the path 'output.png'

ElGato
- 511
- 1
- 5
- 11
3
Personally, I prefer Image Magick over GD. It's a lot better if you're dealing with large images too; you can run into memory allocation issues with GD.

BBonifield
- 4,983
- 19
- 36
0
For anybody who would want to get the binary out of a temporary file, here is my solution:
<?php
$temp = tmpfile();
imagepng(imagecreatefromstring($imgBinary), $temp);
$pathFile = stream_get_meta_data($temp)['uri']; // eg: /tmp/phpFx0513a
$pngBin = file_get_contents($pathFile)
?>

waliby
- 61
- 6