Re: Thumb nail images to MySql
| From: | Dave Carrera | Date: | Tue, 06 May 2003 05:34:35 +0000 |
| Subject: | Re: Thumb nail images to MySql | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-27551@lists.php.net to get a copy of this message | ||
Hi all
I think I did not explain myself properly with my need to create thumbnails
but after much searching and local testing I have created a script of which
I give to all of you here who may want to use it. It may be rough around the
edges but it works.
Prog flow:
Form has FILE input field to find local jpg image.
On submit original file is uploaded to ./big dir
Then a thumbnail of 100x70 pixels is created and placed into ./thumb dir
All files are named with a unique filename.
Then path info only entered into MySql db for recall later, I never wanted
to load images to a db, bad idea...
--- Start ---
If(isset($_POST[picture]){
$bigdir = "./prodimg/big";
$tumbdir = "./prodimg/thumb";
$tw = 70;
$th = 100;
if(isset($_POST[addproduct])){
if(isset($_FILES[picture])){
$fileName = explode(".", $_FILES['picture']['name']);
$name = str_replace(' ', '_', $fileName[0]);
$img_orig_size = getimagesize($_FILES['picture']['tmp_name']);
$img_orig_width = $img_orig_size[0];
$img_orig_height = $img_orig_size[1];
$img_original = ImageCreateFromJpeg($_FILES['picture']['tmp_name']);
$image_stored = time() . "_$name.jpg";
ImageJPEG($img_original, ".$bigdir/$image_stored",100);
$img_resized = ImageCreateTrueColor($tw, $th);
imagecopyresampled($img_resized,
ImageCreateFromJpeg($_FILES['picture']['tmp_name']), 0 , 0 , 0 , 0, $tw,
$th, $img_orig_width, $img_orig_height);
$img_name = "tn_$image_stored";
Imagejpeg($img_resized, ".$tumbdir/$img_name",85);
ImageDestroy($img_resized);
$mysql_thumb = "$tumbdir/$img_name"; //thumbnail path to write to mysql
$mysql_big = "$bigdir/$image_stored"; // original path to write to mysql
// Do your own mysql_query here
}
--- End ---
You will need to 777 both the thumb and big dir. Also note the use of
"ImageCreateTrueColor". This is tons better than just using "ImageCreate",
also note the 85. This sets the jpg compression. I found this value gives a
good balance between visual quality and file size. $_POST[picture] comes
from an form with a field of this name. and remember to add the
ENCTYPE="multipart/form-data" to your form.
If you want to better this script, error checking or code tidying, then
please feel free but let me know.
Thanks for all the pointers within this discussion
Dave C
http://www.ephgroup.com
Secure Hosting Accounts for everyone.
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.476 / Virus Database: 273 - Release Date: 24/04/2003