mahalleday Posted December 20, 2009 Share Posted December 20, 2009 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. Quote Link to comment Share on other sites More sharing options...
nafetski Posted December 20, 2009 Share Posted December 20, 2009 Nyquil is starting to kick in tonight - so I'm a bit fuzzy 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! Quote Link to comment Share on other sites More sharing options...
mahalleday Posted December 21, 2009 Author Share Posted December 21, 2009 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 Quote Link to comment Share on other sites More sharing options...
akitchin Posted December 21, 2009 Share Posted December 21, 2009 is display_errors set to "On" and the error_reporting level set to the max (E_ALL)? if not, try turning those on and see if you spot an error at that point. Quote Link to comment 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.