我正在尝试创建图像的缩略图,但我发现使用下面的代码只能将 JPG 图像转换为缩略图;它不适用于 PNG 和 GIF。我究竟做错了什么?
function createThumbs( $pathToImages,$pathToThumbs,$thumbWidth,$fname)
{
// open the directory
// $dir = opendir( $pathToImages );
// parse path for the extension
$info = pathinfo($pathToImages);
// continue only if this is a JPEG image
//if ( strtolower($info['extension']) == 'jpg' )
//{
//echo "Creating thumbnail for {$fname} <br />";
echo strtolower($info['extension']);
switch(strtolower($info['extension'])){
case 'jpg':
$img = imagecreatefromjpeg("{$pathToImages}" );
break;
case 'gif':
$img = imagecreatefromgif("{$pathToImages}" );
break;
case 'png':
$img = imagecreatefrompng("{$pathToImages}" );
break;
default:
die("ERROR: FILE TYPE DOES NOT SUPPORT");
break;
}
// load image and get image size
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
chmod("{$pathToThumbs}{$fname}",777);
}
}