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 相关文章推荐
最小化数据传输――在客户端存储数据
Oct 09 PHP
php 网页游戏开发入门教程一(webgame+design)
Oct 26 PHP
使用php get_headers 判断URL是否有效的解决办法
Apr 27 PHP
php 使用GD库为页面增加水印示例代码
Mar 24 PHP
php编写批量生成不重复的卡号密码代码
May 14 PHP
php中switch语句用法详解
Aug 17 PHP
PHP获取客户端及服务器端IP的封装类
Jul 21 PHP
PHP面向对象程序设计类的定义与用法简单示例
Dec 27 PHP
PHP实现蛇形矩阵,回环矩阵及数字螺旋矩阵的方法分析
May 29 PHP
PHP7中I/O模型内核剖析详解
Apr 14 PHP
使用laravel和ajax实现整个页面无刷新的操作方法
Oct 03 PHP
yii框架数据库关联查询操作示例
Oct 14 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
咖啡冲泡指南 咖啡有哪些制作方式 单品咖啡 意式咖啡
2021/03/06 冲泡冲煮
Symfony2实现在controller中获取url的方法
2016/03/18 PHP
PHP对象链式操作实现原理分析
2016/10/09 PHP
PHP XML Expat解析器知识点总结
2019/02/15 PHP
PHP封装的分页类与简单用法示例
2019/02/25 PHP
jQuery下的几个你可能没用过的功能
2010/08/29 Javascript
JS文本框默认值处理详解
2013/07/10 Javascript
Jquery创建一个层当鼠标移动到层上面不消失效果
2013/12/12 Javascript
BootStrap入门教程(一)之可视化布局
2016/09/19 Javascript
javascript过滤数组重复元素的实现方法
2017/05/03 Javascript
vue 根据数组中某一项的值进行排序的方法
2018/08/30 Javascript
解决vuecli3.0热更新失效的问题
2018/09/19 Javascript
浅谈javascript错误处理
2019/08/11 Javascript
JS highcharts动态柱状图原理及实现
2020/10/16 Javascript
javascript实现前端分页功能
2020/11/26 Javascript
Python socket实现简单聊天室
2018/04/01 Python
浅谈python中np.array的shape( ,)与( ,1)的区别
2018/06/04 Python
python3实现指定目录下文件sha256及文件大小统计
2019/02/25 Python
初探利用Python进行图文识别(OCR)
2019/02/26 Python
使用Python做垃圾分类的原理及实例代码附源码
2019/07/02 Python
python 寻找离散序列极值点的方法
2019/07/10 Python
Python 3.8 新功能全解
2019/07/25 Python
Python变量、数据类型、数据类型转换相关函数用法实例详解
2020/01/09 Python
如何实现在jupyter notebook中播放视频(不停地展示图片)
2020/04/23 Python
Python venv虚拟环境配置过程解析
2020/07/08 Python
python七种方法判断字符串是否包含子串
2020/08/18 Python
python向企业微信发送文字和图片消息的示例
2020/09/28 Python
Python数据可视化常用4大绘图库原理详解
2020/10/23 Python
Python基于opencv的简单图像轮廓形状识别(全网最简单最少代码)
2021/01/28 Python
css3学习系列之移动属性详解
2017/07/04 HTML / CSS
世界上最大的汽车共享网站:Zipcar
2017/01/14 全球购物
J.Crew官网:美国知名休闲服装品牌
2017/05/19 全球购物
青春飞扬演讲稿
2014/09/11 职场文书
大学生第一学年自我鉴定2015
2014/09/28 职场文书
MySQL性能压力基准测试工具sysbench的使用简介
2021/04/21 MySQL
Python还能这么玩之用Python做个小游戏的外挂
2021/06/04 Python