Jump to content

Help thumbnailing pictures!


samoi

Recommended Posts

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 !

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.