41

Possible Duplicate of
Convert jpg image to gif, png & bmp format using PHP

I have a PHP form that allows image uploads and checks exif_imagetype(); to make sure an image is valid.

However, I want all formats, PNG, JPG, JPEG, and GIF, to end up being PNG once submitted.

How can I go about doing this?

Community
  • 1
  • 1
Aaron
  • 1,956
  • 5
  • 34
  • 56
  • http://stackoverflow.com/questions/755781/convert-jpg-image-to-gif-png-bmp-format-using-php – jcomeau_ictx Dec 18 '11 at 05:34
  • 2
    Possible duplicate of [Convert jpg image to gif, png & bmp format using PHP](https://stackoverflow.com/questions/755781/convert-jpg-image-to-gif-png-bmp-format-using-php) – Cœur Jul 14 '18 at 11:52

4 Answers4

105

You just need imagepng() then. In fact it almost becomes a one-liner:

 imagepng(imagecreatefromstring(file_get_contents($filename)), "output.png");

You would use $_FILES["id"]["tmp_name"] for the filename, and a different output filename obviously. But the image format probing itself would become redundant.

mario
  • 144,265
  • 20
  • 237
  • 291
  • 2
    This worked perfectly. I just changed output.png to "uploads/out.png" to move directories. – Aaron Dec 18 '11 at 06:00
  • But I'm wondering if this destroyed the temp file? Or do I need to run an `imagedestroy()` to remove the temp file? – Aaron Dec 18 '11 at 06:00
  • 1
    In this example, you can't use [`imagedestroy`](http://php.net/imagedestroy) as it needs a handle (you can get this if you use two lines of code). The tempfile from the upload will be deleted automatically when the script ends. – hakre Dec 18 '11 at 06:02
  • Does Image quality get affected if I convert jpg to png? I know that gif and png supports same color types, but jpg supports more. – harishkumar329 Jan 06 '15 at 10:31
  • 2
    @issueNo329 If your input is JPEG, then it *already has* compression artificats. Converting it to PNG will not worsen those, but won't compress well. (Remember PNGs are for solid-paint graphics, JPEGs for squishy photos.) – mario Jan 06 '15 at 16:08
  • How convert to .ico files? – Mostafa Jun 26 '16 at 06:50
  • @Mostafa: First google result: http://stackoverflow.com/questions/2628114/convert-png-file-to-ico-with-php – mario Jun 26 '16 at 06:58
  • I got this error: ```imagepng() [function.imagepng]: Unable to open 'output.png' for writing``` – shintaroid Apr 05 '18 at 07:08
  • @shintaroid Cool. But what are your exact thoughts on "Unable to open ... for writing" ? – mario Apr 05 '18 at 07:12
  • @mario, thanks for your comment. In fact I used ```$file = imagepng(imagecreatefromstring(file_get_contents($_FILES["imgToUpload"]["tmp_name"])), "header.png");```, but if I use ```ftp_put($conn_id, $remote_file, imagepng(imagecreatefromstring(file_get_contents($_FILES["imgToUpload"]["tmp_name"])), "header.png"), FTP_BINARY)``` it kind of worked (I still got this error: ```imagecreatefromstring() [function.imagecreatefromstring]: No JPEG support in this PHP build```but I think it's a problem of the server) – shintaroid Apr 05 '18 at 07:20
16

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.

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
5
<form method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="Submit" />
</form>

<?php
if(isset($_POST['submit']))
{
    if(exif_imagetype($_FILES['image']['tmp_name']) ==  IMAGETYPE_GIF) 
    {
        $newpng = 'image.png';
        $png = imagepng(imagecreatefromgif($_FILES['image']['tmp_name']), $newpng);
    }
    elseif(exif_imagetype($_FILES['image']['tmp_name']) ==  IMAGETYPE_JPEG) 
    {
        $newpng = 'image.png';
        $png = imagepng(imagecreatefromjpeg($_FILES['image']['tmp_name']), $newpng);
    }
    else //already png
    {
        $newpng = 'image.png';
    }       
}
?>
Zul
  • 3,627
  • 3
  • 21
  • 35
3

Very simple using the gd functions:

switch (exif_imagetype($image)) {
    case IMAGETYPE_GIF :
        $img = imagecreatefromgif($image);
        break;
    case IMAGETYPE_JPEG :
        $img = imagecreatefromjpeg($image);
        break;
    default :
        throw new InvalidArgumentException('Invalid image type');
}

imagepng($img, $filename);

For conciseness this obviously doesn't handle the case if the image is already a PNG.

deceze
  • 510,633
  • 85
  • 743
  • 889