用Image类给透明背景图片添加文字丢失透明背景的解决办法

浏览:2395 发布日期:2016/01/20 分类:技术分享 关键字: Image 图像处理
今天用Image类给一个透明背景的png添加文字水印,结果保存后的图片透明背景变白色了,然后就找资料学习,线分享如下:
版本:thinkphp 3.2
前提:采用的Gd库
找到Gd.class.php文件, 路径:ThinkPHP/Library/Think/Image/Driver/Gd.class.php
将类中的save方法替换如下:/**
     * 保存图像
     * @param  string  $imgname   图像保存名称
     * @param  string  $type      图像类型
     * @param  integer $quality   图像质量     
     * @param  boolean $interlace 是否对JPEG类型图像设置隔行扫描
     */
    public function save($imgname, $type = null, $quality=80,$interlace = true){
        if(empty($this->img)) E('没有可以被保存的图像资源');

        //自动获取图像类型
        if(is_null($type)){
            $type = $this->info['type'];
        } else {
            $type = strtolower($type);
        }
        //保存图像
        if('jpeg' == $type || 'jpg' == $type){
            //JPEG图像设置隔行扫描
            imageinterlace($this->img, $interlace);
            imagejpeg($this->img, $imgname,$quality);
        }elseif('gif' == $type && !empty($this->gif)){
            $this->gif->save($imgname);
        }else{
            // 保存透明色
            imagesavealpha($this->img, true);
            $fun  =   'image'.$type;
            $fun($this->img, $imgname);
        }
    }
仅仅在上面代码的26行,添加了如下代码:// 保存透明色
imagesavealpha($this->img, true);
分享给有同样需求的童鞋们,有什么不良反应,请大家指正,谢谢!
最佳答案
评论( 相关
后面还有条评论,点击查看>>