how to obtain byte stream from an image resource

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

how to obtain byte stream from an image resource

Post by davidklonski »

Hello

I have generated an image resource using imagecopyresampled.
Is there a simple way to obtain the byte stream of that resource?

An indirect method would be to store the image to a file like so:

Code: Select all

// assuming this is a jpeg
ImageJPEG($resource, $filename);
// read the byte stream using fread
$stream = fread(fopen($filename, "r"), filesize($filename));
This method is awkward because it may be slow (generating a file) and I would need to delete the file afterwards.

is there a better way?

My ultimate goal is to save the byte stream as a blob in the database.

thanks in advance
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I believe,

Code: Select all

$stream = ImageJPEG($resource)
where $stream is now the data you seek. I think...
Post Reply