cheechm Posted May 11, 2008 Share Posted May 11, 2008 HI, I create an image with this script (http://nicks-world.co.uk/final.phps) and I then want to insert the created image into a database. How would I do that? Thanks Working version here: http://nicks-world.co.uk/final.php Link to comment https://forums.phpfreaks.com/topic/105182-solved-uploading-images/ Share on other sites More sharing options...
owned Posted May 12, 2008 Share Posted May 12, 2008 I tried the script, it was resource hog for some reason. Those kind of system-killer scripts shouldn't be used. And second, probably images shouldn't be put in database without very good reason. File system is most of the time better. Images just slow down sql server, I think. Example of image to database: <?php $font="tahoma.ttf"; $col= imagecolorallocate($im, 0,170,0); $im = imagecreatetruecolor(400, 400); imagettftext($im, 20,0,400/2,400/2, $col, $font, 'TeXt'); header ("Content-type: image/png"); imagepng($im); ob_start(); imagepng($im); $image_data = ob_get_contents(); ob_end_clean(); $query="INSERT INTO images(imagedata) VALUES('".mysql_escape_string($image_data)."')"; $fetch = mysql_query($query, $connection) or die("error"); //or using base64, fetched imagedata must be decoded then of course //$query="INSERT INTO images(imagedata) VALUES('".base64_encode($image_data)."')"; //$fetch = mysql_query($query, $connection) or die("error"); //imagedata field must be longblob imagedestroy($im); ?> When fetching the image from database just echo() the result and set header("Content-Type: image/png"); Link to comment https://forums.phpfreaks.com/topic/105182-solved-uploading-images/#findComment-538645 Share on other sites More sharing options...
cheechm Posted May 12, 2008 Author Share Posted May 12, 2008 That doesn't work. cannot be displayed, because it contains errors. Link to comment https://forums.phpfreaks.com/topic/105182-solved-uploading-images/#findComment-539222 Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 Usually the best thing to do would be to save the image to the server, and keep the path to the image in the database. You will find it much faster and alot less headaches. Ray Link to comment https://forums.phpfreaks.com/topic/105182-solved-uploading-images/#findComment-539225 Share on other sites More sharing options...
cheechm Posted May 12, 2008 Author Share Posted May 12, 2008 How could I do that? (Sorry.. NOOB Alert) Link to comment https://forums.phpfreaks.com/topic/105182-solved-uploading-images/#findComment-539227 Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 First let me ask this. If someone is uploading an image why you using php to create it?? Do you want to resize it or something?? Or is a script which creates an image for the user? Ray Link to comment https://forums.phpfreaks.com/topic/105182-solved-uploading-images/#findComment-539232 Share on other sites More sharing options...
cheechm Posted May 12, 2008 Author Share Posted May 12, 2008 Literally it is for a website. The admin inserts text into a field. An image is then created with that text. This takes along time, so I want to either save the image or insert it into a database instead of re-running the script. No one uploads anything. Thanks Link to comment https://forums.phpfreaks.com/topic/105182-solved-uploading-images/#findComment-539246 Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 OK I got ya. ok so do this. After they enter the text you will direct them to the page. just add a couple lines in. Somewhere up top add a path to where you want the pic to be saved. It should be an absolute path $save_path = "/home/pathe/to/image/"; $image_name = "somename"; The change this header("content-type: image/png"); imagepng($small); imagedestroy($mask); imagedestroy($image); imagedestroy($small); to this // no need for header imagepng($small, $save_path.$image_name); // this will save the file rather than display it imagedestroy($mask); imagedestroy($image); imagedestroy($small); // run sql below to save image name $sql = "INSERT INTO table (`imagename`) VALUES ('$image_name'); Now when you want to display it $sql = "SELECT `imagename` FROM table"; $res = mysql_query($res) or die(mysql_error()); $r = mysql_fetch_assoc($res); echo "<img src=\"path/to/image/{$r['imagename']}\" />"; Ray Link to comment https://forums.phpfreaks.com/topic/105182-solved-uploading-images/#findComment-539263 Share on other sites More sharing options...
cheechm Posted May 12, 2008 Author Share Posted May 12, 2008 Thanks so much craygo. Fixed. Link to comment https://forums.phpfreaks.com/topic/105182-solved-uploading-images/#findComment-539350 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.