deansatch Posted July 6, 2009 Share Posted July 6, 2009 I am using readdir in a while loop to convert a folder full of jpg images to square thumbnails. If I set $source as the name of one file and run the script it creates a perfect square thumbnail. If I run it as it is to do all the images, it creates square thumbnails but crops them wrong as if it is taking the size info for one image from the previous or next image. if($handle = opendir('act-gallery')){ while (false !== ($source = readdir($handle))) { if ($source != "." && $source != ".." && $source != "thumbs") { //echo $source."<br />"; $dest2 = "act-gallery/thumbs/".$source; $thumb_size = 132; $size = getimagesize('act-gallery/'.$source); $width = $size[0]; $height = $size[1]; if($width > $height) { $x = ceil(($width - $height) / 2 ); $width = $height; } elseif($height > $width) { $y = ceil(($height - $width) / 2); $height = $width; } $im = imagecreatefromjpeg('act-gallery/'.$source); $new_im = ImageCreatetruecolor($thumb_size,$thumb_size); imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height); imagejpeg($new_im,$dest2,100); imagedestroy($new_im); } } closedir($handle); } This works:(but only one image at a time.) $source= "image1.jpg"; $dest2 = "act-gallery/thumbs/".$source; $thumb_size = 132; $size = getimagesize('act-gallery/'.$source); $width = $size[0]; $height = $size[1]; if($width > $height) { $x = ceil(($width - $height) / 2 ); $width = $height; } elseif($height > $width) { $y = ceil(($height - $width) / 2); $height = $width; } $im = imagecreatefromjpeg('act-gallery/'.$source); $new_im = ImageCreatetruecolor($thumb_size,$thumb_size); imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height); imagejpeg($new_im,$dest2,100); imagedestroy($new_im); Link to comment https://forums.phpfreaks.com/topic/164930-solved-readdir-loop-acting-strange/ Share on other sites More sharing options...
seventheyejosh Posted July 6, 2009 Share Posted July 6, 2009 if $size is getting carried over, you can just try unset($size); at the bottom of each loop before the next iteration. Link to comment https://forums.phpfreaks.com/topic/164930-solved-readdir-loop-acting-strange/#findComment-869704 Share on other sites More sharing options...
deansatch Posted July 6, 2009 Author Share Posted July 6, 2009 Aha! Thanks, that got me thinking! I just had to put $x=0; $y=0; before and at the end of the loop. Link to comment https://forums.phpfreaks.com/topic/164930-solved-readdir-loop-acting-strange/#findComment-869729 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.