Jump to content

Handling Uploaded Images


calvin182
 Share

Recommended Posts

I've already got a script to upload images, store their values in a flatfile, call that file and display the images 16 to a page and generate pages for the full size image along with a backend of options and other features.Currently, I have to size the thumbnail and image on my system, and upload them to the appropriate folder on my system (they both have the same name but go to different folders).What I want to do is cut out that step and include that in the upload process. In a nutshell, I'd like the image to be resized in proportions not to exceed a maximum width. In the same process, I'd like the same to happen but for a thumbnail. The main image would go into a folder called "images" (where the uploaded image is already going) and the thumbnail to go into a folder called "thumbnails".I don't expect somebody to write me the whole thing but if somebody could help me out with a start or point me in the right direction I'd much appreciate it.Oh and the image formats I'm looking to do this for are .jpg/.jpeg/.png/and maybe .gifI can post the upload code if you'd like to see it.-Calvinhttp://www.oneity-eight.com

Link to comment
Share on other sites

OK, I've done almost this exact same thing myself, except the files are stored in a database.I assume you already have all the code for accepting the upload and saving the uploaded file somewhere. If not, let me know, I can help with that part too. I'll try to include all of the relevant code, but if there's anything you don't understand let me know.BTW: GIF images had copyright issues when I wrote this code, and so I converted all GIF files to JPEG.

//define max thumbnail size (width or height, whatever is the larger dimension)$th_size = 100; // in pixels$filename = $_FILES['newimg']['tmp_name'];if (isset($filename) && is_uploaded_file($filename)){  $data = fread(fopen($filename, "rb"), filesize($filename)); //the bytes of the image file, stored in a string  $orig_data = addslashes($data);}//create the thumbnail:$orig = imagecreatefromstring($data);$w = imagesx($orig);$h = imagesy($orig);//calculate the width/height ratioif ($w > $h){  $ratio = $th_size / $w;  $thumb = imagecreatetruecolor($th_size, round($h * $ratio));  imagecopyresampled($thumb, $orig, 0, 0, 0, 0, $th_size, round($h * $ratio), $w, $h);}else{  $ratio = $th_size / $h;  $thumb = imagecreatetruecolor(round($w * $ratio), $th_size);  imagecopyresampled($thumb, $orig, 0, 0, 0, 0, round($w * $ratio), $th_size, $w, $h);}imagedestroy($orig);$temp_img = $temp_save . "img" . rand(10000, 99999) . rand(10000, 99999);$img_ar = getimagesize($filename);switch($img_ar[2]){  case 1: //FALL-THROUGH    //gif    //$temp_img .= ".gif";    //imagegif($thumb, $temp_img);    //break;  case 2:    //jpeg    $temp_img .= ".jpg";    $type = "jpeg";    imagejpeg($thumb, $temp_img);    break;  case 3:    //png    $temp_img .= ".png";    $type = "png";    imagepng($thumb, $temp_img);    break;  case 15:    //wbmp    $temp_img .= ".wbmp";    $type = "wbmp";    imagewbmp($thumb, $temp_img);    break;  default:    //not supported here    $errStr .= "badtype;";    break;}imagedestroy($thumb);$orig = imagecreatefromstring($data);// $width and $height are both form variables (desired width and height)if ($height == ""){  $ratio = $width / $w;  $height = $h * $ratio;}if ($width == ""){  $ratio = $height / $h;  $width = $w * $ratio;}$resized = imagecreatetruecolor($width, $height);imagecopyresampled($resized, $orig, 0, 0, 0, 0, $width, $height, $w, $h);// $temp_save is a hard-coded directory to save temporary files// create a temp filename based off random numbers:$resized_img = $temp_save . "img" . rand(10000, 99999) . rand(10000, 99999) . "_resize";switch ($type){  case 'jpeg':    $resized_img .= ".jpg";    imagejpeg($resized, $resized_img);    break;  case 'png':    $resized_img .= ".png";    imagepng($resized, $resized_img);    break;  case 'wbmp':    $resized_img .= ".wbmp";    imagewbmp($resized, $resized_img);    break;}imagedestroy($orig);

OK, I think that's pretty much it, but I need to give you some more notes. I extracted the code out of one large file, I can send you the whole file if necessary, it includes a lot more code.Some variables are pre-defined, look at the comments in the code for descriptions of them (i.e. $temp_save, $width, height, etc).Those last few functions (imagejpg, imagepng, imagewbmp etc) can either output to the browser or to a file. I have those outputting to a random filename in the $temp_save folder.You can go to php.net and type in any function name I used above (like 'imagecopyresampled') into the Search box, search the function list, and you can get documentation on that specific function.Also, like I said before, this code is a little different because it saves to a database instead of files, so if you have any questions about it let me know.-Steve

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...