Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save an image created from imagecreatefromstring() function?

Tags:

php

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?

like image 676
Baraskar Sandeep Avatar asked Apr 02 '13 09:04

Baraskar Sandeep


1 Answers

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.

like image 192
Pablo Martinez Avatar answered Sep 20 '22 07:09

Pablo Martinez