Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base64 encode an image without saving

Tags:

php

base64

gd

Can I base64 encode an image that I created on the fly, without first saving it to disk? As far as I know, base64_encode() only accepts strings, and I couldn't find a way to retrive image source object as string without first saving it, and load it with file_get_contents()

like image 878
yasar Avatar asked Oct 17 '11 17:10

yasar


1 Answers

GD doesn't provide a method to return an output image as text, but you can fake it with the output buffering functions:

ob_start();
imagejpeg($handle); // no second parameter, will do output instead of writing to file
$img = ob_get_clean();

echo base64_encode($img);
like image 184
Marc B Avatar answered Sep 22 '22 13:09

Marc B