Reputation: 7109
I am created dynamic image using php and gd library as follows,
imagecopy ($iOut,$imgBuf[0],$left,$top,0,0,imagesx($imgBuf[0]),imagesy($imgBuf[0]));
imagedestroy ($imgBuf[0]);
ob_start();
imagepng($iOut);
printf('<img src="data:image/png;base64,%s"/>', base64_encode(ob_get_clean()));
How can I save this image to my local directory for further use. Any help please
Thanks
Upvotes: 9
Views: 28574
Reputation: 1880
function render image with php GD
function render_image_page() {
header ('Content-type: image/png');
$im = @imagecreatetruecolor(120, 20) or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);exit;
}
Upvotes: -1
Reputation: 3645
Look at the imagepng
documentation at http://php.net/manual/en/function.imagepng.php
You can pass in a second parameter to the function as
bool imagepng ( resource $image [, string $filename [, int $quality [, int $filters ]]] )
So you can do
imagepng($iOut, $filename_to_save_to);
then you can simply display the image in the browser as
echo '<img src="' . $public_visible_path_to_saved_file . '"/>';
For this to work, i would choose the $filename_to_save_to
as a file in a subdirectory of your web root. E.g.
if your web root is /var/www
i would choose /var/www/uploaded_images/filename.png
then you can simply display it by specifying the $public_visible_path_to_saved_file
as uploaded_images/filename.png
Upvotes: 20
Reputation: 4308
This will work if you want to save the files to your server:
ob_start();
imagepng($iOut);
$data = ob_get_clean();
file_put_contents('/my/file.png',$data);
Upvotes: 6