我有一个部分工作的功能来制作缩略图,但 10% 的图像没有创建为缩略图,而且它们是完全相同的 10%。其余 90% 有效。我不知道为什么。请看一下我的代码:
<?php
$image = "511photo.jpg";
if ($image) {
make_thumb("uploads", "thumbnails", $image, 500);
}
function make_thumb($imageFrom, $imageTo, $image, $thumbWidth) {
/* read the source image */
$getFrom = $imageFrom."/".$image;
$source_image = imagecreatefromjpeg($getFrom);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$thumbHeight = floor($height * ($thumbWidth / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($thumbWidth, $thumbHeight);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
/* create the physical thumbnail image to its destination */
$dest = $imageTo."/".$image;
imagejpeg($virtual_image, $dest);
} //end of function make_thumb($imageFrom, $imageTo, $image, $thumbWidth)
?>
注意:以下是其他几个不起作用的 $image:
"434cute-anime-couple-drawing-on-tumblr.png"
"503anime_head_vectorized_by_cona_cru-d784ls0.png"
注意:是的,我确信它们都在上传文件夹中 - 我已经检查过并仔细检查过,老实说,现在我很困惑......
最佳答案
这是因为您正在对 png
图像使用 imagecreatefromjpeg()
。您需要对这些图像使用imagecreatefrompng()
。
$source_image = imagecreatefrompng($getFrom);
要检查图像类型,您可以使用 exif_imagetype()功能:
$imageType = exif_imagetype($getFrom);
if($imageType == IMAGETYPE_PNG) {
//It's PNG
} elseif($imageType == IMAGETYPE_JPEG) {
//It's JPEG
} //You can check more types here.
关于php - 用于创建缩略图的部分工作功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34505303/