Save gd image to database

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
Dynamis
Forum Contributor
Posts: 122
Joined: Thu Jul 10, 2008 3:15 pm
Location: Indiana, US

Save gd image to database

Post by Dynamis »

Here is what I am doing:
Allow user to upload image.
I create a thumbnail of the image.
Want to save thumbnail to database.

A little code:

Code: Select all

 
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
 
So as you can see, dst_img is a truecolor gd image. How can I get the bytes from that to put into the database? I know I could save it as a temporary file, read the file bytes, save those, then delete the temporary file, but I would rather not do that.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Save gd image to database

Post by onion2k »

Use the output buffer. Eg

Code: Select all

ob_start();
imagejpeg($dst_img);
$image = ob_get_clean();
 
$image will contain the image in jpeg format.
Post Reply