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


Posted in PHP onApril 12, 2019

本文实例讲述了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 IP及IP段进行访问限制的代码
Dec 17 PHP
php session和cookie使用说明
Apr 07 PHP
非常好用的两个PHP函数 serialize()和unserialize()
Feb 04 PHP
解析php file_exists无效的解决办法
Jun 26 PHP
PHP错误Allowed memory size of 67108864 bytes exhausted的3种解决办法
Jul 28 PHP
PHP生成网站桌面快捷方式代码分享
Oct 11 PHP
php生成验证码,缩略图及水印图的类分享
Apr 07 PHP
Laravel框架实现发送短信验证功能代码
Jun 06 PHP
详解提高使用Java反射的效率方法
Apr 29 PHP
ThinkPHP5.1的权限控制怎么写?分享一个AUTH权限控制
Mar 09 PHP
浅谈如何提高PHP代码的质量
May 28 PHP
PHP 时间处理类Carbon
May 20 PHP
详解PHP素材图片上传、下载功能
Apr 12 #PHP
laravel 事件/监听器实例代码
Apr 12 #PHP
Laravel5.7 数据库操作迁移的实现方法
Apr 12 #PHP
laravel使用Faker数据填充的实现方法
Apr 12 #PHP
Laravel5.7 Eloquent ORM快速入门详解
Apr 12 #PHP
laravel 数据迁移与 Eloquent ORM的实现方法
Apr 12 #PHP
PHP中的Iterator迭代对象属性详解
Apr 12 #PHP
You might like
fleaphp下不确定的多条件查询的巧妙解决方法
2008/09/11 PHP
php制作圆形用户头像的实例_自定义封装类源代码
2017/09/18 PHP
node.js 一个简单的页面输出实现代码
2012/03/07 Javascript
JQuery学习笔录 简单的JQuery
2012/04/09 Javascript
解决jquery操作checkbox火狐下第二次无法勾选问题
2014/02/10 Javascript
js中array的sort()方法使用介绍
2014/02/20 Javascript
jquery 隐藏与显示tr标签示例代码
2014/06/06 Javascript
JQuery动态添加和删除表格行的方法
2015/03/09 Javascript
JS实现简易图片轮播效果的方法
2015/03/25 Javascript
jQuery中 prop() attr()使用详解
2015/05/19 Javascript
jQuery事件绑定on()、bind()与delegate() 方法详解
2015/06/03 Javascript
javascript实现省市区三级联动下拉框菜单
2015/11/17 Javascript
基于JS如何实现类似QQ好友头像hover时显示资料卡的效果(推荐)
2016/06/09 Javascript
canvas实现流星雨的背景效果
2017/01/13 Javascript
vue router学习之动态路由和嵌套路由详解
2017/09/21 Javascript
JavaScript判断对象和数组的两种方法
2019/05/31 Javascript
如何测量vue应用运行时的性能
2019/06/21 Javascript
python数据结构之二叉树的统计与转换实例
2014/04/29 Python
python中numpy基础学习及进行数组和矢量计算
2017/02/12 Python
浅谈django model postgres的json字段编码问题
2018/01/05 Python
pandas将DataFrame的列变成行索引的方法
2018/04/10 Python
python实现基于信息增益的决策树归纳
2018/12/18 Python
python中必要的名词解释
2019/11/20 Python
MNIST数据集转化为二维图片的实现示例
2020/01/10 Python
tensorflow-gpu安装的常见问题及解决方案
2020/01/20 Python
在Python中通过threshold创建mask方式
2020/02/19 Python
完美解决Django2.0中models下的ForeignKey()问题
2020/05/19 Python
阿迪达斯加拿大官网:Adidas加拿大
2016/08/25 全球购物
会计自我鉴定
2013/11/02 职场文书
学校办公室主任职责
2013/12/27 职场文书
网络技术专业推荐信
2014/02/20 职场文书
中文专业毕业生自荐信
2014/05/24 职场文书
年度安全生产目标责任书
2014/07/23 职场文书
2014年销售人员工作总结
2014/11/27 职场文书
遗失说明具结保证书
2015/02/26 职场文书
2016年村干部公开承诺书(公开承诺事项)
2016/03/25 职场文书