shortysbest Posted June 21, 2010 Share Posted June 21, 2010 I have a script that resizes the image that is selected to upload and saves it into a folder. however i need it to create two different sized images at the same time and put them in separate folders. Ex. /photos/ and /photos/thumbs. Also, not very important, but would be helpful is resizing the image not only by the width, but by the height too, BUT keeping the aspect ratio. Here is my current code: //make sure this directory is writable! $path_thumbs = "photos"; //the new width of the resized image, in pixels. $img_thumb_width = 600; // $extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed) //List of allowed extensions if extlimit = yes $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp"); //the image -> variables $file_type = $_FILES['photo']['type']; $file_name = $_FILES['photo']['name']; $file_size = $_FILES['photo']['size']; $file_tmp = $_FILES['photo']['tmp_name']; //check if you have selected a file. if(!is_uploaded_file($file_tmp)){ //echo "<h3>Please select a file to upload.</h3>"; } //check the file's extension $ext = strrchr($file_name,'.'); $ext = strtolower($ext); //uh-oh! the file extension is not allowed! if (($extlimit == "yes") && (!in_array($ext,$limitedext))) { //echo "<h3>Filetype not allowed.</h3>"; } //so, whats the file's extension? $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; //create a random file name $rand_name = md5(time()); $rand_name= rand(0,999999999); //the new width variable $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); } //list the width and height and keep the height ratio. list($width, $height) = getimagesize($file_tmp); //calculate the image ratio $imgratio=$width/$height; if ($imgratio>0){ $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{ } //the resizing is going on here! imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //finally, save the image ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext"); ImageDestroy ($resized_img); ImageDestroy ($new_img); } //ok copy the finished file to the thumbnail directory move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext"); Quote Link to comment https://forums.phpfreaks.com/topic/205431-php-image-upload-resize-create-two-different-size-images-same-time/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 21, 2010 Share Posted June 21, 2010 Some reason you started a new thread for this same problem instead of sticking with your existing thread - http://www.phpfreaks.com/forums/index.php/topic,301775.0.html Quote Link to comment https://forums.phpfreaks.com/topic/205431-php-image-upload-resize-create-two-different-size-images-same-time/#findComment-1075067 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.