PHP实现可添加水印与生成缩略图的图片处理工具类


Posted in PHP onJanuary 16, 2018

本文实例讲述了PHP实现可添加水印与生成缩略图的图片处理工具类。分享给大家供大家参考,具体如下:

ImageTool.class.php

<?php
class ImageTool
{
  private $imagePath;//图片路径
  private $outputDir;//输出文件夹
  private $memoryImg;//内存图像
  public function __construct($imagePath, $outputDir = null)
  {
    $this->imagePath = $imagePath;
    $this->outputDir = $outputDir;
    $this->memoryImg = null;
  }
  /**
   * 显示内存中的图片
   * @param $image
   */
  public function showImage()
  {
    if ($this->memoryImg != null) {
      $info = getimagesize($this->imagePath);
      $type = image_type_to_extension($info[2], false);
      header('Content-type:' . $info['mime']);
      $funs = "image{$type}";
      $funs($this->memoryImg);
      imagedestroy($this->memoryImg);
      $this->memoryImg = null;
    }
  }
  /**将图片以文件形式保存
   * @param $image
   */
  private function saveImage($image)
  {
    $info = getimagesize($this->imagePath);
    $type = image_type_to_extension($info[2], false);
    $funs = "image{$type}";
    if (empty($this->outputDir)) {
      $funs($image, md5($this->imagePath) . '.' . $type);
    } else {
      $funs($image, $this->outputDir . md5($this->imagePath) . '.' . $type);
    }
  }
  /**
   * 压缩图片
   * @param $width 压缩后宽度
   * @param $height 压缩后高度
   * @param bool $output 是否输出文件
   * @return resource
   */
  public function compressImage($width, $height, $output = false)
  {
    $image = null;
    $info = getimagesize($this->imagePath);
    $type = image_type_to_extension($info[2], false);
    $fun = "imagecreatefrom{$type}";
    $image = $fun($this->imagePath);
    $thumbnail = imagecreatetruecolor($width, $height);
    imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $width, $height, $info[0], $info[1]);
    imagedestroy($image);
    if ($output) {
      $this->saveImage($thumbnail);
    }
    $this->memoryImg = $thumbnail;
    return $this;
  }
  /**
   * 为图像添加文字标记
   *
   * @param $content 文本内容
   * @param $size 字体大小
   * @param $font 字体样式
   * @param bool $output 是否输出文件
   * @return $this
   */
  public function addTextmark($content, $size, $font, $output = false)
  {
    $info = getimagesize($this->imagePath);
    $type = image_type_to_extension($info[2], false);
    $fun = "imagecreatefrom{$type}";
    $image = $fun($this->imagePath);
    $color = imagecolorallocatealpha($image, 0, 0, 0, 80);
    $posX = imagesx($image) - strlen($content) * $size / 2;
    $posY = imagesy($image) - $size / 1.5;
    imagettftext($image, $size, 0, $posX, $posY, $color, $font, $content);
    if ($output) {
      $this->saveImage($image);
    }
    $this->memoryImg = $image;
    return $this;
  }
  /**
   * 为图片添加水印
   *
   * @param $watermark 水印图片路径
   * @param $alpha 水印透明度(0-100)
   * @param bool $output 是否输出文件
   * @return $this
   */
  public function addWatermark($watermark, $alpha, $output = false)
  {
    $image_info = getimagesize($this->imagePath);
    $image_type = image_type_to_extension($image_info[2], false);
    $image_fun = "imagecreatefrom{$image_type}";
    $image = $image_fun($this->imagePath);
    $mark_info = getimagesize($watermark);
    $mark_type = image_type_to_extension($mark_info[2], false);
    $mark_fun = "imagecreatefrom{$mark_type}";
    $mark = $mark_fun($watermark);
    $posX = imagesx($image) - imagesx($mark);
    $posY = imagesy($image) - imagesy($mark);
    imagecopymerge($image, $mark, $posX, $posY, 0, 0, $mark_info[0], $mark_info[1], $alpha);
    if ($output) {
      $this->saveImage($image);
    }
    $this->memoryImg = $image;
    return $this;
  }
}

ImageTool使用

首先导入ImageTool工具:

require_once 'ImageTool.class.php';

然后实例化ImageTool对象:

$imageTool = new ImageTool('img/oppman.jpeg', 'out/');//图片路径、输出文件夹

一、生成压缩图片

$imageTool->compressImage(350, 250, true);//压缩宽度、压缩高度、是否保存
$imageTool->showImage();

PHP实现可添加水印与生成缩略图的图片处理工具类

二、添加文字水印

$imageTool->addTextmark('一拳超人', 50, 'res/micro.ttf', true);//内容、尺寸、字体、是否保存
$imageTool->showImage();

