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中的MVC模式运用技巧
May 03 PHP
php中curl使用指南
Feb 05 PHP
Ubuntu中启用php的mail()函数并解决发送邮件速度慢问题
Mar 27 PHP
PHP错误Warning:mysql_query()解决方法
Oct 24 PHP
PHP解压ZIP文件到指定文件夹的方法
Nov 17 PHP
php 运算符与表达式详细介绍
Nov 30 PHP
PHP实现找出有序数组中绝对值最小的数算法分析
Aug 07 PHP
PHP中抽象类,接口功能、定义方法示例
Feb 26 PHP
基于PHP实现微信小程序客服消息功能
Aug 12 PHP
laravel 解决paginate查询多个字段报错的问题
Oct 22 PHP
laravel 查询数据库获取结果实现判断是否为空
Oct 24 PHP
PHP 实现 JSON 数据的编码和解码操作详解
Apr 22 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
不用数据库的多用户文件自由上传投票系统(3)
2006/10/09 PHP
微盾PHP脚本加密专家php解密算法
2020/09/13 PHP
php安装ssh2扩展的方法【Linux平台】
2016/07/20 PHP
form表单传递数组数据、php脚本接收的实例
2017/02/09 PHP
jQuery回车实现登录简单实现
2013/08/20 Javascript
JavaScript动态提示输入框输入字数的方法
2015/07/27 Javascript
javascript实现简单的分页特效
2015/08/12 Javascript
分享两段简单的JS代码防止SQL注入
2016/04/12 Javascript
JavaScript知识点总结(五)之Javascript中两个等于号(==)和三个等于号(===)的区别
2016/05/31 Javascript
微信JS-SDK自定义分享功能实例详解【分享给朋友/分享到朋友圈】
2016/11/25 Javascript
javascript实现根据函数名称字符串动态执行函数的方法示例
2016/12/28 Javascript
js数字计算 误差问题的快速解决方法
2017/02/28 Javascript
vue使用Google地图的实现示例代码
2018/12/19 Javascript
vue项目中自定义video视频控制条的实现代码
2020/04/26 Javascript
2020京东618叠蛋糕js脚本(亲测好用)
2020/06/02 Javascript
Vue仿百度搜索功能
2020/12/28 Vue.js
jQuery使用hide()、toggle()函数实现相机品牌展示隐藏功能
2021/01/29 jQuery
Python简单进程锁代码实例
2015/04/27 Python
Python的Django框架中的数据过滤功能
2015/07/17 Python
Python 自动化表单提交实例代码
2017/06/08 Python
python如何使用unittest测试接口
2018/04/04 Python
python实现比较文件内容异同
2018/06/22 Python
django 将model转换为字典的方法示例
2018/10/16 Python
Django框架模板注入操作示例【变量传递到模板】
2018/12/19 Python
pandas条件组合筛选和按范围筛选的示例代码
2019/08/26 Python
python 如何读、写、解析CSV文件
2021/03/03 Python
Burberry英国官网:英国标志性奢侈品牌
2017/03/29 全球购物
Stio官网:男女、儿童户外服装
2019/12/13 全球购物
美国职棒大联盟的官方手套、球和头盔:Rawlings
2020/02/15 全球购物
学习自我鉴定
2014/02/01 职场文书
哈弗商学院毕业生求职信
2014/02/26 职场文书
责任书格式
2015/01/29 职场文书
民事起诉书范本
2015/05/19 职场文书
Js类的构建与继承案例详解
2021/09/15 Javascript
Python的三个重要函数详解
2022/01/18 Python
使用python生成大量数据写入es数据库并查询操作(2)
2022/09/23 Python