Click to See Complete Forum and Search --> : [RESOLVED] Set GD image stream to variable?


tarsus
08-06-2008, 01:58 PM
With the GD library functions, it seems one can only output an image resource's binary data directly, or to a file:

imagejpeg($imgresource);

imagejpeg($imgresource, $filename);

But the data stream can't simply be stored as a variable:

$datastream = imagejpeg($imgresource);

The above does not work, because imagejpeg() doesn't return a stream, it outputs it.

Why do I want to store the stream itself? Because rather than output the data directly or write a new image file, I want to store it in a database! I've used the GD functions to manipulate an image, and now I want to store the binary data of the resulting image in a database. That is, the same kind of data I would get by running fread() on an existing image.

Isn't there any way to do this?

SyCo
08-06-2008, 05:26 PM
imagejpeg($imgresource, $filename);

Then read with file_get_contents() the file and unlink() it.

MrCoder
08-07-2008, 12:58 AM
ob_start();
imagejpeg($imgresource);
$data = ob_get_clean();

tarsus
08-07-2008, 09:31 AM
Interesting. I've never worked with output buffering; I looked up the documentation after seeing your reply, and it seems pretty straightforward. I'll give it a try!

tarsus
08-07-2008, 09:50 AM
That worked great! Thanks!

MrCoder
08-07-2008, 12:11 PM
Glad I could help!