public static function makeThumb($image, $thumbName, $type = null, $width = 100, $height = 50, $isAutoFix = true)
{
is_dir(dirname($thumbName)) || mkdir(dirname($thumbName), 0700, true);
$imageInfo = self::getImageInfo($image);
if (!$imageInfo) {
return false;
}
$type = is_null($type) ? strtolower($imageInfo['ext']) : strtolower($type == 'jpg' ? 'jpeg' : $type);
if ($isAutoFix) {
//根据比例缩放
$fixScale = min($width / $imageInfo['width'], $height / $imageInfo['height']);
//缩放的比例
if ($fixScale > 1) {
//缩略图超过原图大小
$width = $imageInfo['width'];
$height = $imageInfo['height'];
} else {
$width = $imageInfo['width'] * $fixScale;
$height = $imageInfo['height'] * $fixScale;
}
} else {
$width > $imageInfo['width'] && ($width = $imageInfo['width']);
$height > $imageInfo['height'] && ($height = $imageInfo['height']);
}
if (!in_array($type, ['jpeg', 'gif', 'png'])) {
return false;
}
$createImageFunc = "imagecreatefrom{$type}";
$sourceCreateImage = $createImageFunc($image);
//载入原图
//生成缩略图
if ($type == 'gif' || !function_exists('imagecreatetruecolor')) {
$thumbImg = imagecreate($width, $height);
} else {
$thumbImg = imagecreatetruecolor($width, $height);
}
if ($type == 'png') {
imagealphablending($thumbImg, false);
//取消默认的混色模式(为解决阴影为绿色的问题)
imagesavealpha($thumbImg, true);
//设定保存完整的 alpha 通道信息(为解决阴影为绿色的问题)
} elseif ($type == 'gif') {
$transparentIndex = imagecolortransparent($sourceCreateImage);
if ($transparentIndex >= 0) {
$transparentColor = imagecolorsforindex($sourceCreateImage, $transparentIndex);
$transparentIndex = imagecolorallocate($thumbImg, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']);
imagefill($thumbImg, 0, 0, $transparentIndex);
imagecolortransparent($thumbImg, $transparentIndex);
}
}
//复制
function_exists('imagecopyresampled') ? imagecopyresampled($thumbImg, $sourceCreateImage, 0, 0, 0, 0, $width, $height, $imageInfo['width'], $imageInfo['height']) : imagecopyresized($thumbImg, $sourceCreateImage, 0, 0, 0, 0, $width, $height, $imageInfo['width'], $imageInfo['height']);
self::output($thumbImg, $type, $thumbName);
imagedestroy($sourceCreateImage);
return $thumbName;
}