php 裁剪png图片,解决thinkphp裁剪png图片背景不透明的方法

在使用thinkphp对图片处理的时候,对于png图片的处理比如裁剪,缩略图,都会出现背景为白色,不是透明的情况。

这里只是使用了thinkphp3.2.3版本,默认GD库处理,其他的类库自行解决哦。

下面我们看,thinkphp裁剪png图片背景不透明的问题怎么解决?

其实,解决起来也是很简单的,只要将文件ThinkPHP/Library/Think/Image/Driver/GD.class.php修改其中的三个方法即可。

1、save()方法,第98行。原代码:

$fun  =   'image'.$type;

$fun($this->img, $imgname);

修改为:

$fun  =   'image'.$type;

imagesavealpha($this->img, true);

$fun($this->img, $imgname);

2、crop()方法,第168行。原代码:

$color = imagecolorallocate($img, 255, 255, 255);

imagefill($img, 0, 0, $color);

修改为:

$color = imagecolorallocatealpha($img, 0, 0, 0, 127);

imagefill($img, 0, 0, $color);

imagesavealpha($img, true);

3、thumb()方法,第266行。原代码:

$color = imagecolorallocate($img, 255, 255, 255);

修改为:

$color = imagecolorallocatealpha($img, 0, 0, 0, 127);

通过修改上面三处方法,即可实现thinkphp裁剪png图片支持透明的效果了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值