热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP水印类,支持添加图片、文字、填充颜色区域的实现

下面小编就为大家带来一篇PHP水印类,支持添加图片、文字、填充颜色区域的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

*自己整理的一个水印类*

支持添加图片、文字、填充颜色区域

<&#63;php
/**
 * 图片加水印类,支持文字水印、透明度设置、自定义水印位置等。
 * 使用示例:
 *   $obj = new WaterMask($imgFileName);       //实例化对象
 *   $obj->$waterType = 1;           //类型:0为文字水印、1为图片水印
 *   $obj->$transparent = 45;         //水印透明度
 *   $obj->$waterStr = 'icp.niufee.com';        //水印文字
 *   $obj->$fOntSize= 18;           //文字字体大小
 *   $obj->$fOntColor= array(255,255,255);        //水印文字颜色(RGB)
 *   $obj->$fOntFile= 'AHGBold.ttf';       //字体文件
 *   ……
 *   $obj->output();              //输出水印图片文件覆盖到输入的图片文件
 * @modify liuzp111
 */
class WaterMask{
  public $waterTypeImage   = false;                //水印类型:启用图片水印
  public $waterTypeStr    = false;          //水印类型:启用文字水印
  public $pos        = 0;          //水印位置
  public $transparent    = 45;         //水印透明度(0---100)数值越大越不透明

  public $waterStr      = 'icp.niufee.com';      //水印文字
  public $fOntSize= 14;         //文字字体大小
  public $fOntColor= array(0,0,0);          //水印文字颜色(RGB) 默认黑色
  public $fOntFile= './font/simfang.ttf';      //字体文件

  public $waterImg      = 'logo.png';       //水印图片

  private $srcImg       = '';         //需要添加水印的图片
  private $im         = '';         //图片句柄
  private $water_im      = '';         //水印图片句柄
  private $srcImg_info    = '';         //图片信息
  private $waterImg_info   = '';         //水印图片信息
  private $str_w       = '';         //水印文字宽度
  private $str_h       = '';         //水印文字高度
  private $x         = '';         //水印X坐标
  private $y         = '';         //水印y坐标
  public $output_img     = '';                  //存储输出图片到哪里
  public $is_draw_rectangle = false;                  //是否绘制矩形区域 (暂不支持自定义位置)
  //public $rectange_color   = '';                  //绘制矩形区域的颜色  
  private $result_array    = array();               //结果数组 
  public function __construct($img) {    //析构函数
    //$this->srcImg = file_exists($img) &#63; $img : die('"'.$img.'" 源文件不存在!');
    if(file_exists($img)){
      $this->srcImg = $img;
    }else{
      return array('data'=>'','info'=>'源文件不存在!','status'=>0);
    }
  }

  private function imginfo() {  //获取需要添加水印的图片的信息,并载入图片。
    $this->srcImg_info = getimagesize($this->srcImg);
    switch ($this->srcImg_info[2]) {
      case 3:
        $this->im = imagecreatefrompng($this->srcImg);
        break 1;
      case 2:
        $this->im = imagecreatefromjpeg($this->srcImg);
        break 1;
      case 1:
        $this->im = imagecreatefromgif($this->srcImg);
        break 1;
      default:
        //die('原图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
        return array('data'=>'','info'=>'原图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。','status'=>0);
    }
  }

