In the PHP manual for base64_encode() I saw the following script for outputting an image.
You have to save your image as a PNG first, and then read from it to get its contents as a value.
http://www.php.net/manual/en/function.imagepng.php
imagepng() does not return the PNG file. It outputs it directly to the browser and then returns a boolean meaning success or failure.
(from php.net:) PHP internally works with a temporary file when sending the image to the browser, so you'll gain nothing by calling imagepng() twice.