21

Here is my code:

$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
       . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
       . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
       . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);

$im = imagecreatefromstring($data);
if ($im !== false) {
    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);

}
else {
    echo 'An error occurred.';
}

I'd like to save the image generated this way to a directory. How do I do this?

YakovL
  • 7,557
  • 12
  • 62
  • 102
Baraskar Sandeep
  • 698
  • 2
  • 8
  • 16
  • It happens that the example to save an image is in the [documentation about jpeg](http://www.php.net/manual/en/function.imagejpeg.php), but the usage is very similar for png. – Antony Apr 02 '13 at 09:31

3 Answers3

26

This is the correct syntax for imagepng:

imagepng($im, "/path/where/you/want/save/the/png.png");

According to the PHP manual:

bool imagepng ( resource $image [, string $filename [, int $quality [, int $filters ]]] )

filename - The path to save the file to.

If not set or NULL, the raw image stream will be outputted directly.

tony gil
  • 9,424
  • 6
  • 76
  • 100
Pablo Martinez
  • 2,172
  • 1
  • 23
  • 27
5

Below code will be helpful:

$data = 'code in bytes'; // replace with an image string in bytes
$data = base64_decode($data); // decode an image
$im = imagecreatefromstring($data); // php function to create image from string
// condition check if valid conversion
if ($im !== false) 
{
    // saves an image to specific location
    $resp = imagepng($im, $_SERVER['DOCUMENT_ROOT'].'folder_location/'.date('ymdhis').'.png');
    // frees image from memory
    imagedestroy($im);
}
else 
{
    // show if any error in bytes data for image
    echo 'An error occurred.'; 
}

Please suggest if some other better way of doing !

Aditya P Bhatt
  • 21,431
  • 18
  • 85
  • 104
0

Q: What if image is JPG or GIF?

A: When you use imagecreatefrom... methods, the image is loaded into memory as the uncompressed bitmap. there is not really a image type at this point. you can save it back out as whatever type you wish using the image... function.
Source: https://stackoverflow.com/a/7176074/3705191

But what if we wanted to Determine the Filetype of Byte String?

Before using imagecreatefromstring( $data_string ), the actual $data_string you're providing as the argument for that function can be used to determine the image type:

$data = base64_decode($data);
// imagecreatefromstring( $data ); -- DON'T use this just yet

$f = finfo_open();

$mime_type = finfo_buffer($f, $data, FILEINFO_MIME_TYPE);
// $mime_type will hold the MIME type, e.g. image/png

Credit: https://stackoverflow.com/a/6061602/3705191

You'll have to compare the string you get with the MIME type of common image files (image/jpeg, image/png, image/gif, etc.).

$im = imagecreatefromstirng( $data );

if( $mime_type == "image/png" )
  imagepng( $im, "/path/where/you/want/save/the/png.png" );
if( $mime_type == "image/jpeg" )
  imagejpeg( $im, "/path/where/you/want/save/the/jpg_image.jpg" );
// ...

Check this List of Common MIME Types for reference.

Prid
  • 1,272
  • 16
  • 20
  • **Beware:** this *may* return the generic `application/octet-stream` MIME type if it fails to determine the correct type of image. This can happen when the byte stream holds incomplete information (https://stackoverflow.com/a/66750339/3705191). That's why I'm not sure how reliable this is. Someone suggests to save the file somewhere first and then using e.g. `finfo_open` as a more reliable approach: https://www.php.net/manual/en/function.finfo-buffer.php#Hcom118313 – Prid Jan 25 '23 at 23:56