I'm trying to create an image with the php libgd library and use the variable that is being used to store the image info to embed the image on a page.
e.g.
$image = get_image();
function get_image() {
$image = imagecreate(...);
...
return $image;
}
//echo '<img src="'.$image.'"/>';
Hopefully you get what I'm trying to do.. I know I can create a separate .php file that could be used like:
echo '<img src="image.php"/>';
where image.php is..
<?php
$image = get_image();
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
Is there anyway to avoid doing the second example, i.e. what I tried to explain th开发者_C百科e first time?
The only way you could do something similar is with a data URI. However, cross-browser support is very mixed, so it's probably not an option yet.
Your best bet is probably an appropriate caching scheme in the filesystem. This also has performance benefits, since you don't need to generate the image every time.
If you do go the data URI route, you can output the base64 part like this:
ob_start();
imagepng($image);
$image_str = ob_get_clean();
echo base64_encode($image_str);
精彩评论