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 session 预定义数组
Mar 16 PHP
php 数组二分法查找函数代码
Feb 16 PHP
PHP setcookie设置Cookie用法(及设置无效的问题)
Jul 13 PHP
jQuery EasyUI API 中文文档 - DateBox日期框
Oct 15 PHP
php获取目录所有文件并将结果保存到数组(实例)
Oct 25 PHP
PHP错误Warning: Cannot modify header information - headers already sent by解决方法
Sep 27 PHP
php以post形式发送xml的方法
Nov 04 PHP
PHP结合Ueditor并修改图片上传路径
Oct 16 PHP
[原创]PHP获取数组表示的路径方法分析【数组转字符串】
Sep 01 PHP
总结PHP代码规范、流程规范、git规范
Jun 18 PHP
PDO::rollBack讲解
Jan 29 PHP
浅谈Laravel中的三种中间件的作用
Oct 13 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
让你同时上传 1000 个文件 (二)
2006/10/09 PHP
详解WordPress中用于合成数组的wp_parse_args()函数
2015/12/18 PHP
php实现微信支付之退款功能
2018/05/30 PHP
createTextRange()的使用示例含文本框选中部分文字内容
2014/02/24 Javascript
javascript实现的右下角弹窗实例
2015/04/24 Javascript
微信小程序 教程之WXML
2016/10/18 Javascript
深入理解JS继承和原型链的问题
2016/12/17 Javascript
js操作浏览器的参数方法
2017/01/21 Javascript
jquery实时获取时间的简单实例
2017/01/26 Javascript
利用js判断手机是否安装某个app的多种方案
2017/02/13 Javascript
angularjs指令之绑定策略(@、=、&)
2017/04/13 Javascript
实例讲解DataTables固定表格宽度(设置横向滚动条)
2017/07/11 Javascript
vue.js实现标签页切换效果
2018/06/07 Javascript
浅谈微信小程序之官方UI框架we-ui使用教程
2018/08/20 Javascript
Python数据处理numpy.median的实例讲解
2018/04/02 Python
Python3 中把txt数据文件读入到矩阵中的方法
2018/04/27 Python
python使用gdal对shp读取,新建和更新的实例
2020/03/10 Python
selenium+python配置chrome浏览器的选项的实现
2020/03/18 Python
python中shell执行知识点
2020/05/06 Python
Python实现读取并写入Excel文件过程解析
2020/05/27 Python
python判断正负数方式
2020/06/03 Python
pandas to_excel 添加颜色操作
2020/07/14 Python
Python pip install之SSL异常处理操作
2020/09/03 Python
html5各种页面切换效果和模态对话框用法总结
2014/12/15 HTML / CSS
约瑟夫·特纳男装:Joseph Turner
2017/10/10 全球购物
中专毕业自我鉴定
2013/10/16 职场文书
医院实习介绍信
2014/01/12 职场文书
网上开商店的创业计划书
2014/01/19 职场文书
家长寄语大全
2014/04/02 职场文书
物理学专业自荐信
2014/06/11 职场文书
受伤赔偿协议书
2014/09/24 职场文书
民主评议党员个人自我评价
2015/03/03 职场文书
家长意见书
2015/06/04 职场文书
2016先进工作者事迹材料
2016/02/25 职场文书
导游词之无锡古运河
2019/11/14 职场文书
win10如何快速切换窗口 win10切换窗口快捷键分享
2022/07/23 数码科技