登录

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

php笔记 置顶
0 1456

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

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

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

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

1、save()方法,第98行。
  1. 原代码:
  2.  
  3. $fun  =   'image'.$type;
  4. $fun($this->img $imgname);
  5.  
  6. 修改为:
  7.  
  8. $fun  =   'image'.$type;
  9. imagesavealpha($this->img true);
  10. $fun($this->img $imgname);
2、crop()方法,第168行。
  1. 原代码:
  2.  
  3. $color = imagecolorallocate($img 255 255 255);
  4. imagefill($img 0 0 $color);
  5.  
  6. 修改为:
  7.  
  8. $color = imagecolorallocatealpha($img 0 0 0 127);
  9. imagefill($img 0 0 $color);
  10. imagesavealpha($img true);
3、thumb()方法,第266行。
  1. 原代码:
  2.  
  3. $color = imagecolorallocate($img 255 255 255);
  4.  
  5. 修改为:
  6.  
  7. $color = imagecolorallocatealpha($img 0 0 0 127);

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

发表评论

0 个回复

乐哥

乐哥

25 2020-06-17 加入 中国

(这个人懒得留下签名)

热门标签

友情连接