someone1 Posted April 6, 2007 Share Posted April 6, 2007 Hey, i have a script that stores images in a MySQL database. I need to be able to create thumbnails from those stored images, here's my code so far: <?php include 'db.inc'; if (!($connection = @ mysql_pconnect($hostName, $username, $password))) showerror(); if (!mysql_select_db($database, $connection)) showerror(); $query = "SELECT * FROM pw_catalog WHERE id = {$_GET['id']}"; if (!($result = @ mysql_query ($query,$connection))) showerror(); $data = @ mysql_fetch_array($result); $img_thumb_width = 100; //the new width of the resized image, in pixels. //the image -> variables $file_name = $data['image']; $file_tmp = $data['image']; $file_size = true; //filesize($file_tmp); Can't get working $file_type = $data['image_type']; //create a file name $rand_name = md5($data['name']); $ThumbWidth = $img_thumb_width; ////////////////////////// // CREATE THE THUMBNAIL // ////////////////////////// //keep image type if($file_size){ /* if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){ $new_img = imagecreatefromjpeg($file_tmp); }elseif($file_type == "image/x-png" || $file_type == "image/png"){ $new_img = imagecreatefrompng($file_tmp); }elseif($file_type == "image/gif"){ $new_img = imagecreatefromgif($file_tmp); }*/ $new_img = imagecreatefromstring($file_tmp); //list the width and height and keep the height ratio. //***** list($width, $height) = getimagesize($file_tmp); can't get working $width =396; $height = 285; //calculate the image ratio $imgratio=$width/$height; if ($imgratio>1){ $newwidth = $ThumbWidth; $newheight = $ThumbWidth/$imgratio; }else{ $newheight = $ThumbWidth; $newwidth = $ThumbWidth*$imgratio; } //function for resize image. if (function_exists(imagecreatetruecolor)){ $resized_img = imagecreatetruecolor($newwidth,$newheight); }else{ die("Error: Please make sure you have GD library ver 2+"); } //the resizing is going on here! header("Content-Type: {$file_type}"); imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); ImageJpeg ($resized_img,NULL, 100); //finally, save the image } ImageDestroy ($resized_img); ImageDestroy ($new_img); exit(); ?> What i need: 1. A way to get the dimensions of the image,i only keep image in a blob field in mysql table and the mime_type in a separate field. 2. keep the generated thumbnails in a temp folder so the files aren't being constantly generated, if the file already exists, use it! (but if it doesn't, make the thumbnail and copy it over to the thumbnail directory where it'll self-delete in 1 day). my main concern is only number 1, part 2 is something nice that i don't necessarily need. Any help would be greatly appreciated! Thanks in advanced! Quote Link to comment Share on other sites More sharing options...
joquius Posted April 6, 2007 Share Posted April 6, 2007 $a = getimagesize($url); $a[0]; // x dimension $a[1]; // y dimension $a[2]; // file type (numeric, check php for the types http://www.php.net/function.getimagesize ) Quote Link to comment Share on other sites More sharing options...
jitesh Posted April 6, 2007 Share Posted April 6, 2007 Attached a script to thumbnail images. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
someone1 Posted April 7, 2007 Author Share Posted April 7, 2007 Sorry guys, not what i was looking for, but i did find the answer: imagesx() and imagesy() get the width and height respectively for image types, as I needed since i was creating an image type from the image string i keep in the MySQL db. Thanks for the help anyway! getimagesize() looks for the given parameter as a file, it won't give the dimensions if i pass in the image type Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 7, 2007 Share Posted April 7, 2007 You can't store the images in the database, but the file and/or path - http://snippets.tzfiles.com/snippet.php?id=5 Quote Link to comment Share on other sites More sharing options...
someone1 Posted April 8, 2007 Author Share Posted April 8, 2007 I CAN store images in mysql databases and i HAVE done so. I also successfully generated thumbnails from those images. Just takes a bit of logic Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.