博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用PHP GD库为一张图片创建多个水印,缩放..
阅读量:6993 次
发布时间:2019-06-27

本文共 5832 字,大约阅读时间需要 19 分钟。

  hot3.png

  目标:1、主:一张图片添加多个水印  ok

            2、副:水印图片可缩放、旋转      no
           3、加完水印可压缩            ok

getImgInfo($bpath); $this->bpath = $bpath; $this->bw = $bres['w']; $this->bh = $bres['h']; $this->btype = $bres['type']; } /** * 根据图片类型创建对象 * @param string 图片类型 * @return obj */ public function createObj($path) { $info = $this->getImgInfo($path); $type = $info['type']; $func = 'imagecreatefrom'.$type; return $func($path); } /** * 获取图片尺寸 * @param [type] $path [description] * @return [type] [w:宽, h:高,type :类型] */ public function getImgInfo($path) { $info = getimagesize($path); $res['w'] = $info[0]; $res['h'] = $info[1]; $res['type'] = image_type_to_extension($info[2],false); return $res; } /** * 模糊定位,获取水印图相对于背景图的 x,y * @return [type] [description] */ public function getPosition($wpath,$p ='') { $binfo = getimagesize($this->bpath); // echo "
";        // 获取水印图信息        $waterinfo = getimagesize($wpath);        /*原图必须大于水印*/        if($binfo[0] < $waterinfo[0]){            return ['error'=>'水印图过大'];        }                if($binfo[1] < $waterinfo[1]){            return ['error'=>'水印图过高'];        }        $positon = empty($p) ? $this->positon : $p;        switch ($positon) {            //1顶部居左            case 1:                $x=$y=0;                break;            //2顶部居右            case 2:                 // 原图宽 - 水印图宽                 $x = $binfo[0] - $waterinfo[0];                 $y = 0;                 break;            //3居中            case 3:                 $x = ($binfo[0] - $waterinfo[0])/2;                $y = ($binfo[1] - $waterinfo[1])/2;                break;            //4底部居左            case 4:                 $x = 0;                 $y = $binfo[1] - $waterinfo[1];                break;            //5底部居右            case 5:                 $x = $binfo[0] - $waterinfo[0];                $y = $binfo[1] - $waterinfo[1];                 break;            default:                 $x=$y=0;        }        return ['x'=>$x,                'y'=>$y,                'w'=>$waterinfo[0],                'h'=>$waterinfo[1],                'error'=>false               ];    }    /**     * 创建单图片水印     * @param $wpath 水印图路径     * @return [type] [description]     */    public function createWate($wpath)    {        // 创建图片对象        $bobj = $this->createObj($this->bpath);        $wobj = $this->createObj($wpath);        $pores = $this->getPosition($wpath);        if($pores['error']){            return $pores['error'];        }        $x = $pores['x'];        $y = $pores['y'];        $w = $pores['w'];        $h = $pores['h'];        // 原图obj , 水印图obj ,水start_x , 水start_y, 水x , 水y ,拷贝的宽度, 拷贝的高度 , 透明度        $res = imagecopymerge($bobj, $wobj, $x, $y, 0, 0, $w, $h, $this->alpha);        header('Content-Type: image/jpeg');        imagepng($bobj);        imagedestroy($bobj);         imagedestroy($wobj);      }    /**     * 创建多个图片水印     * @param  [array] $waterpaths [水印图路径]     * @param  [array] $positon [水印位置]     * @param  [array] $alpha [水印透明度]     * @param  [int]   $re    [缩放值 0 - 1]     * @return [type]             [description]     * 使用方式     * $res =$obj->createWates([$waterpath,'./img/share.png'],[1,3],[40,50]);     */    public function createWates($waterpaths = array(),$positon = array(),$alpha = array(),$percent = 1)    {           // 创建背景对象        $bobj = $this->createObj($this->bpath);        $deobjs[] = $bobj;        foreach ($waterpaths as $key => $value) {            $deobjs[] = $nobj = $this->createObj($value);            $pos = empty($positon[$key]) ? $this->positon : $positon[$key];            $al = empty($alpha[$key]) ? $this->alpha : $alpha[$key];            $pores = $this->getPosition($value,$pos);            if($pores['error']){                return $pores['error'];            }            $x = $pores['x'];            $y = $pores['y'];            $w = $pores['w'];            $h = $pores['h'];            // 原图obj , 水印图obj ,水start_x , 水start_y, 水x , 水y ,拷贝的宽度, 拷贝的高度 , 透明度            imagecopymerge($bobj, $nobj, $x, $y, 0, 0, $w, $h, $al);               }        // 是否开启缩放        if( $percent < 1){            // 压缩            $this->nw = $this->bw * $percent;            $this->nh = $this->bh * $percent;            // 分配颜色            $newsouce = imagecreatetruecolor($this->nw, $this->nh);             // 填充背景            $white = ImageColorAllocate($newsouce,255,255,255);            // 开始缩放            imagecopyresampled($newsouce, $bobj, 0, 0, 0, 0, $this->nw, $this->nh, $this->bw, $this->bh);            $bobj = $newsouce;        }        $t = $this->btype;        header("Content-Type: image/$t");        $func = "image" . $t;        $func($bobj);        foreach ($deobjs as $v) {            imagedestroy($v);         }    }    /**     * 创建缩放的图片     * @param  图片路径     * @return obj     */    public function createReObj($filepath)    {        // 获得原来的图片大小          list($width, $height) = getimagesize($filepath);        $this->nw = $width * $this->percent;          $this->nh = $height * $this->percent;        // 原比例        $ratio_orig = $width / $height;        // 新比例        $neww = $this->nw / $this->nh;        // 源图像资源        $reobj = $this->createObj($filepath);        // 分配颜色        $image = imagecreatetruecolor($this->nw, $this->nh);         // 填充背景        $white = ImageColorAllocate($image,255,255,255);        // 开始缩放        imagecopyresampled($image, $reobj, 0, 0, 0, 0, $this->nw, $this->nh, $width, $height);        imagepng($image);    }}//多水印使用方式$bpath = './img/4.jpg';$waterpath = './img/tx.png';$obj = new WaterMark($bpath);// $obj->createWate($waterpath);$res = $obj->createWates([$waterpath,'./img/share.png'],[1,3],[40,50],0.5);

 

转载于:https://my.oschina.net/18y/blog/997680

你可能感兴趣的文章
基于 html5 geolocation来获取经纬度地址(copy)
查看>>
电脑上设置对眼睛友好的绿豆沙色
查看>>
Hadoop+Spark:集群环境搭建
查看>>
有你的地方才是家!澳70年婚龄夫妇手牵手同时离世
查看>>
大数据时代,这项能力程序员不具备就out了!
查看>>
怎么让你在工作中不无聊
查看>>
“寻迹京杭运河”两岸青年交流活动在北京落幕
查看>>
一滴血或能预测老年痴呆 韩国研究帮助你了解病情
查看>>
台湾海峡掀9级大风 福建平潭赴台海上航线连续三天停航
查看>>
摩登兄弟:参加《歌手》压力很大,在准备下一期歌曲
查看>>
面向 Kubernetes 编程:Kubernetes 是下一代操作系统
查看>>
Swift iOS : self sized cell
查看>>
Android多个网络连接
查看>>
前沿 | BAIR探索机器学习公平准则的长期影响:对弱势群体的善意真的种出了善果?...
查看>>
这才是我想要的彩虹进度条-[vue-ins-progress-bar]
查看>>
非JSON格式字符串转换为正规JSON字符串
查看>>
[译] 深入 React 高阶组件
查看>>
计算机网络第一篇【基础知识】
查看>>
产品经理,你会陪着设计师出图吗?
查看>>
带有Apache Spark的Lambda架构
查看>>