[HELP] How to save the GD resource into MySQL.

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
00061205
Forum Newbie
Posts: 4
Joined: Thu Nov 13, 2008 9:37 pm

[HELP] How to save the GD resource into MySQL.

Post by 00061205 »

I created a image and got a GD resource. Can anybody tell me how to save the image which i created into database?
Thanks!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: [HELP] How to save the GD resource into MySQL.

Post by requinix »

It'd be better for you to dump that image into a file and store the filename instead.
00061205
Forum Newbie
Posts: 4
Joined: Thu Nov 13, 2008 9:37 pm

Re: [HELP] How to save the GD resource into MySQL.

Post by 00061205 »

tasairis wrote:It'd be better for you to dump that image into a file and store the filename instead.
i'm afraid it's not efficient.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: [HELP] How to save the GD resource into MySQL.

Post by requinix »

00061205 wrote:
tasairis wrote:It'd be better for you to dump that image into a file and store the filename instead.
i'm afraid it's not efficient.
And how do you know that? Because last I knew it was more efficient than doing it your way.
00061205
Forum Newbie
Posts: 4
Joined: Thu Nov 13, 2008 9:37 pm

Re: [HELP] How to save the GD resource into MySQL.

Post by 00061205 »

tasairis wrote:
00061205 wrote:
tasairis wrote:It'd be better for you to dump that image into a file and store the filename instead.
i'm afraid it's not efficient.
And how do you know that? Because last I knew it was more efficient than doing it your way.
OK,i'll try, thank you for your reply.
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: [HELP] How to save the GD resource into MySQL.

Post by chopsmith »

I, too, prefer the method suggested by tasairis. However, if you want to save the image to the db, just use the BLOB field type: http://dev.mysql.com/doc/refman/5.0/en/blob.html
00061205
Forum Newbie
Posts: 4
Joined: Thu Nov 13, 2008 9:37 pm

Re: [HELP] How to save the GD resource into MySQL.

Post by 00061205 »

chopsmith wrote:I, too, prefer the method suggested by tasairis. However, if you want to save the image to the db, just use the BLOB field type: http://dev.mysql.com/doc/refman/5.0/en/blob.html
yeah, i know use the BLOB type but how to get the image data from the GD resource? I can't just save the GD resource to the db.

Code: Select all

 
$im = imagecreate(500,500);
$red = imagecolorallocate($im, 255,0,0);
imagefilledrectangle($im,0,0,500,500,$red);
 
If just save the $im to the db the image data actually not save to the db.
Is there a way to transform the GD resource type $im to string type?
Tellurian7
Forum Newbie
Posts: 1
Joined: Fri Dec 19, 2008 4:27 am

Re: [HELP] How to save the GD resource into MySQL.

Post by Tellurian7 »

Hi,

(First, sorry for my bad english, i'm french. :D )

I'm programming a php class with image manipulation functions, and i have the same answer about saving gd image resource in a BLOB field ...
Then i'm lookin for a function called like "imagestring" (like imagejpeg, imagepng.., not the text writing function).
But this function musn't use thinks like this (bad performance) :

imagepng($oImage, "temp.png"); $sData = file_get_content("temp.png") ...

Any idea? Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: [HELP] How to save the GD resource into MySQL.

Post by requinix »

There's no "imagestring" like what you want. Images have different formats thus all those imagepng/imagegif/etc functions.

Use output buffering.

Code: Select all

ob_start();
imagepng($oImage);
$string = ob_get_contents();
ob_end_clean();
 
// $string is the image in PNG format
And next time don't hijack someone else's thread for your own question, even if the two are related.
Post Reply