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 相关文章推荐
超强分页类2.0发布,支持自定义风格,默认4种显示模式
Jan 02 PHP
解析crontab php自动运行的方法
Jun 24 PHP
php用header函数实现301跳转代码实例
Nov 25 PHP
深入解读php中关于抽象(abstract)类和抽象方法的问题分析
Jan 03 PHP
ThinkPHP的MVC开发机制实例解析
Aug 23 PHP
PHP中JSON的应用技巧
Oct 10 PHP
WordPress开发中的get_post_custom()函数使用解析
Jan 04 PHP
PHP数组去重比较快的实现方式
Jan 19 PHP
Zend Framework教程之Bootstrap类用法概述
Mar 14 PHP
什么是OneThink oneThink后台添加插件步骤
Apr 13 PHP
php创建图像具体步骤
Mar 13 PHP
PHP抽象类与接口的区别实例详解
May 09 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
全国FM电台频率大全 - 2 天津市
2020/03/11 无线电
php实现jQuery扩展函数
2009/10/30 PHP
PHP中替换键名的简易方法示例详解
2014/01/07 PHP
使用PHP生成图片的缩略图的方法
2015/08/18 PHP
JQuery 简便实现页面元素数据验证功能
2007/03/24 Javascript
extjs 学习笔记(二) Ext.Element类
2009/10/13 Javascript
前台js改变Session的值(用ajax实现)
2012/12/28 Javascript
node.js中的console.time方法使用说明
2014/12/09 Javascript
jquery实现对联广告的方法
2015/02/05 Javascript
BootStrap的Datepicker控件使用心得分享
2016/05/25 Javascript
jQuery提示插件qTip2用法分析(支持ajax及多种样式)
2016/06/08 Javascript
jquery判断类型是不是number类型的实例代码
2016/10/07 Javascript
JavaScript仿微博发布信息案例
2016/11/16 Javascript
简单实现AngularJS轮播图效果
2020/04/10 Javascript
vue-hook-form使用详解
2017/04/07 Javascript
vue一个页面实现音乐播放器的示例
2018/02/06 Javascript
解决Vue2.x父组件与子组件之间的双向绑定问题
2018/03/06 Javascript
jQuery+ajax读取json数据并按照价格排序示例
2018/03/28 jQuery
微信小程序之裁剪图片成圆形的实现代码
2018/10/11 Javascript
Node+OCR实现图像文字识别功能
2018/11/26 Javascript
nuxt踩坑之Vuex状态树的模块方式使用详解
2019/09/06 Javascript
node.js基础知识汇总
2020/08/25 Javascript
Python实现的检测网站挂马程序
2014/11/30 Python
Python面向对象程序设计构造函数和析构函数用法分析
2019/04/12 Python
python Dijkstra算法实现最短路径问题的方法
2019/09/19 Python
Python函数的定义方式与函数参数问题实例分析
2019/12/26 Python
Python 找出出现次数超过数组长度一半的元素实例
2020/05/11 Python
Python实现爬取并分析电商评论
2020/06/19 Python
python else语句在循环中的运用详解
2020/07/06 Python
HTML5本地数据库基础操作详解
2016/04/26 HTML / CSS
日本乐天德国站:Rakuten.de
2019/05/16 全球购物
十佳家长事迹材料
2014/08/26 职场文书
领导欢送会主持词
2015/07/06 职场文书
Redis主从配置和底层实现原理解析(实战记录)
2021/06/30 Redis
Java面试题冲刺第十九天--数据库(4)
2021/08/07 Java/Android
Windows server 2012搭建FTP服务器
2022/04/29 Servers