• 技术文章 >php教程 >PHP源码

    php给图片加水印四

    2016-06-08 17:30:53原创510

    /**
    * 生成水印,调用了生成水印文字和水印图片两个方法
    */
    function _createMask()
    {
    if($this->mask_word)
    {
    // 获取字体信息
    $this->_setFontInfo();

    if($this->_isFull())
    {
    die("水印文字过大");
    }
    else
    {
    $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
    $white = ImageColorAllocate($this->h_dst,255,255,255);
    imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
    $this->_drawBorder();
    imagecopyresampled( $this->h_dst, $this->h_src,
    $this->start_x, $this->start_y,
    $this->src_x, $this->src_y,
    $this->fill_w, $this->fill_h,
    $this->copy_w, $this->copy_h);
    $this->_createMaskWord($this->h_dst);
    }
    }

    if($this->mask_img)
    {
    $this->_loadMaskImg();//加载时,取得宽高

    if($this->_isFull())
    {
    // 将水印生成在原图上再拷
    $this->_createMaskImg($this->h_src);
    $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
    $white = ImageColorAllocate($this->h_dst,255,255,255);
    imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
    $this->_drawBorder();
    imagecopyresampled( $this->h_dst, $this->h_src,
    $this->start_x, $this->start_y,
    $this->src_x, $this->src_y,
    $this->fill_w, $this->fill_h,
    $this->copy_w, $this->copy_h);
    }
    else
    {
    // 创建新图并拷贝
    $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
    $white = ImageColorAllocate($this->h_dst,255,255,255);
    imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
    $this->_drawBorder();
    imagecopyresampled( $this->h_dst, $this->h_src,
    $this->start_x, $this->start_y,
    $this->src_x, $this->src_y,
    $this->fill_w, $this->fill_h,
    $this->copy_w, $this->copy_h);
    $this->_createMaskImg($this->h_dst);
    }
    }

    if(empty($this->mask_word) && empty($this->mask_img))
    {
    $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
    $white = ImageColorAllocate($this->h_dst,255,255,255);
    imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
    $this->_drawBorder();

    imagecopyresampled( $this->h_dst, $this->h_src,
    $this->start_x, $this->start_y,
    $this->src_x, $this->src_y,
    $this->fill_w, $this->fill_h,
    $this->copy_w, $this->copy_h);
    }
    }

    /**
    * 画边框
    */
    function _drawBorder()
    {
    if(!empty($this->img_border_size))
    {
    $c = $this->_parseColor($this->img_border_color);
    $color = ImageColorAllocate($this->h_src,$c[0], $c[1], $c[2]);
    imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$color);// 填充背景色
    }
    }

    /**
    * 生成水印文字
    */
    function _createMaskWord($src)
    {
    $this->_countMaskPos();
    $this->_checkMaskValid();

    $c = $this->_parseColor($this->mask_font_color);
    $color = imagecolorallocatealpha($src, $c[0], $c[1], $c[2], $this->mask_txt_pct);

    if(is_numeric($this->font))
    {
    imagestring($src,
    $this->font,
    $this->mask_pos_x, $this->mask_pos_y,
    $this->mask_word,
    $color);
    }
    else
    {
    imagettftext($src,
    $this->font_size, 0,
    $this->mask_pos_x, $this->mask_pos_y,
    $color,
    $this->font,
    $this->mask_word);
    }
    }

    /**
    * 生成水印图
    */
    function _createMaskImg($src)
    {
    $this->_countMaskPos();
    $this->_checkMaskValid();
    imagecopymerge($src,
    $this->h_mask,
    $this->mask_pos_x ,$this->mask_pos_y,
    0, 0,
    $this->mask_w, $this->mask_h,
    $this->mask_img_pct);

    imagedestroy($this->h_mask);
    }

    /**
    * 加载水印图
    */
    function _loadMaskImg()
    {
    $mask_type = $this->_getImgType($this->mask_img);
    $this->_checkValid($mask_type);

    // file_get_contents函数要求php版本>4.3.0
    $src = '';
    if(function_exists("file_get_contents"))
    {
    $src = file_get_contents($this->mask_img);
    }
    else
    {
    $handle = fopen ($this->mask_img, "r");
    while (!feof ($handle))
    {
    $src .= fgets($fd, 4096);
    }
    fclose ($handle);
    }
    if(empty($this->mask_img))
    {
    die("水印图片为空");
    }
    $this->h_mask = ImageCreateFromString($src);
    $this->mask_w = $this->getImgWidth($this->h_mask);
    $this->mask_h = $this->getImgHeight($this->h_mask);
    }

    /**
    * 图片输出
    */
    function _output()
    {
    $img_type = $this->img_type;
    $func_name = $this->all_type[$img_type]['output'];
    if(function_exists($func_name))
    {
    // 判断浏览器,若是IE就不发送头
    if(isset($_SERVER['HTTP_USER_AGENT']))
    {
    $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);
    if(!preg_match('/^.*MSIE.*\)$/i',$ua))
    {
    header("Content-type:$img_type");
    }
    }
    $func_name($this->h_dst, $this->dst_img, $this->img_display_quality);
    }
    else
    {
    Return false;
    }
    }

    /**
    * 分析颜色
    *
    * @param string $color 十六进制颜色
    */
    function _parseColor($color)
    {
    $arr = array();
    for($ii=1; $ii {
    $arr[] = hexdec(substr($color,$ii,2));
    $ii++;
    }

    Return $arr;
    }

    /**
    * 计算出位置坐标
    */
    ?>

    php入门到就业线上直播课:查看学习

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:nbsp this gt dst mask
    上一篇:用 Smarty 分离 PHP 应用程序中的形式与功能 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• PHP实现301跳转,及延时跳转代码• php 列出目录与删除目录实例代码• php 模拟用户抓取网页内容程序• 高亮显示搜索结果代码• php入门教程-留言板程序
    1/1

    PHP中文网