PHP开发的文字水印,缩略图,图片水印实现类与用法示例

所属分类: 网络编程 / PHP编程 阅读数: 1371
收藏 0 赞 0 分享

本文实例讲述了PHP开发的文字水印,缩略图,图片水印实现类与用法。分享给大家供大家参考,具体如下:

1.实现类ImageToTest.class.php参考代码

class ImageToTest {
  /**
   * 图片的基本信息
   */
  private $info;
  private $image;
  public function __construct($src){
    $info = getimagesize($src);
    $this->info = array(
      'width'=> $info[0],
      'height'=> $info[1],
      'type'=> image_type_to_extension($info[2],false),
      'mime'=>$info['mime']
    );
    $fun = "imagecreatefrom{$this->info['type']}";
    $this->image = $fun($src);
  }
  /**
   * 操作图片 (压缩)
   */
  public function thumb($width,$height){
    $image_thumb = imagecreatetruecolor($width,$height);
    imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,
      $this->info['width'],$this->info['height']);
    imagedestroy($this->image);
    $this->image = $image_thumb;
  }
  /**
   * 操作图片(添加文字水印)
   */
  public function fontMark($content,$font_url,$size,$color,$local,$angle){
    $col = imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
    imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$col,$font_url,$content);
  }
  /**
   * 操作图片(添加水印图片)
   */
  public function imageMark($source,$local,$alpha){
    //1.获取水印图片的基本信息
    $info2 = getimagesize($source);
    //2.通过水印的图片编号来获取水印的图片类型
    $type2 = image_type_to_extension($info2[2],false);
    //3.在内存中创建一个和我们的水印图像一致的图像类型
    $func2 = "imagecreatefrom{$type2}";
    //4.把水印图片复制到内存中
    $water = $func2($source);
    //5.合并图片
    imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$info2[0],$info2[1],$alpha);
    //6.销毁水印图片
    imagedestroy($water);
  }
  /**
   * 在浏览器中输出图片
   */
  public function show(){
    header("Content-type:".$this->info['mime']);
    $funs = "image{$this->info['type']}";
    $funs($this->image);
  }
  /**
   * 把图片保存到硬盘里
   */
  public function save($newName){
    $funs = "image{$this->info['type']}";
    $funs($this->image,'./outPut/'.$newName.'.'.$this->info['type']);
  }
  /**
   * 销毁图片 使用析构函数
   */
  public function __destruct()
  {
    imagedestroy($this->image);
  }
}

2.测试参考代码

require_once('ImageToTest.class.php');
/*$src = './image/wbg.jpg';
$image = new ImageToTest($src);
$image->thumb(700,550);
$image->show();*/
/*$src2 = './image/wbg.jpg';
$content = 'SGC';
$font_url = './image/YGYcuhei.ttf';
$size = 33;
$color = array(
  0=>2,
  1=>222,
  2=>222,
  3=>60
);
$local = array(
  'x'=>20,
  'y'=>100
);
$angle = 10;
$image2 = new ImageToTest($src2);
$image2->fontMark($content,$font_url,$size,$color,$local,$angle);
$image2->show();
$image2->save('hahahah');*/
$src3 = './image/wbg.jpg';
$source = './image/water.jpg';
$local = array(
  'x'=>20,
  'y'=>100
);
$font_url = './image/YGYcuhei.ttf';
$size = 38;
$color = array(
  0=>2,
  1=>222,
  2=>222,
  3=>60
);
$alpha = 60;
$angle = 50;
$image3 = new ImageToTest($src3);
$image3->imageMark($source,$local,$alpha);
$image3->thumb(700,550);
$image3->fontMark('Hello',$font_url,$size,$color,$local,$angle);
$image3->show();
$image3->save('WAWAWAWAWA');

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

更多精彩内容其他人还在看

php实现转换html格式为文本格式的方法

这篇文章主要介绍了php实现转换html格式为文本格式的方法,通过一个自定义函数实现针对HTML标签的过滤,涉及php正则替换的相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

php使用curl通过代理获取数据的实现方法

这篇文章主要介绍了php使用curl通过代理获取数据的实现方法,主要涉及php curl中CURLOPT_PROXYUSERPWD参数的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Yii2 assets清除缓存的方法

这篇文章主要介绍了Yii2 assets清除缓存的方法,涉及Yii中assets相关函数调用与配置技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Yii2中OAuth扩展及QQ互联登录实现方法

这篇文章主要介绍了Yii2中OAuth扩展及QQ互联登录的方法,实例分析了OAuth扩展的相关配置与QQ互联登陆的实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

php pdo oracle中文乱码的快速解决方法

下面小编就为大家带来一篇php pdo oracle中文乱码的快速解决方法。小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧
收藏 0 赞 0 分享

PHP获取用户访问IP地址的5种方法

这篇文章主要为大家详细介绍了PHP获取用户访问IP地址的5种方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

PHP中常用的数组操作方法笔记整理

PHP中拥有传统的array数组结构,并且随着版本的升级也在不断改进,比如从php5.4起可以使用短数组定义语法、我们文中会讲到,下面就来看一下PHP中常用的数组操作方法笔记整理:
收藏 0 赞 0 分享

php时间计算相关问题小结

这篇文章主要介绍了php时间计算相关问题,结合实例形式总结分析了php关于时间与日期的常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP使用内置函数生成图片的方法详解

这篇文章主要介绍了PHP使用内置函数生成图片的方法,结合实例形式详细分析了php生成图片的步骤与相关实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

使用php实现从身份证中提取生日

本文给大家分享的是一则使用php实现的从身份证中提取出生日期的函数,非常的简单,有需要的小伙伴可以参考下
收藏 0 赞 0 分享
查看更多