public function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG/PNG/BMP/GIF files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' || strtolower($info['extension']) == 'png' || strtolower($info['extension']) == 'gif' || strtolower($info['extension']) == 'bmp' )
{
// echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
switch (imagetypes()) {
case IMG_JPG:
$img = imagecreatefromjpeg( "{$pathToImages}/{$fname}" );
break;
case IMG_PNG:
$img = imagecreatefrompng( "{$pathToImages}/{$fname}" );
break;
case IMG_GIF:
$img = imagecreatefromgif( "{$pathToImages}/{$fname}" );
break;
case IMG_WBMP:
$img = imagecreatefromwbmp( "{$pathToImages}/{$fname}" );
break;
case IMG_XPM:
$img = imagecreatefromxpm( "{$pathToImages}/{$fname}" );
break;
default:
$img = imagecreatefromxbm( "{$pathToImages}/{$fname}" );
break;
}
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$newWidth = $thumbWidth;
$newHeight = floor( $height * ( $thumbWidth / $width ) );
// create a new tempopary image
$tmpImg = imagecreatetruecolor( $newWidth, $newHeight );
// copy and resize old image into new image
imagecopyresized( $tmpImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height );
// save thumbnail into a file
switch (imagetypes()) {
case IMG_JPG:
imagejpeg( $tmpImg, "{$pathToThumbs}/{$fname}" );
break;
case IMG_PNG:
imagepng( $tmpImg, "{$pathToThumbs}/{$fname}" );
break;
case IMG_GIF:
imagegif( $tmpImg, "{$pathToThumbs}/{$fname}" );
break;
case IMG_WBMP:
imagewbmp( $tmpImg, "{$pathToThumbs}/{$fname}" );
break;
case IMG_XPM:
imagegd( $tmpImg, "{$pathToThumbs}/{$fname}" );
break;
default:
imagegd2( $tmpImg, "{$pathToThumbs}/{$fname}" );
break;
}
}
}
// close the directory
closedir( $dir );
}