Forum Moderators: coopster

Message Too Old, No Replies

php imagecreate and mysql

inserting an image created by php into a mysql blob field

         

aztuscani

5:44 pm on Aug 10, 2007 (gmt 0)

10+ Year Member



Is it possible to create an image with php(using imagecreate), and insert the image data into a mysql blob field directly? I can only make it work by saving the image as a physical file first, and then uploading said file, but I would like to skip that step as it seems unnecessary.

aztuscani

6:00 pm on Aug 10, 2007 (gmt 0)

10+ Year Member



Ok here is what I have come up with if anyone wants to offer suggestions anyways:

function imagepng2string($im)
{
$contents = ob_get_contents();

if ($contents!== false) ob_clean(); else ob_start();

imagepng($im);
$data = ob_get_contents();

if ($contents!== false)
{
ob_clean();
echo $contents;
}
else ob_end_clean();

return $data;
}

$data = addslashes(imagepng2string($im));

now $data is formatted properly to be inserted into a mysql table.

coopster

10:22 pm on Aug 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, aztuscani.

Buffering the output is a viable option. However, you should use mysql_real_escape_string() [php.net] to insert binary data rather than addslashes.