Jump to content

Creating thumbnails form a folder on images


mahalleday

Recommended Posts

I am trying to write a php function to loop through a directory of images, take only the images that have approved extensions and create a thumbnail of each image under a 'thumbs' subdirectory.

 

Here is the code to call the function:

 

<?php
//include functions
    include('./functions.php');
//set function parameters
    $extensions = array('jpg', 'jpeg', 'png', 'gif');
    $dir = './media/';
    $thumbDir = $dir.'/thumbs/';
    $max_size = 100;
    $quality = 75;
    $imagepath = $dir;
//create thumbnails
    createThumbs( $dir, $thumbDir, $max_size, $extensions, $quality )
?>

Here is the function:

<?php
function createThumbs($imagepath, $thumbDir, $max_size, $extensions, $quality) {
// open the directory
$dir = opendir( $imagepath );

// loop through it, looking for any/all image files:
while (false !== ($file = readdir( $dir ))) {
   if ($file != "." && $file != "..") {
  if(strstr($file,'.')) {
	  $fileChunks = explode(".", $file);
	  $ext = $fileChunks[1];
	  if(in_array($ext, $extensions)) {
		  echo "Creating thumbnail for {$file} <br />";
	  //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, $thumbDir, $quality);
			}
			elseif($ext == '.png') {
			  $imageTmp = imagecreatefrompng($imagepath . $file);
			  imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $nw, $nh, $ow, $oh);
			  imagepng($imageResized, $thumbDir);
			}
			else{
				  exit();
			}
		//destroy un-need images
			imagedestroy($imageResized);
			imagedestroy($imageTmp);
	  }
  }
   }
}//end while
// close the directory
closedir( $dir );
}
?>

 

I receive no errors when i run the function it only echos out 'echo "Creating thumbnail for pic01.jpg" then stops running.  It makes no thumbnails not even for pic01.jpg.

 

Any help on this would be greatly appreciated.

Link to comment
Share on other sites

Nyquil is starting to kick in tonight - so I'm a bit fuzzy :P

 

Couple things I see.  (not on my dev box, so unfortunately can't test any of this)

 

1)  When using any of the imagecreatefrom functions, you don't need to do a imagecreattrue color.

2)  When you call the imagejpeg/imagepng function - you want to supply not only the path, but also the full filename as well.

3)  When determining which type of image creation function you are going to use, you can just do a strpos call (instead of exploding the string to get the image extension.

4)  Start from the top, and work your way down.  Start with just echoing out all the filenames.  Then try just calling the image creation function and seeing if you can copy the image from src to another directory.  Also make sure that you have write permissions on the image directory you are trying to write to.  My first guess is that you're not giving imagejpg a full filename and it's just not writing correctly :)

 

Hopefully something there helped!

Link to comment
Share on other sites

I went though the code top to bottom commenting out sections and echoing out stuff to see what was and wasn't working.  The problem seems to be at image manipulation stage.  I have tried rewriting this a few time even switching to 'imagecopyresized'  which i would assume would be more appropriate for this kind of application anyways.  And I still can't get it to work.  I have to admit I ma pretty green when it comes to image manipulation in php, but I have written scripts to do this sort of thing in the past (image upload script), so I am kinda stumped here.

 

Mike

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.