Image watermarks and thumbnails

巴扎黑
Release: 2016-11-23 14:36:22
Original
1048 people have browsed it

<?php 
/**
 * @title:图片水印
 * @autor:zms
 * @version:2011-7-26
 * 因为GD库的限制,现在只能使用gif,jpg,png三种格式
 */
 // //////////使用例子
 //实例化
 //参数说明:路径,是否使用水印(default:false)
 $img = new image_watermark(true);//实例化
 //设置原始图
 $img->dst_image(&#39;卡通.jpg&#39;);
 //加入文字
 //参数说明:内容,位置(default:图片中间位置),透明度(defulat:127  0-127),大小(default:12),角度(default:0),颜色(RGB:default:255,255,255),(字体:default:shongti.ttf,编码:default:UTF-8)
 $img->set_font(&#39;测试text,www.baidu.com&#39;, "300,422", 50, 15, 50, &#39;36,93,219&#39;);
 $img->set_font(&#39;测试text,www.baidu.com&#39;);
 //加入水印图片
 //参数说明:路径,透明度(default:0),位置:default:图片右下角位置&#39;将图像中坐标从 x3 ,x4 开始,宽度为 x5 ,高度为 x6 的一部分拷贝到 原 图像中坐标为 x1 和 x2 的位置上。&#39;
 //如果水印图的高宽设置大于原始图的大小,不打水印
 $img->src_image(&#39;2010723136342131.jpg&#39;, &#39;80&#39;, "0,0,460,150,200,200");
 $img->src_image(&#39;2010723136342131.jpg&#39;, &#39;80&#39;);
 $img->src_image(&#39;2010723136342131.jpg&#39;, &#39;80&#39;,"0,0,55,56,150,520");
 //缩放图片(0-?)
 //参数说明:倍数(defautl:1),(位置x1,x2,x3,x4)将图像中坐标从 x3 ,x4 开始一部分拷贝到 剪切板 中坐标为 x1 和 x2 的位置上。
 //$img->resize_image(1);
 //将水印后图片写到本地
 //参数说明:是否覆盖原始图片(default:false)
 //$img->write_img(false);
 //将水印后图片输出到浏览器
 $img->print_img();
 
class image_watermark {
    private $dst_img = "";//原始图片真彩图像
    private $dst_img_url = "";//原始图片地址
    private $dst_img_info = "";//原始图片信息
    
    private $src_img = "";//水印图片真彩图像
    private $src_img_url = "";//水印图片地址
    private $src_img_info = "";//水印图片信息
    
    private $resize_img = "";//缩放后图片
    
    //设置图片名的输入编码,解决中文名图片乱码问题
    private $img_in_encode = "UTF-8";
    function set_img_in_encode($encode) {
        $this->img_in_encode = $encode;
    }
    //-------------------
    /**
     * @param object $dst_image: 原始图片
     * @param object $use [optional]: true ,false 是否使用水印
     * @param object $cover [optional]: true ,false 是否覆盖旧图片(true:覆盖 false:生成新图)
     * @return
     */
    function __construct($use = false) {
        header("content-type:text/html; charset=UTF-8");
        if ($use) {//使用水印
            if (!extension_loaded(&#39;gd&#39;)) {
                echo "GD库未开启,请设置php.int开启GD库";
                exit;//中断所有操作
            }
        } else {
            echo "未开启水印功能,请实例化方法时开启";
            exit;//中断所有操作
        }
    }//end __construct
    
    /**
     * 清理
     * @return
     */
    function __destruct() {
        @imagedestroy();
    }
    
    /**
     * 设置原始图
     * @param object $dst_image
     * @return
     */
    function dst_image($dst_image) {
        //中文名图片乱码解决
        $dst_image = @iconv($this->img_in_encode, "gb2312", $dst_image);
        $this->dst_img_url = $dst_image;
        //1.获取图片信息
        $this->dst_img_info = @getimagesize($this->dst_img_url);
        //2.验证是否为可用图片
        if ($this->check_type($this->dst_img_info[2])) {
            //2.1.获取图片信息,识别编码,建立真彩图像
            $this->create_image($this->dst_img_info[2], &#39;dst&#39;);
        } else {
            echo "原始图片文件格式错误!现在只支持:GIF,JPG,PNG";
            exit;//中断所有操作
        }
    }
    /**
     * 设置水印图
     * @param object $src_image : 水印图地址
     * @param object $src_image_style [optional] : x1,x2,x3,x4,x5,x6
     * 将图像中坐标从 x3 ,x4 开始,宽度为 x5 ,高度为 x6 的一部分拷贝到 原 图像中坐标为 x1 和 x2 的位置上。
     * @param object $transparent [optional] : 透明度 其值范围从 0 到 100
     * @return
     */
    function src_image($src_image, $transparent = 100, $src_image_style = "0,0,0,0,100,100") {
        $transparent = $transparent >= 0 && $transparent <= 100 ? $transparent : 100;
        //中文名图片乱码解决
        $src_image = @iconv($this->img_in_encode, "gb2312", $src_image);
        $this->src_img_url = $src_image;
        //1.获取图片信息
        $this->src_img_info = @getimagesize($this->src_img_url);
        $src_image_style = trim(ltrim(rtrim($src_image_style, ","), ","));
        $src_image_style_array = split(",", $src_image_style);
        
        if ($src_image_style == "0,0,0,0,100,100") {
            $src_image_style_array[0] = $this->dst_img_info[0] - 100;
            $src_image_style_array[1] = $this->dst_img_info[1] - 100;
        }
        //判断水印图的设置大小是否会超出原始图的大小,如果超过,则不打水印
        if ($src_image_style_array[4] < $this->dst_img_info[0] && $src_image_style_array[5] < $this->dst_img_info[1]) {
            //2.验证是否为可用图片
            if ($this->check_type($this->src_img_info[2])) {
                //2.1.获取图片信息,识别编码,建立真彩图像
                $this->create_image($this->src_img_info[2], &#39;src&#39;);
                @imagecopymerge($this->dst_img, $this->src_img, $src_image_style_array[0], $src_image_style_array[1], $src_image_style_array[2], $src_image_style_array[3], $src_image_style_array[4], $src_image_style_array[5], $transparent);
            } else {
                echo "水印图片文件格式错误!现在只支持:GIF,JPG,PNG";
                exit;//中断所有操作
            }
        }
    }
    /**
     * 识别编码,建立真彩图像
     * @param object $img_info:图像格式
     * @param object $type : 识别图像是水印或是原始 &#39;dsk&#39; &#39;src&#39;
     * @return
     */
    function create_image($img_info, $type) {
        if ($type == &#39;dst&#39;) {
            $url = $this->dst_img_url;
        } elseif ($type == &#39;src&#39;) {
            $url = $this->src_img_url;
        }
        switch ($img_info) {
            case "1":
                $img = @imagecreatefromgif($url);
                break;
            case "2":
                $img = @imagecreatefromjpeg($url);
                break;
            case "3":
                $img = @imagecreatefrompng($url);
                break;
            default:
                break;
        }//end switch
        if ($type == &#39;dst&#39;) {
            $this->dst_img = $img;
        } elseif ($type == &#39;src&#39;) {
            $this->src_img = $img;
        }
    }//end create_image
    
    /**
     * 设置文字
     * @param object $content : 内容
     * @param object $transparent [optional] : 透明度 其值从 0 到 127。0 表示完全不透明,127 表示完全透明
     * @param object $fontfile [optional] : 字体文件 (.ttf)
     * @param object $col [optional] : 字体颜色 RGB "255,255,255"
     * @param object $coordinate [optional] : 坐标 "x,y"
     * @param object $size [optional] : 大小 int
     * @param object $angle [optional] : 角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转
     * @param object $encode [optional] : 编码 "UTF-8"
     * @return
     */
    function set_font($content, $coordinate = "0,0", $transparent = 0, $size = 12, $angle = 0, $col = "0,0,0", $fontfile = "shongti.ttf") {
        $transparent = $transparent >= 0 && $transparent <= 127 ? $transparent : 0;
        //1.对颜色split
        $col = trim(ltrim(rtrim($col, ","), ","));
        $col_array = split(",", $col);
        //$col = imagecolorallocate($this->dst_img, $col_array[0], $col_array[1], $col_array[2]);
        $col = @imagecolorallocatealpha($this->dst_img, $col_array[0], $col_array[1], $col_array[2], $transparent);
       
        $coordinate = trim(ltrim(rtrim($coordinate, ","), ","));
        $coordinate_array = split(",", $coordinate);
        if ($coordinate == "0,0") {
            $coordinate_array[0] = $this->dst_img_info[0] / 2;
            $coordinate_array[1] = $this->dst_img_info[1] / 2;
        }
        @imagettftext($this->dst_img, $size, $angle, $coordinate_array[0], $coordinate_array[1], $col, $fontfile, $content);
    }//end set_font
    
    /**
     * 缩放图片
     * @param object $resize [optional] : 缩放
     * @param object $coordinate [optional] : 剪切板开始位置:x1,x2,x3,x4
     * 将图像中坐标从 x3 ,x4 开始一部分拷贝到 剪切板 中坐标为 x1 和 x2 的位置上。
     * @return
     */
    function resize_image($resize = 1, $coordinate = "0,0,0,0") {
        $resize = $resize > 0 ? $resize : 1;
        //创建剪切板
        $this->resize_img = @imagecreatetruecolor($this->dst_img_info[0] * $resize, $this->dst_img_info[1] * $resize);
        $coordinate = trim(ltrim(rtrim($coordinate, ","), ","));
        $coordinate_array = split(",", $coordinate);
        //剪切图像
        @imagecopyresized($this->resize_img, $this->dst_img, $coordinate_array[0], $coordinate_array[1], $coordinate_array[2], $coordinate_array[3], $this->dst_img_info[0] * $resize, $this->dst_img_info[1] * $resize, $this->dst_img_info[0], $this->dst_img_info[1]);
    }
    
    /**
     * 将水印后图片写到本地
     * @param object $cover [optional] : 是否覆盖原始图片
     * @return
     */
    function write_img($cover = false) {
        $c = $cover ? $this->str_img_name() : $this->str_img_name(true);
        
        switch ($this->dst_img_info[2]) {
            case "1":
                if ($this->resize_img != "") {
                    @imagegif($this->resize_img, $c);
                } else {
                    @imagegif($this->dst_img, $c);
                }
                break;
            case "2":
                if ($this->resize_img != "") {
                    @imagejpeg($this->resize_img, $c);
                } else {
                    @imagejpeg($this->dst_img, $c);
                }
                break;
            case "3":
                if ($this->resize_img != "") {
                    @imagepng($this->resize_img, $c);
                } else {
                    @imagepng($this->dst_img, $c);
                }
                break;
            default:
                break;
        }//end switch
    }//end write_img
    
    /**
     * 将水印后图片输出到浏览器
     * @return
     */
    function print_img() {
        switch ($this->dst_img_info[2]) {
            case "1":
                header("content-type:image/gif");
                if ($this->resize_img != "") {
                    @imagegif($this->resize_img);
                } else {
                    @imagegif($this->dst_img);
                }
                break;
            case "2":
                header("content-type:image/jpeg");
                if ($this->resize_img != "") {
                    @imagejpeg($this->resize_img);
                } else {
                    @imagejpeg($this->dst_img);
                }
                break;
            case "3":
                header("content-type:image/png");
                if ($this->resize_img != "") {
                    @imagepng($this->resize_img);
                } else {
                    @imagepng($this->dst_img);
                }
                break;
            default:
                break;
        }//end switch
    }//end write_img
    
    /**
     * 验证文件格式
     * @param object $type : 图片格式
     * 1:gif 2:jpg,jpeg 3:png
     * @return
     */
    function check_type($type) {
        return $type == "1" ? true : $type == "2" ? true : $type == "3" ? ture : false;
    }//end check_type
    /**
     * 截取文件名
     * $new : 是否为新图片
     * @return
     */
    function str_img_name($new = false) {
        if (strstr($this->dst_img_url, "/")) {//判断文件名是否为最后一级
            $path = trim(substr($this->dst_img_url, 0, strrpos($this->dst_img_url, "/")));
            //截取文件名
            $file_name = trim(substr($this->dst_img_url, strrpos($this->dst_img_url, "/"), strlen($this->dst_img_url)));
        } else {
            $file_name = trim($this->dst_img_url);
        }
        if ($new) {
            $file_name = str_replace(".", "_new.", $file_name);
        }
        return $path.$file_name;
    }//end str_img_name
    
}//end class
?>
Copy after login

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!