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 相关文章推荐
基于mysql的论坛(7)
Oct 09 PHP
Windows7下PHP开发环境安装配置图文方法
May 20 PHP
php SQL Injection with MySQL
Feb 27 PHP
防止本地用户用fsockopen DDOS攻击对策
Nov 02 PHP
php通过COM类调用组件的实现代码
Jan 11 PHP
input file获得文件根目录简单实现
Apr 26 PHP
探讨PHP调用时间格式的参数详解
Jun 06 PHP
thinkPHP中_initialize方法实例分析
Dec 05 PHP
PHP读取XML格式文件的方法总结
Feb 27 PHP
thinkPHP框架实现图像裁剪、缩放、加水印的方法
Mar 14 PHP
关于PHP定时发送服务的解决办法
Apr 23 PHP
yii2.0整合阿里云oss上传单个文件的示例
Sep 19 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
十天学会php之第二天
2006/10/09 PHP
不用数据库的多用户文件自由上传投票系统(1)
2006/10/09 PHP
经典的PHPer为什么被认为是草根?
2007/04/02 PHP
mysql 字段类型说明
2007/04/27 PHP
php Xdebug 调试扩展的安装与使用.
2010/03/13 PHP
php生成静态文件的多种方法分享
2012/07/17 PHP
php 伪静态之IIS篇
2014/06/02 PHP
php支付宝手机网页支付类实例
2015/03/04 PHP
PHP+MySQL统计该库中每个表的记录数并按递减顺序排列的方法
2016/02/15 PHP
greybox——不开新窗口看新的网页
2007/02/20 Javascript
学习ExtJS border布局
2009/10/08 Javascript
javascript将数组插入到另一个数组中的代码
2013/01/10 Javascript
Javascript解析URL方法详解
2014/12/05 Javascript
javascript删除数组重复元素的方法汇总
2015/06/24 Javascript
详解Angularjs filter过滤器
2016/02/06 Javascript
Angularjs中使用轮播图指令swiper
2017/05/30 Javascript
JavaScript实现获取用户单击body中所有A标签内容的方法
2017/06/05 Javascript
浅谈React和Redux的连接react-redux
2017/12/04 Javascript
Vue 2.0学习笔记之使用$refs访问Vue中的DOM
2017/12/19 Javascript
[05:08]第一届“网鱼杯”DOTA2比赛精彩集锦
2014/09/05 DOTA
Python中规范定义命名空间的一些建议
2016/06/04 Python
Python快速排序算法实例分析
2017/11/29 Python
Python使用sax模块解析XML文件示例
2019/04/04 Python
django中forms组件的使用与注意
2019/07/08 Python
python Event事件、进程池与线程池、协程解析
2019/10/25 Python
windows下python 3.9 Numpy scipy和matlabplot的安装教程详解
2020/11/28 Python
CSS3动画之利用requestAnimationFrame触发重新播放功能
2019/09/11 HTML / CSS
使用CSS3制作版头动画效果
2020/12/24 HTML / CSS
KLOOK客路:发现更好玩的世界,预订独一无二的旅行体验
2016/12/16 全球购物
自动化专业本科毕业生求职信
2013/10/20 职场文书
小学生竞选班长演讲稿
2014/04/24 职场文书
临时用工协议书范本
2014/10/29 职场文书
中学推普周活动总结
2015/05/07 职场文书
新学期开学寄语2016
2015/12/04 职场文书
Nginx反向代理至go-fastdfs案例讲解
2021/08/02 Servers
详解解Django 多对多表关系的三种创建方式
2021/08/23 Python