PHP实现可添加水印与生成缩略图的图片处理工具类

三、添加图片水印

$imageTool->addWatermark('res/logo.jpeg', 100, true);//水印路径、透明度、是否保存
$imageTool->showImage();

PHP实现可添加水印与生成缩略图的图片处理工具类

仅当做临时图像输出:

$imageTool->addTextmark('快捷输出', 50, 'res/micro.ttf')->showImage();
PHP 相关文章推荐
PHP的栏目导航程序
Oct 09 PHP
计数器详细设计
Oct 09 PHP
怎样在UNIX系统下安装php3
Oct 09 PHP
php 文件缓存函数
Oct 08 PHP
PHP设计模式 注册表模式(多个类的注册)
Feb 05 PHP
PHP使用GIFEncoder类生成gif动态滚动字幕
Jul 01 PHP
PHP实现算式验证码和汉字验证码实例
Mar 09 PHP
php实现分页显示
Nov 03 PHP
浅析PHP中call user func()函数及如何使用call user func调用自定义函数
Nov 05 PHP
thinkphp自带验证码全面解析
Sep 18 PHP
PHP自动补全表单的两种方法
Mar 06 PHP
php中字符串和整数比较的操作方法
Jun 06 PHP
PHP实现找出链表中环的入口节点
Jan 16 #PHP
详解thinkphp中的volist标签
Jan 15 #PHP
thinkphp 中的volist标签在ajax操作中的特殊性(推荐)
Jan 15 #PHP
PHP7扩展开发之基于函数方式使用lib库的方法详解
Jan 15 #PHP
PHP7扩展开发之hello word实现方法详解
Jan 15 #PHP
基于 Swoole 的微信扫码登录功能实现代码
Jan 15 #PHP
详解PHP序列化和反序列化原理
Jan 15 #PHP
You might like
《星际争霸重制版》兵种对比图鉴
2020/03/02 星际争霸
Amazon Prime Video平台《无限住人 -IMMORTAL-》2020年开始TV放送!
2020/03/06 日漫
Laravel中扩展Memcached缓存驱动实现使用阿里云OCS缓存
2015/02/10 PHP
既简单又安全的PHP验证码 附调用方法
2016/06/02 PHP
php使用 readfile() 函数设置文件大小大小的方法
2017/08/11 PHP
详解PHP字符串替换str_replace()函数四种用法
2017/10/13 PHP
php在linux环境中如何使用redis详解
2020/12/15 PHP
JQUERY复选框CHECKBOX全选,取消全选
2008/08/30 Javascript
jQuery get和post 方法传值注意事项
2009/11/03 Javascript
Textbox控件注册回车事件及触发按钮提交事件具体实现
2013/03/04 Javascript
如何动态的导入js文件具体该怎么实现
2014/01/14 Javascript
PHPMyAdmin导入时提示文件大小超出PHP限制的解决方法
2015/03/30 Javascript
浅谈JavaScript事件绑定的常用方法及其优缺点分析
2016/11/01 Javascript
BootStrap中关于Select下拉框选择触发事件及扩展
2016/11/22 Javascript
vue-hook-form使用详解
2017/04/07 Javascript
Angular 作用域scope的具体使用
2017/12/11 Javascript
详解从买域名到使用pm2部署node.js项目全过程
2018/03/07 Javascript
Node爬取大批量文件的方法示例
2019/06/28 Javascript
解决layui-open关闭自身窗口的问题
2019/09/10 Javascript
Python的gevent框架的入门教程
2015/04/29 Python
python实现的希尔排序算法实例
2015/07/01 Python
python实现ping的方法
2015/07/06 Python
Python中list列表的一些进阶使用方法介绍
2015/08/15 Python
Python操作SQLite数据库的方法详解【导入,创建,游标,增删改查等】
2017/07/11 Python
python使用正则筛选信用卡
2019/01/27 Python
Python代码实现http/https代理服务器的脚本
2019/08/12 Python
耐克波兰官方网站:Nike波兰
2019/09/03 全球购物
Cynthia Rowley官网:全球领先的生活方式品牌
2020/10/27 全球购物
2014年度安全生产目标管理责任书
2014/07/25 职场文书
2015应届毕业生求职信范文
2015/03/20 职场文书
2015年乡镇环保工作总结
2015/04/22 职场文书
优秀学生主要事迹怎么写
2015/11/04 职场文书
高效课堂教学反思
2016/02/24 职场文书
Python+Appium实现自动抢微信红包
2021/05/21 Python
Go 语言下基于Redis分布式锁的实现方式
2021/06/28 Golang
instantclient客户端 连接oracle数据库
2022/04/26 Oracle