我正在使用 php 创建缩略图,它适用于 GIF 和 JPEG,但不适用于 PNG。当我使用 PNG 运行此脚本时,不会保存缩略图。你能告诉我我做错了什么吗?提前致谢!
public function make_thumbs($img_src)
{
//Desired thumbnail width
$width = 100;
//Thumbnail name
$thumb = 'th_' . $img_src;
//Ensure the image exists
if(file_exists($img_src)){
if (exif_imagetype($img_src) == IMAGETYPE_GIF)
{
//Create image stream
$image = imagecreatefromgif($img_src);
}
elseif(exif_imagetype($img_src) == IMAGETYPE_JPEG)
{
//Create image stream
$image = imagecreatefromjpeg($img_src);
}
elseif(exif_imagetype($img_src) == IMAGETYPE_PNG)
{
//Create image stream
$image = imagecreatefrompng($img_src);
}
//Gather and store the width and height
list($image_width, $image_height) = getimagesize($img_src);
//Calculate new height while maintaining aspect ratio
$height = (($width / $image_width) * $image_height);
//Resample/resize the image
$tmp_img = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp_img, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
//Attempt to save the new thumbnail
if(is_writeable(dirname($thumb)))
{
if (exif_imagetype($img_src) == IMAGETYPE_GIF)
{
imagegif($tmp_img, $thumb, 100);
}
elseif(exif_imagetype($img_src) == IMAGETYPE_JPEG)
{
imagejpeg($tmp_img, $thumb, 100);
}
elseif(exif_imagetype($img_src) == IMAGETYPE_PNG)
{
imagepng($tmp_img, $thumb, 100);
}
}
//clear memory
imagedestroy($tmp_img);
imagedestroy($image);
}
}