Jump to content

[SOLVED] Uploading Images


cheechm

Recommended Posts

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");

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.