【php学习】图片、文字水印,仅供参考

一、php水印类

<?php
class Image_class {

    private $image;

    public $info;

    public $fontfile="../../system/admin/staff_cert/font/SIMSUN.TTF";



    /**

    * @param $src:图片路径

    * 加载图片到内存中

    */

    function __construct($src){
        $info = getimagesize($src);

        $type = image_type_to_extension($info[2],false);

        $this -> info =$info;

        $this->info['type'] = $type;

        $fun = "imagecreatefrom" .$type;

        $this -> image = $fun($src);

    }



    /**

    * @param $fontsize: 字体大小

    * @param $x: 字体在图片中的x位置

    * @param $y: 字体在图片中的y位置

    * @param $color: 字体的颜色是一个包含rgba的数组

    * @param $text: 想要添加的内容

    * 操作内存中的图片,给图片添加文字水印

    */

    public function fontMark($fontsize,$x,$y,$color,$text){
        $col = imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
        imagefttext($this->image, $fontsize, 0, $x, $y, $col, $this->fontfile, $text);

    }

    /*

    * 输出图片到浏览器中

    */

    public function show(){

        header('content-type:' . $this -> info['mime']);

        $fun='image' . $this->info['type'];

        $fun($this->image);

    }

    /**
     * 保存图片
     * @param unknown $src
     */
    public function imgWrite($src){
        header('content-type:' . $this -> info['mime']);
        $fun='image' . $this->info['type'];
        $fun($this->image,$src);
    }



    /**

    * 销毁图片

    */

    function __destruct(){

        imagedestroy($this->image);

    }

    /**
     * 图片水印
     * @param unknown $dst_path
     * @param unknown $src_path
     * @param unknown $x 
     * @param unknown $y
     */
    public function img_water($src_path,$x,$y,$towidth,$toheight,$opacity=null){
        //创建图片的实例
        $opacity==null?$opacity=100:'';
        $info=$this->info;
        //$dst = imagecreatefromstring(file_get_contents($dst_path));
        $src = imagecreatefromstring(file_get_contents($src_path));
        //获取水印图片的宽高
        list($src_w, $src_h) = getimagesize($src_path);
        //将水印图片缩小到指定大小
        if($toheight/$src_w > $towidth/$src_h){
            $b = $toheight/$src_h;
        }else{
            $b = $towidth/$src_w;
        }
        $new_w = floor($src_w*$b);
        $new_h = floor($src_h*$b);
        $im2 = imagecreatetruecolor($new_w, $new_h);
        imagecopyresized($im2, $src, 0, 0, 0, 0, floor($new_w), floor($new_h), $src_w, $src_h);
        //再次印刷图片
        imagecopymerge($this->image, $src, $x, $y, 0, 0, $src_w, $src_h, $opacity);
        //如果水印图片本身带透明色,则使用imagecopy方法
        //imagecopy($this->image, $src, $x, $y, 0, 0, $src_w, $src_h);
        //输出图片
        //list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);

        //$info = getimagesize($dst_path);//获取图片信息
        $type = image_type_to_extension($info[2], false);//通过编号获取图像类型
        $fun = "image" . $type;
        //-----在浏览器中输出图片
        //header("Content-type:".$info['mime']);
        //$fun($this->image);//在浏览器中输出图片
        //------end
        //$fun($dst,'3.'.$type); //保存图片

        //销毁
        imagedestroy($src);
        /* imagedestroy($this->image);
        imagedestroy($src); */
    }

    /**
     *
     * @param unknown $src_path 图片
     * @param unknown $x
     * @param unknown $y
     * @param unknown $towidth
     * @param unknown $toheight
     * @param string $opacity
     */
    public function ImageToJPG($src_path,$x,$y,$towidth,$toheight,$opacity=null)
    {
        $opacity==null?$opacity=100:'';
        $info=$this->info;
        //$dst = imagecreatefromstring(file_get_contents($dst_path));
        $src = imagecreatefromstring(file_get_contents($src_path));
        //获取水印图片的宽高
        list($src_w, $src_h) = getimagesize($src_path);
        if($toheight/$src_w > $towidth/$src_h){
            $b = $toheight/$src_h;
        }else{
            $b = $towidth/$src_w;
        }
        $new_w = floor($src_w*$b);
        $new_h = floor($src_h*$b);
        /* $src_w=$new_w;
        $src_h=$new_h; */
        //将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果
        //imagecopymerge($this->image, $src, $x, $y, 0, 0, $src_w, $src_h, $opacity);
        imagecopyresized($this->image, $src,$x,$y,0, 0, $new_w, $new_h, $src_w, $src_h);
        //imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
        //如果水印图片本身带透明色,则使用imagecopy方法
        //imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
        //输出图片
        //list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);

        //$info = getimagesize($dst_path);//获取图片信息
        $type = image_type_to_extension($info[2], false);//通过编号获取图像类型
        $fun = "image" . $type;
        //-----在浏览器中输出图片
        //header("Content-type:".$info['mime']);
        //$fun($this->image);//在浏览器中输出图片
        //------end
        //$fun($dst,'3.'.$type); //保存图片

        //销毁
        imagedestroy($src);
    }



}

二、用法 

require_once '../lib/imgWater.php';
$obj = new Image_class($_FILES['file']["tmp_name"]);
$water_src='img/water.png';
$obj->fontfile="../../system/admin/staff_cert/font/msyh.ttc";
$obj->ImageToJPG($water_src,10,10,80,80);
$obj->fontMark($font1_size,10,20,array(255, 250, 250,0),'水印文字');

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智商不够_熬夜来凑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值