Hi,
I am new to PHP and am having trouble debugging a script. I am trying to develop a Thumbnail Generator for a back end of a website. Currently the generator will work as its supposed to with PNG images. It will read the selected parent directory and make a thumbs folder within it. With all the correct associated thumbnails. If I try to execute it with a folder of JPG images it will create the thumbs folder, convert the first JPG then stop. any ideas would be appreciated.
[php]
';
echo '
';
}
function warning($message)
{
echo '' . $message . '×';
echo '
';
echo '
';
}
function imageCreateFromAny($filepath) {
$type = exif_imagetype($filepath); // [] if you don't have exif you could use getImageSize()
$allowedTypes = array(
1, // [] gif
2, // [] jpg
3, // [] png
6 // [] bmp
);
if (!in_array($type, $allowedTypes)) {
return false;
}
switch ($type) {
case 1 :
$im = imageCreateFromGif($filepath);
break;
case 2 :
$im = imageCreateFromJpeg($filepath);
break;
case 3 :
$im = imageCreateFromPng($filepath);
break;
case 6 :
$im = imageCreateFromBmp($filepath);
break;
}
return $im;
}
function createThumbnail($imageDirectory, $thumbDirectory, $thumbWidth)
{
$files = glob($imageDirectory.'/*.{jpg,png,gif}', GLOB_BRACE);
echo '' . $message . '×';
echo '
Thumbnails Generated:
'; if(! empty( $files) ) { foreach($files as $file) { echo ''.$file . '
'; $srcImg = @imagecreatefromAny($file); $origWidth = imagesx($srcImg); $origHeight = imagesy($srcImg); $ratio = $origHeight/ $origWidth; $thumbHeight = $thumbWidth * $ratio; $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight); if (!file_exists($imageDirectory."/thumbs/")) { mkdir($imageDirectory."/thumbs/", 0777, true); } imagejpeg($thumbImg, $imageDirectory."/thumbs/".basename($file)); } echo "
"; alert("Thumbnails Generated Successfully"); } else { echo "
"; warning("No images in directory!"); } } if (isset($_POST['createThumbnails'])) { createThumbnail($_POST['imgDir'],$_POST['imgThumbDir'],$_POST['imgWidth']); } ?>
[/php]
Thanks Again!