samoi Posted November 24, 2009 Share Posted November 24, 2009 Hello guys! I need to make a script that makes thumbnail for pictures in a directory! the idea might be handled by a cron job script and will be developed later on! But what I really need is, say I have 200 picture of my vacation trip! and I need to make thumbnails for those picture to use it with jquery for example! so I need to upload my picture in the 'x' directory, and then run the script inside the directory to do thumbnail for all of the pictures at once! again, the point is: i need the script to do it all at once when it runs! not uploading picture by picture. please note, the credits of most of the following code is for cafewebmaster.com! here is the code: <?php /** * Smart Image Uploader by @cafewebmaster.com * Free for private use * Please support us with donations or backlinks */ $desired_extension = 'jpg'; //extension we're looking for $dirname = "/Applications/MAMP/htdocs/imgs"; $dir = opendir($dirname); $num_files = 0; while(false != ($file = readdir($dir))) { if(($file != ".") and ($file != "..")) { $fileChunks = explode(".", $file); if($fileChunks[1] == $desired_extension) //interested in second chunk only { echo '<a href="../imgs/'.$file.'"> '.$file.'</a> || '; $num_files++; } } } closedir($dir); $upload_image_limit = $num_files; // How many images you want to upload at once? $upload_dir = "/Applications/MAMP/htdocs/tm/thumb/"; // default script location, use relative or absolute path $enable_thumbnails = 1 ; // set 0 to disable thumbnail creation ##################### THUMBNAIL CREATER FROM GIF / JPG / PNG function make_thumbnails($updir, $img){ $thumbnail_width = 130; $thumbnail_height = 100; $thumb_preword = "T"; $arr_image_details = GetImageSize("$updir"."$img"); $original_width = $arr_image_details[0]; $original_height = $arr_image_details[1]; if( $original_width > $original_height ){ $new_width = $thumbnail_width; $new_height = intval($original_height*$new_width/$original_width); } else { $new_height = $thumbnail_height; $new_width = intval($original_width*$new_height/$original_height); } $dest_x = intval(($thumbnail_width - $new_width) / 2); $dest_y = intval(($thumbnail_height - $new_height) / 2); if($arr_image_details[2]==1) { $imgt = "ImageGIF"; $imgcreatefrom = "ImageCreateFromGIF"; } if($arr_image_details[2]==2) { $imgt = "ImageJPEG"; $imgcreatefrom = "ImageCreateFromJPEG"; } if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG"; } if( $imgt ) { $old_image = $imgcreatefrom("$updir"."$img"); $new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height); imageCopyResized($new_image,$old_image,$dest_x, $dest_y,0,0,$new_width,$new_height,$original_width,$original_height); $imgt($new_image,"$updir"."$thumb_preword"."$img"); } } ################################# UPLOAD IMAGES foreach($_FILES as $k => $v){ $img_type = ""; ### $htmo .= "$k => $v<hr />"; ### print_r($_FILES); if( !$_FILES[$k]['error'] && preg_match("#^image/#i", $_FILES[$k]['type']) && $_FILES[$k]['size'] < 1000000){ $img_type = ($_FILES[$k]['type'] == "image/jpeg") ? ".jpg" : $img_type ; $img_type = ($_FILES[$k]['type'] == "image/gif") ? ".gif" : $img_type ; $img_type = ($_FILES[$k]['type'] == "image/png") ? ".png" : $img_type ; $img_rname = $_FILES[$k]['name']; $img_path = $upload_dir.$img_rname; copy( $_FILES[$k]['tmp_name'], $img_path ); if($enable_thumbnails) make_thumbnails($upload_dir, $img_rname); $feedback .= "Image and thumbnail created $img_rname<br />"; } } ############################### HTML FORM while($i++ < $upload_image_limit){ $form_img .= '<label>Image '.$i.': </label> <input type="file" name="uplimg'.$i.'" ><br />'; #value="'.$dirname.'/jnon-'.$i'.jpg" } $htmo .= ' <p>'.$feedback.'</p> <form method="post" enctype="multipart/form-data"> '.$form_img.' <br /> <input type="submit" value="Upload Images!" style="margin-left: 50px;" /> Powered by <a href="http://cafewebmaster.com">Cafe Webmaster</a> </form> '; echo $htmo; ?> Help will be appreciated ! Quote Link to comment https://forums.phpfreaks.com/topic/182729-help-thumbnailing-pictures/ Share on other sites More sharing options...
samoi Posted November 24, 2009 Author Share Posted November 24, 2009 Any suggestions ? help? Quote Link to comment https://forums.phpfreaks.com/topic/182729-help-thumbnailing-pictures/#findComment-964452 Share on other sites More sharing options...
samoi Posted November 24, 2009 Author Share Posted November 24, 2009 Please help me out! Quote Link to comment https://forums.phpfreaks.com/topic/182729-help-thumbnailing-pictures/#findComment-964628 Share on other sites More sharing options...
Yesideez Posted November 24, 2009 Share Posted November 24, 2009 Personally I'd avoid running something like this on a server as resizing images takes too much processing power from the server. Instead, Google "Oscar's jpeg thumb maker" and create your thumbnails with that and upload thumbs and originals together. Quote Link to comment https://forums.phpfreaks.com/topic/182729-help-thumbnailing-pictures/#findComment-964630 Share on other sites More sharing options...
samoi Posted November 24, 2009 Author Share Posted November 24, 2009 Personally I'd avoid running something like this on a server as resizing images takes too much processing power from the server. Instead, Google "Oscar's jpeg thumb maker" and create your thumbnails with that and upload thumbs and originals together. Thank you for the advice and reply! will that thing do so for all pictures at once? Quote Link to comment https://forums.phpfreaks.com/topic/182729-help-thumbnailing-pictures/#findComment-964633 Share on other sites More sharing options...
samoi Posted November 24, 2009 Author Share Posted November 24, 2009 oh! that's a software for PC ! I got mac ! I thought it was a website ! Quote Link to comment https://forums.phpfreaks.com/topic/182729-help-thumbnailing-pictures/#findComment-964635 Share on other sites More sharing options...
Yesideez Posted November 24, 2009 Share Posted November 24, 2009 I use it a lot and all you need to do is set up how you want it to behave when making the thumbnails, point it to a folder containg the original images, point it to a folder where you want the thumbnails to go and click start. It'll go through the folder and convert every picture it finds into a thumbnail. Ah - you have a Mac! Sorry, not much use for you then unless you can find a similar Mac version out there - apologies for presuming you had a Windows machine! Quote Link to comment https://forums.phpfreaks.com/topic/182729-help-thumbnailing-pictures/#findComment-964638 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.