缩略图支持webp

pull/18/head
icret 2022-01-05 01:19:42 +08:00
parent 341cf72709
commit 7e581563a7
1 changed files with 88 additions and 57 deletions

View File

@ -18,7 +18,8 @@
// namespace Thumb;
// https://github.com/aileshe/Thumb
class Thumb{
class Thumb
{
private static $img_type; # 原图片格式
/**
@ -28,9 +29,10 @@ class Thumb{
* @param String $height 预生成缩略图高度
* @param String $valign [middle|top|bottom],默认 居中
*/
public static function show($filename, $width, $height, $valign='middle'){
public static function show($filename, $width, $height, $valign = 'middle')
{
$thumb = self::make($filename, $width, $height, $valign);
header('Content-Type:image/'.self::$img_type);
header('Content-Type:image/' . self::$img_type);
echo $thumb;
}
@ -42,10 +44,11 @@ class Thumb{
* @param String $height 预生成缩略图高度
* @param String $valign [middle|top|bottom],默认 居中
*/
public static function out($filename, $output, $width, $height, $valign='middle'){
public static function out($filename, $output, $width, $height, $valign = 'middle')
{
$thumb = self::make($filename, $width, $height, $valign);
$fh = fopen($output, 'wb'); # 二进制文件
fwrite($fh,$thumb);
fwrite($fh, $thumb);
fclose($fh);
}
@ -57,12 +60,13 @@ class Thumb{
* @param String $height 预生成缩略图高度
* @param String $valign [middle|top|bottom],默认 居中
*/
public static function showOut($filename, $output, $width, $height, $valign='middle'){
public static function showOut($filename, $output, $width, $height, $valign = 'middle')
{
$thumb = self::make($filename, $width, $height, $valign);
$fh = fopen($output, 'wb'); # 二进制文件
fwrite($fh,$thumb);
fwrite($fh, $thumb);
fclose($fh);
header('Content-Type:image/'.self::$img_type);
header('Content-Type:image/' . self::$img_type);
echo $thumb;
}
@ -74,62 +78,89 @@ class Thumb{
* @param String $valign [middle|top|bottom],默认 居中
* @return FileString 原始图象流
*/
private static function make($filename,$width,$height,$valign='middle'){
ini_set('gd.jpeg_ignore_warning',true);
$filetype=array(1=>'gif',2=>'jpeg',3=>'png');
private static function make($filename, $width, $height, $valign = 'middle')
{
ini_set('gd.jpeg_ignore_warning', true);
/**
* 2022-1-4 23:34:37 EasyImage更改图像类型判断
* getimagesize索引2给出的是图像的类型返回的是数字
* 1 = GIF2 = JPG3 = PNG4 = SWF5 = PSD6 = BMP7 = TIFF(intel byte order)8 = TIFF(motorola byte order)
* 9 = JPC10 = JP211 = JPX12 = JB213 = SWC14 = IFF15 = WBMP16 = XBM
*/
$filetype = array(
0 => 'unknown',
1 => 'gif',
2 => 'jpeg',
3 => 'png',
4 => 'swf',
5 => 'psd',
6 => 'bmp',
7 => 'tif_ii',
8 => 'tiff_mm',
9 => 'jpc',
10 => 'jp2',
11 => 'jpx',
12 => 'jb2',
13 => 'swc',
14 => 'iff',
15 => 'wbmp',
16 => 'xbm',
17 => 'ico',
18 => 'webp'
);
# 获取图片信息
$imginfo=getimagesize($filename);
$img_w=$imginfo[0];
$img_h=$imginfo[1];
self::$img_type=$filetype[$imginfo[2]];
$imginfo = getimagesize($filename);
$img_w = $imginfo[0];
$img_h = $imginfo[1];
self::$img_type = $filetype[$imginfo[2]];
$thumb_h=$height; # 固定背景画布的高度
$height=$img_h/($img_w/$width); # 图片等比例缩放后的高度=原图的高度÷(原图的宽度÷背景画布固定宽带)
$thumb_h = $height; # 固定背景画布的高度
$height = $img_h / ($img_w / $width); # 图片等比例缩放后的高度=原图的高度÷(原图的宽度÷背景画布固定宽带)
# 创建新的背景画布
if($height>=$thumb_h){
$thumb=imagecreatetruecolor($width,$thumb_h);
if ($height >= $thumb_h) {
$thumb = imagecreatetruecolor($width, $thumb_h);
@imagealphablending($thumb, false); //这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;2-1
@imagesavealpha($thumb, true); //这里很重要,意思是不要丢了$thumb图像的透明色;2-2 EasyImage修改
}else{
$thumb=imagecreatetruecolor($width,$height);
} else {
$thumb = imagecreatetruecolor($width, $height);
@imagealphablending($thumb, false); //这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;2-1
@imagesavealpha($thumb, true); //这里很重要,意思是不要丢了$thumb图像的透明色;2-2 EasyImage修改
$thumb_h=$height;
$thumb_h = $height;
}
# 载入要缩放的图片
$loadimg='imagecreatefrom'.self::$img_type;
$tmp_img=$loadimg($filename);
$loadimg = 'imagecreatefrom' . self::$img_type;
$tmp_img = $loadimg($filename);
switch($valign){
case 'top':{
$dst_y=0;
switch ($valign) {
case 'top': {
$dst_y = 0;
break;
}
case 'middle':{
$dst_y=($img_h-$img_w/$width*$thumb_h)/2;
case 'middle': {
$dst_y = ($img_h - $img_w / $width * $thumb_h) / 2;
break;
}
case 'bottom':{
$dst_y=$img_h-$img_w/$width*$thumb_h;
case 'bottom': {
$dst_y = $img_h - $img_w / $width * $thumb_h;
break;
}
default:{
$dst_y=0;
default: {
$dst_y = 0;
break;
}
}
# 合成缩略图
imagecopyresampled($thumb,$tmp_img,0,0,0,$dst_y,$width,$height, $img_w, $img_h);
imagecopyresampled($thumb, $tmp_img, 0, 0, 0, $dst_y, $width, $height, $img_w, $img_h);
ob_clean();
# 展示图片
ob_start();
$showimg='image'.self::$img_type;
$showimg = 'image' . self::$img_type;
$showimg($thumb); # 输出原始图象流
$thumb_img = ob_get_clean();