Jump to content


Photo

PHP Images


  • Please log in to reply
6 replies to this topic

#1 ssjskipp

ssjskipp
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 24 April 2006 - 12:19 AM

Okay, here's one for ya'
I've made this little test:
<?php
header ("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 Pixels
for ($i=0;$i<55;$i++){
    for ($a=0;$a<55;$a+=5){
        imagesetpixel($im, $i, $a, $textcolor);
        imagesetpixel($im, $a, $i, $textcolor);
    }
}
//Output the image
imagepng($im);
?>

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.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 24 April 2006 - 12:44 AM

Change end bit of code to

//Output the image
imagepng($im, $filename);
imagedestroy($im);

where $filename is the file you want to save the image in. Store the file name in the db table;
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 KrisNz

KrisNz
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • LocationAuckland, New Zealand

Posted 24 April 2006 - 01:01 AM

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.


#4 ssjskipp

ssjskipp
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 24 April 2006 - 01:23 AM

[!--quoteo(post=367822:date=Apr 23 2006, 09:01 PM:name=KrisNz)--][div class=\'quotetop\']QUOTE(KrisNz @ Apr 23 2006, 09:01 PM) View Post[/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:
<?php
header ("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 Pixels
for ($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 image
imagepng($im, $filename);
imagedestroy($im);
?>
That doesn't work...any ideas?

#5 KrisNz

KrisNz
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • LocationAuckland, New Zealand

Posted 24 April 2006 - 01:42 AM

This is pretty rough but should get you started.
  $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);


with your code try removing the header and passing imagepng an absolute path.

#6 ssjskipp

ssjskipp
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 24 April 2006 - 11:33 PM

[!--quoteo(post=367829:date=Apr 23 2006, 09:42 PM:name=KrisNz)--][div class=\'quotetop\']QUOTE(KrisNz @ Apr 23 2006, 09:42 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
This is pretty rough but should get you started.
  $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);


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'?

#7 KrisNz

KrisNz
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • LocationAuckland, New Zealand

Posted 25 April 2006 - 12:09 AM

[!--quoteo(post=368207:date=Apr 25 2006, 09:33 AM:name=ssjskipp)--][div class=\'quotetop\']QUOTE(ssjskipp @ Apr 25 2006, 09:33 AM) View Post[/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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users