March 9th, 2005, 06:02 AM
inline images in <img> tag -how can I base64 encode a image created with gd libriary?
I've have a function with dynamically creates a png image using php's GD library. Currently I am using a script which just outputs the image to the browser, Like so.
This is called from a php generated html page by the tag,
<img src='map.php' width='600' height='600' alt='' border=0>.
This work fine but I would like to place the image data inline (or within) the original html page. I've been googling heavily and there seems to be a way to do this using a <img> tag like so
The ... in the src represent the image base64 encoded.
<img src="data:image/png;base64, ..." width='600' height='600' alt='' border=0>
This is working great but I can't turn my GD image resource ($image) into a base64 encoded string. Using the command base64_encode($image) throws the error "Warning: base64_encode() expects parameter 1 to be string".
Does anyone know how exactly the GD libriary is storing images and how do you convert these into a base64 encoded string?
Thanks in advance for your time.
PS, The application will be used by mozilla users only and so ie quirks do not apply.
March 9th, 2005, 07:20 AM
I did a little experimenting and I can encode the image fine. It's reading it back that seems to be the problem.
I doubt you'll be able to base64_encode the image in memory.
It'll be something like "resource ID#23" and that's all you'll be encoding is the resource id or array name.
Here's some stuff to play with anyway
$time = time();
$image = imagecreatefromPNG("clock_bg.png");
ImagePng($image, "$time.png", 100);
$img = base64_encode("$time.png");
echo "<p> </p>";
$img = base64_decode($img);
echo "<p> </p>";
$fp = fopen ("$time.png", "rb");
$bytes = 10240;
$buffer = fread($fp, $bytes);
$base = base64_encode($buffer);
<img src="<?php echo"$img"; ?>">
March 9th, 2005, 09:40 AM
That's got me going again and trying some new stuff. Just a couple of questions with regards to some of the problems that have come up using a file based approach.
Is there a good way of deleting the images after a certain amount of time as they will be out of date immediatly after they have been displayed.
How do you handle the hypothetical situation of concurrent users using the function at the exact same time, thus mixing up the image files on the server.
Originally Posted by balamm
March 9th, 2005, 08:50 PM
Using Unix time(); for the filename, chances are real slim that any 2 users would ever get the same file.
You could add sessions, microtime, or something similar to the filename as well I guess.
to delete the temp file, just add
to the end of the script