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缓存技术的多种方法小结
Aug 14 PHP
web站点获取用户IP的安全方法 HTTP_X_FORWARDED_FOR检验
Jun 01 PHP
PHP性能分析工具XHProf安装使用教程
May 13 PHP
8个必备的PHP功能开发
Oct 02 PHP
php实现CSV文件导入和导出
Oct 24 PHP
PHP Static延迟静态绑定用法分析
Mar 16 PHP
php中Ioc(控制反转)和Di(依赖注入)
May 07 PHP
PHP校验15位和18位身份证号的类封装
Nov 07 PHP
PHP convert_uudecode()函数讲解
Feb 14 PHP
什么是PHP7中的孤儿进程与僵尸进程
Apr 14 PHP
安装docker和docker-compose实例详解
Jul 30 PHP
thinkphp5 路由分发原理
Mar 18 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代码
2013/11/19 PHP
php stripslashes和addslashes的区别
2014/02/03 PHP
php中file_get_contents与curl性能比较分析
2014/11/08 PHP
浅谈laravel orm 中的一对多关系 hasMany
2019/10/21 PHP
比较简单的异步加载JS文件的代码
2009/07/18 Javascript
十分钟打造AutoComplete自动完成效果代码
2009/12/26 Javascript
js调试工具console.log()方法查看js代码的执行情况
2014/08/08 Javascript
jquery+css3实现网页背景花瓣随机飘落特效
2015/08/17 Javascript
理解JavaScript中worker事件api
2015/12/25 Javascript
全国省市二级联动下拉菜单 js版
2016/05/10 Javascript
angular ngClick阻止冒泡使用默认行为的方法
2016/11/03 Javascript
boostrapTable的refresh和refreshOptions区别浅析
2017/01/22 Javascript
js绑定事件和解绑事件
2017/04/27 Javascript
详解vue.js的devtools安装
2017/05/26 Javascript
微信小程序 页面跳转传值实现代码
2017/07/27 Javascript
JS的Ajax与后端交互数据的实例
2018/08/08 Javascript
小程序云开发实战小结
2018/10/25 Javascript
小程序实现多列选择器
2019/02/15 Javascript
Vue中Table组件行内右键菜单实现方法(基于 vue + AntDesign)
2019/11/21 Javascript
JS使用Chrome浏览器实现调试线上代码
2020/07/23 Javascript
vue中使用腾讯云Im的示例
2020/10/23 Javascript
[01:04:30]Fnatic vs Mineski 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
python dict.get()和dict['key']的区别详解
2016/06/30 Python
Python实现字典的遍历与排序功能示例
2017/12/23 Python
简单了解Python3里的一些新特性
2019/07/13 Python
Python如何使用内置库matplotlib绘制折线图
2020/02/24 Python
CSS3 Backgrounds属性相关介绍
2011/05/11 HTML / CSS
全球酒店比价网:HotelsCombined
2017/06/20 全球购物
Puccini乌克兰:购买行李箱、女士手袋网上商店
2020/08/06 全球购物
JS原生实现轮播图的几种方法
2021/03/23 Javascript
大学毕业生个人自荐信范文
2014/01/08 职场文书
两年的个人工作自我评价
2014/01/10 职场文书
公积金单位接收函
2014/01/11 职场文书
公司授权委托书样本
2014/09/15 职场文书
导游词之秦始皇兵马俑博物馆
2019/09/29 职场文书
Windows安装Anaconda3的方法及使用过程详解
2021/06/11 Python