From 5dd33243f4084a99881ac04cd6dac0e65f4bd3bc Mon Sep 17 00:00:00 2001 From: chulaixi Date: Sun, 22 Nov 2020 22:32:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=9D=E7=95=99=E5=9B=BE=E7=89=87=E9=80=8F?= =?UTF-8?q?=E6=98=8E=E8=83=8C=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加保留图片透明背景保留的处理。 --- extend/base/Images.php | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/extend/base/Images.php b/extend/base/Images.php index e2c969685..c3b097d7e 100755 --- a/extend/base/Images.php +++ b/extend/base/Images.php @@ -319,17 +319,54 @@ class Images /* 新建一个真彩色图像 */ $dst_im = imagecreatetruecolor($new_width, $new_height); - /* 是否裁剪图片 */ + /* 保留透明背景 */ + switch($type) + { + case 'gif': + case 'png': + /* 保存完整alpha通道信息 */ + imagesavealpha($dst_im, true); + /* 上色 */ + $color = imagecolorallocatealpha($dst_im, 0, 0, 0, 127); + /* 设置透明色 */ + imagecolortransparent($dst_im, $color); + /* 填充透明色 */ + imagefill($dst_im, 0, 0, $color); + break; + default:; + } + + /* 是否裁剪图片 */ if($src_width > 0 && $src_height > 0) { /* 新建拷贝大小的真彩图像 */ $cpd_im = imagecreatetruecolor($src_width, $src_height); + /* 保留透明背景 */ + switch($type) + { + case 'gif': + case 'png': + /* 保存完整alpha通道信息 */ + imagesavealpha($cpd_im, true); + /* 上色 */ + $color = imagecolorallocatealpha($cpd_im, 0, 0, 0, 127); + /* 设置透明色 */ + imagecolortransparent($cpd_im, $color); + /* 填充透明色 */ + imagefill($cpd_im, 0, 0, $color); + break; + default:; + } + /* 拷贝图片 */ imagecopy($cpd_im, $src_im, 0, 0, $src_x, $src_y, $src_width, $src_height); /* 图片缩放 */ $s = imagecopyresampled($dst_im, $cpd_im, 0, 0, 0, 0, $new_width, $new_height, $src_width, $src_height); + + /* 销毁画布 */ + imagedestroy($cpd_im); } else { /* 图片缩放 */ $s = imagecopyresampled($dst_im, $src_im, 0, 0, 0, 0, $new_width, $new_height, $w, $h);