Jump to content

[SOLVED] readdir() loop acting strange


deansatch

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.