简体   繁体   中英

base64 encode an image without saving

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()

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM