Jump to content

PHP Images


ssjskipp

Recommended Posts

Okay, here's one for ya'
I've made this little test:
[code]<?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);
?> [/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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[!--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]<?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);
?> [/code]
That doesn't work...any ideas?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[!--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'?
Link to comment
Share on other sites

[!--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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.