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 mysql数据库操作类
Jun 04 PHP
php自动加载的两种实现方法
Jun 21 PHP
php设计模式 Singleton(单例模式)
Jun 26 PHP
php数组函数序列之array_keys() - 获取数组键名
Oct 30 PHP
PHP COOKIE及时生效的方法介绍
Feb 14 PHP
php学习笔记之面向对象
Nov 08 PHP
php+mysqli实现批量替换数据库表前缀的方法
Dec 29 PHP
PHP中把有符号整型转换为无符号整型方法
May 27 PHP
php使用curl通过代理获取数据的实现方法
May 16 PHP
浅谈php数组array_change_key_case() 函数和array_chunk()函数
Oct 22 PHP
PHP配置ZendOpcache插件加速
Feb 14 PHP
PHP实现字符串的全排列详解
Apr 24 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
php连接mysql数据库代码
2009/03/10 PHP
php制作unicode解码工具(unicode编码转换器)代码分享
2013/12/24 PHP
php过滤所有的空白字符(空格、全角空格、换行等)
2015/10/27 PHP
thinkPHP5.0框架安装教程
2017/03/25 PHP
PHP编程实现计算抽奖概率算法完整实例
2017/08/09 PHP
基于Laravel(5.4版本)的基本增删改查操作方法
2019/10/11 PHP
PHP设计模式(五)适配器模式Adapter实例详解【结构型】
2020/05/02 PHP
JavaScript里实用的原生API汇总
2015/05/14 Javascript
Javascript面试经典套路reduce函数查重
2017/03/23 Javascript
Express + Node.js实现登录拦截器的实例代码
2017/07/01 Javascript
基于Vue实例对象的数据选项
2017/08/09 Javascript
js 开发之autocomplete=&quot;off&quot;在chrom中失效的解决办法
2017/09/28 Javascript
node.js博客项目开发手记
2018/03/16 Javascript
解决vue动态为数据添加新属性遇到的问题
2018/09/18 Javascript
vue实现循环切换动画
2018/10/17 Javascript
vue计算属性get和set用法示例
2019/02/08 Javascript
解决vuecli3中img src 的引入问题
2020/08/04 Javascript
基于VUE实现简单的学生信息管理系统
2021/01/13 Vue.js
python中pass语句用法实例分析
2015/04/30 Python
Python OS模块常用函数说明
2015/05/23 Python
Python的组合模式与责任链模式编程示例
2016/02/02 Python
Python 爬虫学习笔记之正则表达式
2016/09/21 Python
django主动抛出403异常的方法详解
2019/01/04 Python
python字符串切割:str.split()与re.split()的对比分析
2019/07/16 Python
Python实现栈和队列的简单操作方法示例
2019/11/29 Python
tensorboard实现同时显示训练曲线和测试曲线
2020/01/21 Python
Django实现从数据库中获取到的数据转换为dict
2020/03/27 Python
Python发送邮件实现基础解析
2020/08/14 Python
CSS3改变浏览器滚动条样式
2019/01/04 HTML / CSS
JPA的优势都有哪些
2013/07/04 面试题
教育局党的群众路线教育实践活动整改方案
2014/09/20 职场文书
2015年教导处教学工作总结
2015/07/22 职场文书
中学校园广播稿
2015/08/18 职场文书
会计专业2019暑假实习报告
2019/06/21 职场文书
企业管理不到位检讨书
2019/06/27 职场文书
用position:sticky完美解决小程序吸顶问题的实现方法
2021/04/24 HTML / CSS