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

Posted 智商不够_熬夜来凑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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),'水印文字');

 

以上是关于php学习图片文字水印,仅供参考的主要内容,如果未能解决你的问题,请参考以下文章

php给图片添加文字水印方法汇总

PHP加水印代码 支持文字和图片水印

PHP给图片添加文字水印

php对图片加水印--将文字作为水印加到图片

PHP图片水印

PHP实现水印效果(文字图片)