Finding the error - PHP
Fumph Oct 1, 2013 12:20 AMHi there, as part of a bigger project, I need to copy and resize some pictures. It'd be great if someone could tell me where I'm going wrong! I've got 100+ pictures in the folder "photoframe" and they need to be scaled, compressed, and then copied to the folder "cache." The foreach loop gets to 24 pictures and then stops! Why is this?
function cache_old() {
$imgs = glob("photoframe/*.{jpg,png,gif,JPG,PNG,GIF}",GLOB_BRACE);
foreach ($imgs as $image) {
// Set a maximum height and width
$width = 200;
$height = 1000;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($image);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
switch (strtolower(pathinfo($image,PATHINFO_EXTENSION))) {
case "jpg":
case "jpeg":
$image_c = imagecreatefromjpeg ($image);
break;
case "png":
$image_c = imagecreatefrompng($image);
break;
case "gif":
$image_c = imagecreatefromgif($image);
break;
}
imagecopyresampled($image_p, $image_c, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
$saveAs = "cache/thumb_".basename($image);
switch (strtolower(pathinfo($image,PATHINFO_EXTENSION))) {
case "jpg":
case "jpeg":
imagejpeg($image_p, $saveAs,50);
break;
case "png":
imagepng($image_p, $saveAs,5);
break;
case "gif":
imagegif($image_p, $saveAs);
break;
}
echo "Resampled ".$saveAs." as a ".strtolower(pathinfo($image,PATHINFO_EXTENSION)).". Dimensions: ".$width."x".$height."<br>";
}
}
cache_old();
In an attempt to get things woking, I've copied other code, twisted things up, and still, only 24 pictures copied. The 24 that do get copied are perfect! I'm open to anything, I probably didn't read the fine print in the functions or something simple like that. Renaming files and moving files doesn't affect anything, and resampling individual files past the 24th image work fine.
Each picture is roughly 500kB-3mB large, and I'm looking to get each picture 200 pixels wide, keeping the aspect ratio, but slimming down the size a lot! Quality isn't a objection here, the copied versions will be thumbnail-like.
Thank you all!



