mahalleday Posted September 22, 2009 Share Posted September 22, 2009 I have a script the allows uploading of an image. Assuming the image is on the correct file type size etc. I then run a function to re size the image to something reasonable for the web ie 640x480. What I would like to do as well it create a thumbnail version of the image and save it in a separate folder ie /thumbs/. So far the only solution i have found was to create a copy of my imageResize function and call one after the other. This seems very redundant to me. Is there anyway I can reduce to code to use just one function even if i have to call it multiple times. This is the code I have now other than the names of the function they are identical. I should be able to get rid of one all together and just call one twice but can't get that to work. <?php $uploadir = 'images/'; imageReszie($file, &uploaddir, $maxsize, $quality); $uploadir = 'images/thumbs/'; makeThumb($file, &uploaddir, $maxsize, $quality); ?> I'll post more code tonight. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/175127-resize-and-create-thumbnails-from-uploaded-image/ Share on other sites More sharing options...
JonnoTheDev Posted September 22, 2009 Share Posted September 22, 2009 Call the 2 functions from 1 function using new parameters: <?php function abc($imagePath, $thumbPath, $file, $size, $quality) { imageReszie($file, $imagePath, $size, $quality); makeThumb($file, $thumbPath, $size, $quality); } // usage abc('images/','images/thumbs/',$file,$quality); ?> Also check your function names, you have spelling mistakes Quote Link to comment https://forums.phpfreaks.com/topic/175127-resize-and-create-thumbnails-from-uploaded-image/#findComment-922983 Share on other sites More sharing options...
kickstart Posted September 22, 2009 Share Posted September 22, 2009 Hi Or to loop around a directory something like this (excuse probably typos). You should check that any file is an image before just processing it (this basic script doesn't). And you would probably be best off resizing the images to keep the same ratio of width to height, rather than just using a fixed output size. <?php $idir = "'images/'"; $odir = "'images/thumbs/'"; // Open a known directory, and proceed to read its contents if (is_dir($idir)) { if ($dh = opendir($idir)) { while (($file = readdir($dh)) !== false) { // Load $thumb = imagecreatetruecolor(120, 90); $source = imagecreatefromjpeg($idir.$file); imagecopyresized($thumb, $source, 0, 0, 0, 0, 120, 90, imagesx($source), imagesx($source)); imagejpeg($thumb,$odir.$file); } closedir($dh); } } ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/175127-resize-and-create-thumbnails-from-uploaded-image/#findComment-923025 Share on other sites More sharing options...
mahalleday Posted September 22, 2009 Author Share Posted September 22, 2009 Call the 2 functions from 1 function using new parameters: <?php function abc($imagePath, $thumbPath, $file, $size, $quality) { imageReszie($file, $imagePath, $size, $quality); makeThumb($file, $thumbPath, $size, $quality); } // usage abc('images/','images/thumbs/',$file,$quality); ?> Also check your function names, you have spelling mistakes What you suggest is how i currently have it set up. It just seems redundant to have to functions that preform the same basic function. Perhaps it would be helpful to explain exactly what I am doing. I am in the process of building a classifieds ads ad-don for the cms I use. What i want/need is to allow users to upload a single image. this image should be re sized to some reasonable size for the web (640x480) then 2 thumbnails large and small need to be made. The small say 150 x something is for the main list of ads. the large say 300 x something would be displayed when you are looking at the detailed view of one particular ad. Right now my script runs the two functions as i have shown so I get the big image and one thumbnail. I need to make the third but don;t want to make a whole new function just for that. I am just looking for a more effecient way of doing this. I will post the code from my form validation and from the imagerezie and thumbanil functions tonight. Quote Link to comment https://forums.phpfreaks.com/topic/175127-resize-and-create-thumbnails-from-uploaded-image/#findComment-923188 Share on other sites More sharing options...
mahalleday Posted September 23, 2009 Author Share Posted September 23, 2009 Image resize function <?php //image resizing function function IMGresize ($file, $max_size, $quality, $imagepath, $ext) { //new name/path for the image $dst_image = $imagepath . $file; //get the width and height of origonal image list($ow, $oh) = getimagesize($imagepath . $file); //check if image is larger the $max_size if($ow > $max_size || $oh > $max_size) { //image dim(s) are larger therfore resize $ratio = $ow/$oh; //if width > height if($ow > $oh) { $nw = $max_size; $nh = $nw/$ratio; } //hegith > width else if($oh > $ow) { $nh = $max_size; $nw = $nh * $ratio; } //width and height are equal else { $oh = $max_size; $nh = $max_size; } } //the image is already an appropriate size else { $nw = $ow; $nh = $oh; }//end of image size check // Resize the original image $imageResized = imagecreatetruecolor($nw, $nh); if($ext == '.jpg' || $ext == '.jpeg') { $imageTmp = imagecreatefromjpeg ($imagepath . $file); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $nw, $nh, $ow, $oh); imagejpeg($imageResized, $dst_image, $quality); } elseif($ext == '.png') { $imageTmp = imagecreatefrompng($imagepath . $file); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $nw, $nh, $ow, $oh); imagepng($imageResized, $dst_image); } else{ exit(); } //destroy un-need images imagedestroy($imageResized); imagedestroy($imageTmp); } ?> Create ad function portion that calls image functions <?php //create ad function function create_ad($title, $cat, $subcat, $cur, $price, $ad_durr, $file_temp, $file, $desc, $user_id, $ext, $group_name, $page_id, $verify, $big, $thumb) { //set file upload directory $uploaddir = WB_PATH . '/modules/AdBaker/images/ads/'; if (move_uploaded_file($file_temp, $uploaddir . $file)) { //resize uploaded image IMGresize ($file, $big, 100, $uploaddir, $ext); makeTHUMB ($file, $thumb, 75, $uploaddir, $ext); list($ow, $oh) = getimagesize($uploaddir . $file); } else {$errors[] = 'There was a problem uploading your image please go back and try again';} ?> Form code that get image file and calls create ad function <?php //validate and handle file upload if present if(!empty($_FILES['image']['name'])) { $file_temp = $_FILES['image']['tmp_name']; $file = media_filename($_FILES['image']['name']); list($ow, $oh) = getimagesize($file_temp); $img_ratio = $ow/$oh; //set the allowed types of file extensions $extensions = $sett_value[6]; $allowed_extensions = explode(",", $extensions); //get extension $ext = strtolower(strrchr($file, '.')); if(in_array($ext, $allowed_extensions)) { //create new randomfile name $file = randomName().$ext; } else{$errors[] = '- '.$LANG[5]['TXT_ImageErr2'];} //check image file size if($_FILES['image']['size'] > 5242880) { $errors[] = '- '.$LANG[5]['TXT_ImageErr1']; } }else {$file = 'ad_img.png'; $file_temp = NULL; $ext = NULL;} //check for a description if(isset($_POST['description']) & !empty($_POST['description'])) { $desc = clean($_POST['description']); } else {$errors[] = '- '.$LANG[5]['TXT_DescErr'];} //handle form based on any errors that have occurred if(empty($errors)) { create_ad($title, $cat, $subcat, $cur, $price, $ad_durr, $file_temp, $file, $desc, $user_id, $ext, $group_name, $page_id, $sett_value[4], $big, $thumb); } else { foreach($errors as $msg) { echo '<p class="errorMsg">'.$msg.'</p>'; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/175127-resize-and-create-thumbnails-from-uploaded-image/#findComment-923245 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.