MySQL Forums
Forum List  »  PHP

Re: How to store a php image resource in MySQL?
Posted by: Paresh Karandikar
Date: February 01, 2010 12:32PM

I found the answer to my own question on php.net. Here is a link to Ashley's write-up:
http://php.net/manual/pl/book.image.php

Thanks Ashley!

For completeness, I will also summarize how to accomplish this here...

Apparently. there is no built-in function to convert a GD image resource to a string, so Ashley accomplished this by writing the image out to the output buffer. Sample code is:

// start output buffering
op_start();
// write the image to the output buffer the same way you would write to a file
imagejpeg($my_resized_image);
// copy the contents of the output buffer to a string variable and stop buffering
$my_image_string = ob_get_clean();


The imagejpeg function expects a filename for the second parameter. If it is null, the raw image stream is outputted directly. Since we first turned on output buffering, the image stream is outputted to the output buffer.

Once we have the image in a string variable we can easily insert it into the MySQL database using the addslashes function:
$sql = "insert into my_image_table(image_id, image) values(" . $img_id;
$sql .= ", \"" . addslashes($my_image_string) . "\")";

Options: ReplyQuote


Subject
Written By
Posted
Re: How to store a php image resource in MySQL?
February 01, 2010 12:32PM


Sorry, you can't reply to this topic. It has been closed.
This forum is currently read only. You can not log in or make any changes. This is a temporary situation.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.