  private function waterimginfo() {  //获取水印图片的信息,并载入图片。
    $this->waterImg_info = getimagesize($this->waterImg);
    switch ($this->waterImg_info[2]) {
      case 3:
        $this->water_im = imagecreatefrompng($this->waterImg);
        break 1;
      case 2:
        $this->water_im = imagecreatefromjpeg($this->waterImg);
        break 1;
      case 1:
        $this->water_im = imagecreatefromgif($this->waterImg);
        break 1;
      default:
        //die('水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
         return array('data'=>'','info'=>'水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。','status'=>0);
    }
  }
  private function waterpos() {  //水印位置算法
    switch ($this->pos) {
      case 0:   //随机位置
        $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);
        $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);
        break 1;
      case 1:   //上左
        $this->x = 0;
        $this->y = 0;
        break 1;
      case 2:   //上中
        $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
        $this->y = 0;
        break 1;
      case 3:   //上右
        $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
        $this->y = 0;
        break 1;
      case 4:   //中左
        $this->x = 0;
        $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
        break 1;
      case 5:   //中中
        $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
        $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
        break 1;
      case 6:   //中右
        $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
        $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
        break 1;
      case 7:   //下左
        $this->x = 0;
        $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
        break 1;
      case 8:   //下中
        $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
        $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
        break 1;
      case 9:   //下中偏上100px
        $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
        $this->y = $this->srcImg_info[1]-$this->waterImg_info[1] - 100;
        break 1;
      default:  //下右
        $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
        $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
        break 1;
    }
  }
  /**
   * 水印文字图片位置,根据需求调整
   */
  private function waterposStr() {
    $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
    $this->y = $this->srcImg_info[1]-$this->waterImg_info[1] - 3;    
  }
  private function waterimg($type='') {
    if ($this->srcImg_info[0] <= $this->waterImg_info[0] || $this->srcImg_info[1] <= $this->waterImg_info[1]){
      //die('水印比原图大!');
      return array('data'=>'','info'=>'水印比原图大!','status'=>0);
    }
    if($type == 'waterstr'){
      $this->waterposStr();
    }else{
      $this->waterpos();
    }
    $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
    imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]);
    $pct = $this->transparent;
    imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
    imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct);
  }

  private function waterstr() {
    $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
    $w = abs($rect[2]-$rect[6]);
    $h = abs($rect[3]-$rect[7]);
    $fOntHeight= $this->fontSize;
    $this->water_im = imagecreatetruecolor($w, $h);
    imagealphablending($this->water_im,false);
    imagesavealpha($this->water_im,true);
    $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);
    imagefill($this->water_im,0,0,$white_alpha);
    $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
    imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
    $this->waterImg_info = array(0=>$w,1=>$h);
    $this->waterimg($type='waterstr');
  }
  /**
   * 绘制矩形区
   * bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
   * bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
   * @author liuzp111
   */
  public function drawRectangle()
  {
    //imagefill($im,0,0,$gray);//填充资源,填充的坐标(类似PS魔棒),颜色
    /*
     *  1--------------画长方形--------------
     *  bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
     *  参数: 画布资源, 左上角x坐标,左上y坐标,右下x坐标,右下y坐标,颜色
     */
    $color = imagecolorallocate($this->im,255,255,255);//创建矩形边框颜色和填充颜色
    //=========================================================================
    //绘制矩形区域并填充
    // 参数说明:
    //bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
    // im:为将图像载入为图像资源
    // $x1:表示矩形左上角的X坐标
    // $y1:表示矩形左上角的Y坐标
    // $x2:表示矩形右下角的X坐标
    // $y2:表示矩形右下角的Y坐标
    // $color:为填充的RGB颜色
    //
    imagefilledrectangle($this->im,3,$this->srcImg_info[1] - 20,$this->srcImg_info[0]-3,$this->srcImg_info[1]-3,$color);
    //不要使用下方的函数填充,下方填充函数为魔棒填充,容易导致填充不完整
    //imagefill($this->im,$this->srcImg_info[0]/2,$this->srcImg_info[1]-8,$color);//填充资源,填充的坐标(魔棒),颜色

  }
  function output() {
    $this->imginfo();
    //是否创建矩形区域
    if($this->is_draw_rectangle){
      $this->drawRectangle();
    }
    if ($this->waterTypeStr ) {
      $this->waterstr();
    } 
    if($this->waterTypeImage )
    {
      $this->waterimginfo();
      $this->waterimg();
    }
    switch ($this->srcImg_info[2]) {
      case 3:
        $res_output = imagepng($this->im,$this->output_img);
        break 1;
      case 2:
        $res_output = imagejpeg($this->im,$this->output_img);
        break 1;
      case 1:
        $res_output = imagegif($this->im,$this->output_img);
        break 1;
      default:
        // die('添加水印失败!');
        return array('data'=>'','info'=>'添加水印失败!','status'=>0);
        break;
    }
    imagedestroy($this->im);
    imagedestroy($this->water_im);
    return array('data'=>$res_output,'info'=>'添加水印成功!','status'=>1);
  }
}

使用方式:

$file = '58368dddc8c51_22';//需要加水印的图片
$file_ext = '.jpeg';//扩展名
$imgFileName = './'.$file.$file_ext;//需要加水印图片路径
$obj = new WaterMask($imgFileName); //实例化对象
$obj->waterTypeStr = true;     //开启文字水印      
$obj->waterTypeImage = true;    //开启图片水印  
$obj->pos = 9;         //定义水印图片位置 
$obj->waterImg = './water.png';      //水印图片     
$obj->transparent = 100;          //水印透明度
$obj->waterStr = '保险经纪人:刘测试 电话:02052552';       //水印文字
$obj->fOntSize= 9;            //文字字体大小
$obj->fOntColor= array(0,0,0);        //水印文字颜色(RGB)
$obj->fOntFile= './font/msyh.ttc';    //字体文件,这里是微软雅黑
$obj->is_draw_rectangle = TRUE;      //开启绘制矩形区域
$obj ->output_img = './'.$file.'_n'.$file_ext;//输出的图片路径
$obj->output();  

以上这篇PHP水印类,支持添加图片、文字、填充颜色区域的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


推荐阅读
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • 2018年人工智能大数据的爆发,学Java还是Python?
    本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • 学习SLAM的女生,很酷
    本文介绍了学习SLAM的女生的故事,她们选择SLAM作为研究方向,面临各种学习挑战,但坚持不懈,最终获得成功。文章鼓励未来想走科研道路的女生勇敢追求自己的梦想,同时提到了一位正在英国攻读硕士学位的女生与SLAM结缘的经历。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • “你永远都不知道明天和‘公司的意外’哪个先来。”疫情期间,这是我们最战战兢兢的心情。但是显然,有些人体会不了。这份行业数据,让笔者“柠檬” ... [详细]
  • 生成对抗式网络GAN及其衍生CGAN、DCGAN、WGAN、LSGAN、BEGAN介绍
    一、GAN原理介绍学习GAN的第一篇论文当然由是IanGoodfellow于2014年发表的GenerativeAdversarialNetworks(论文下载链接arxiv:[h ... [详细]
  • [译]技术公司十年经验的职场生涯回顾
    本文是一位在技术公司工作十年的职场人士对自己职业生涯的总结回顾。她的职业规划与众不同,令人深思又有趣。其中涉及到的内容有机器学习、创新创业以及引用了女性主义者在TED演讲中的部分讲义。文章表达了对职业生涯的愿望和希望,认为人类有能力不断改善自己。 ... [详细]
  • 本文介绍了游戏开发中的人工智能技术,包括定性行为和非定性行为的分类。定性行为是指特定且可预测的行为,而非定性行为则具有一定程度的不确定性。其中,追逐算法是定性行为的具体实例。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 关羽败走麦城时路过马超封地 马超为何没有出手救人
    对当年关羽败走麦城,恰好路过马超的封地,为啥马超不救他?很感兴趣的小伙伴们,趣历史小编带来详细的文章供大家参考。说到英雄好汉,便要提到一本名著了,没错,那就是《三国演义》。书中虽 ... [详细]
author-avatar
知足幸福_21942
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有