ssjskipp Posted April 24, 2006 Share Posted April 24, 2006 Okay, here's one for ya'I've made this little test:[code]<?phpheader ("Content-type: image/png");$im = imagecreate(51, 51) or die("Error");//Set the BG$bg = imagecolorallocate($im, 255, 255, 255);//Set the Pixel Color$textcolor = imagecolorallocate($im, 0, 0, 0);//Draw the Pixelsfor ($i=0;$i<55;$i++){ for ($a=0;$a<55;$a+=5){ imagesetpixel($im, $i, $a, $textcolor); imagesetpixel($im, $a, $i, $textcolor); }}//Output the imageimagepng($im);?> [/code]And it worked with creating the image, but all I need to know is:How do I save it to the directory the PHP file was?OR!How do I save the image to a MySQL database, to be retrieved any time?PS:I know how to do everything with the database, but I just need ot know what to insert. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 24, 2006 Share Posted April 24, 2006 Change end bit of code to[code]//Output the imageimagepng($im, $filename);imagedestroy($im);[/code]where $filename is the file you want to save the image in. Store the file name in the db table; Quote Link to comment Share on other sites More sharing options...
KrisNz Posted April 24, 2006 Share Posted April 24, 2006 imagepng will save the file for you if you pass it a path to save to. if you want to put the raw image data into a mysql field you'll need to save the image at least temporarily, read the image data into a variable with file_get_contents, base64_encode & chunk_split the data and write it to a medium blob field. Quote Link to comment Share on other sites More sharing options...
ssjskipp Posted April 24, 2006 Author Share Posted April 24, 2006 [!--quoteo(post=367822:date=Apr 23 2006, 09:01 PM:name=KrisNz)--][div class=\'quotetop\']QUOTE(KrisNz @ Apr 23 2006, 09:01 PM) [snapback]367822[/snapback][/div][div class=\'quotemain\'][!--quotec--]imagepng will save the file for you if you pass it a path to save to. if you want to put the raw image data into a mysql field you'll need to save the image at least temporarily, read the image data into a variable with file_get_contents, base64_encode & chunk_split the data and write it to a medium blob field.[/quote]May I see an example of this?Also:[code]<?phpheader ("Content-type: image/png");$im = imagecreate(51, 51) or die("Error");//Set the BG$bg = imagecolorallocate($im, 255, 255, 255);//Set the Pixel Color$textcolor = imagecolorallocate($im, 0, 0, 0);//Draw the Pixelsfor ($i=0;$i<55;$i++){ for ($a=0;$a<55;$a+=5){ imagesetpixel($im, $i, $a, $textcolor); imagesetpixel($im, $a, $i, $textcolor); }}//Set a file location$filename = "test.png";//Output the imageimagepng($im, $filename);imagedestroy($im);?> [/code]That doesn't work...any ideas? Quote Link to comment Share on other sites More sharing options...
KrisNz Posted April 24, 2006 Share Posted April 24, 2006 This is pretty rough but should get you started.[code] $tempname ="/tmp/".uniqid("img"); imagepng($im,$tempname); $rawImageData = file_get_contents($tempname); $rawImageData = chunk_split(base64_encode($rawImageData)); $sql = "insert into TABLE('imagedata') VALUES('$rawImageData')"; mysql_query($sql); unlink($tempname);[/code] with your code try removing the header and passing imagepng an absolute path. Quote Link to comment Share on other sites More sharing options...
ssjskipp Posted April 24, 2006 Author Share Posted April 24, 2006 [!--quoteo(post=367829:date=Apr 23 2006, 09:42 PM:name=KrisNz)--][div class=\'quotetop\']QUOTE(KrisNz @ Apr 23 2006, 09:42 PM) [snapback]367829[/snapback][/div][div class=\'quotemain\'][!--quotec--]This is pretty rough but should get you started.[code] $tempname ="/tmp/".uniqid("img"); imagepng($im,$tempname); $rawImageData = file_get_contents($tempname); $rawImageData = chunk_split(base64_encode($rawImageData)); $sql = "insert into TABLE('imagedata') VALUES('$rawImageData')"; mysql_query($sql); unlink($tempname);[/code] with your code try removing the header and passing imagepng an absolute path.[/quote]Thanks mate! I've got one more quick question -- how would I go about recalling the images? Like, after I query the database, get the data back, how do I convert it to an 'image'? Quote Link to comment Share on other sites More sharing options...
KrisNz Posted April 25, 2006 Share Posted April 25, 2006 [!--quoteo(post=368207:date=Apr 25 2006, 09:33 AM:name=ssjskipp)--][div class=\'quotetop\']QUOTE(ssjskipp @ Apr 25 2006, 09:33 AM) [snapback]368207[/snapback][/div][div class=\'quotemain\'][!--quotec--]Thanks mate! I've got one more quick question -- how would I go about recalling the images? Like, after I query the database, get the data back, how do I convert it to an 'image'?[/quote]base64_decode the data and then write it out to a file. So when you write it to the db you'll also need to save the filename or at least the file extension so you know what to recreate it as later. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.