1.开始

在网站上传图片过程,经常用到缩略图功能。这里我自己写了一个图片处理的Image类,能生成缩略图,并且可以添加水印图。

2.如何生成缩略图

生成缩略图,关键的是如何计算缩放比率。

这里,我根据图片等比缩放,宽高的几种常见变化,得出一个算缩放比率算法是,使用新图(即缩略图)的宽高,分别除以原图的宽高,看哪个值大,就取它作为缩放比率:

缩放比率  = Max( { 新图高度  / 原图高度 新图宽度  / 原图宽度 } )

也就是:

If ( (新图高度  / 原图高度)  >  (新图宽度  / 原图宽度 ) )  {

              缩放比率 =  新图高度  / 原图高度;

      }ELSE {

             缩放比率 =  新图宽度 / 原图宽度;

     }

 这里列出场景的图片缩放场景,及处理方法:

 e.g 

场景1原图比新图大的情况, 缩放比率 =  新图宽度 / 原图宽度 :

场景2,原图比新图大的情况,b. 缩放比率 =  新图高度 / 原图高度 :

场景3,原图比新图大的情况,而且新图宽高相等,即新图形状是正方形,那么上面的缩放算法也是适用的。

场景4,如果 “新图宽度 >= 原图宽度”  ,同时  “新图高度 >= 原图高度”,那么不缩放图片,也不放大图片,保持原图。

场景5,如果 “新图宽度 < 原图宽度”,同时  “新图高度 >= 原图高度”  ,那么先设置  “新图高度= 原图高度”,再剪切。

场景6,如果 “新图高度 < 原图高度”,同时  “新图宽度 >= 原图宽度”  ,那么先设置  “新图宽度= 原图宽度”,再剪切。

3.如何添加水印图片

添加水印很容易,我这里没考虑那么复杂,主要是控制水印位置在图片的右下角,和控制水印在图片中的大小。如,当目标图片与水印图大小接近,那么需要先等比缩放水印图片,再添加水印图片。

左边两幅图,上面是原图,下面是水印图,右边的缩放后加水印的新图。

4.类图

5.PHP代码

5.1. 构造函数 __construct()

在Image类中,除了构造函数__construct()是public,其它函数都为private.也就是在函数__construct()中,直接完成了生成缩略图和添加水印图的功能。如果,只生成缩略图而不需要添加水印,那么直接在__construct()的参数$markPath,设置为null即可。

其中,“$this->quality = $quality ? $quality : 75;” 控制输出为JPG图片时,控制图片质量(0-100),默认值为75;

     /**
* Image constructor.
* @param string $imagePath 图片路径
* @param string $markPath 水印图片路径
* @param int $new_width 缩略图宽度
* @param int $new_height 缩略图高度
* @param int $quality JPG图片格输出质量
*/
public function __construct(string $imagePath,
string $markPath = null,
int $new_width = null,
int $new_height = null,
int $quality = 75)
{
$this->imgPath = $_SERVER['DOCUMENT_ROOT'] . $imagePath;
$this->waterMarkPath = $markPath;
$this->newWidth = $new_width ? $new_width : $this->width;
$this->newHeight = $new_height ? $new_height : $this->height;
$this->quality = $quality ? $quality : 75; list($this->width, $this->height, $this->type) = getimagesize($this->imgPath);
$this->img = $this->_loadImg($this->imgPath, $this->type); //生成缩略图
$this->_thumb();
//添加水印图片
if (!empty($this->waterMarkPath)) $this->_addWaterMark();
//输出图片
$this->_outputImg();
}

Note: 先生成缩略图,再在新图上添加水印 图片。

5.2. 生成缩略图函数_thumb() 

     /**
* 缩略图(按等比例,根据设置的宽度和高度进行裁剪)
*/
private function _thumb()
{ //如果原图本身小于缩略图,按原图长高
if ($this->newWidth > $this->width) $this->newWidth = $this->width;
if ($this->newHeight > $this->height) $this->newHeight = $this->height; //背景图长高
$gd_width = $this->newWidth;
$gd_height = $this->newHeight; //如果缩略图宽高,其中有一边等于原图的宽高,就直接裁剪
if ($gd_width == $this->width || $gd_height == $this->height) {
$this->newWidth = $this->width;
$this->newHeight = $this->height;
} else { //计算缩放比率
$per = 1; if (($this->newHeight / $this->height) > ($this->newWidth / $this->width)) {
$per = $this->newHeight / $this->height;
} else {
$per = $this->newWidth / $this->width;
} if ($per < 1) {
$this->newWidth = $this->width * $per;
$this->newHeight = $this->height * $per;
}
} $this->newImg = $this->_CreateImg($gd_width, $gd_height, $this->type);
imagecopyresampled($this->newImg, $this->img, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height);
}

生成缩略图函数_thumb() ,是按照前面的分析来进行编码。

5.3. 添加水印图片函数 _addWaterMark()

     /**
* 添加水印
*/
private function _addWaterMark()
{
$ratio = 1 / 5; //水印缩放比率 $Width = imagesx($this->newImg);
$Height = imagesy($this->newImg); $n_width = $Width * $ratio;
$n_height = $Width * $ratio; list($markWidth, $markHeight, $markType) = getimagesize($this->waterMarkPath); if ($n_width > $markWidth) $n_width = $markWidth;
if ($n_height > $markHeight) $n_height = $markHeight; $Img = $this->_loadImg($this->waterMarkPath, $markType);
$Img = $this->_thumb1($Img, $markWidth, $markHeight, $markType, $n_width, $n_height);
$markWidth = imagesx($Img);
$markHeight = imagesy($Img);
imagecopyresampled($this->newImg, $Img, $Width - $markWidth - 10, $Height - $markHeight - 10, 0, 0, $markWidth, $markHeight, $markWidth, $markHeight);
imagedestroy($Img);
}

在添加水印图片中,用到一个_thumb1()函数来缩放水印图片:

    /**
* 缩略图(按等比例)
* @param resource $img 图像流
* @param int $width
* @param int $height
* @param int $type
* @param int $new_width
* @param int $new_height
* @return resource
*/
private function _thumb1($img, $width, $height, $type, $new_width, $new_height)
{ if ($width < $height) {
$new_width = ($new_height / $height) * $width;
} else {
$new_height = ($new_width / $width) * $height;
} $newImg = $this->_CreateImg($new_width, $new_height, $type);
imagecopyresampled($newImg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
return $newImg;
}

5.4. 完整代码:

<?php

/**
* 图片处理,生成缩略图和添加水印图片
* Created by PhpStorm.
* User: andy
* Date: 17-1-3
* Time: 上午11:55
*/
class Image
{
//原图
private $imgPath; //图片地址
private $width; //图片宽度
private $height; //图片高度
private $type; //图片类型
private $img; //图片(图像流) //缩略图
private $newImg; //缩略图(图像流)
private $newWidth;
private $newHeight; //水印图路径
private $waterMarkPath; //输出图像质量,jpg有效
private $quality; /**
* Image constructor.
* @param string $imagePath 图片路径
* @param string $markPath 水印图片路径
* @param int $new_width 缩略图宽度
* @param int $new_height 缩略图高度
* @param int $quality JPG图片格输出质量
*/
public function __construct(string $imagePath,
string $markPath = null,
int $new_width = null,
int $new_height = null,
int $quality = 75)
{
$this->imgPath = $_SERVER['DOCUMENT_ROOT'] . $imagePath;
$this->waterMarkPath = $markPath;
$this->newWidth = $new_width ? $new_width : $this->width;
$this->newHeight = $new_height ? $new_height : $this->height;
$this->quality = $quality ? $quality : 75; list($this->width, $this->height, $this->type) = getimagesize($this->imgPath);
$this->img = $this->_loadImg($this->imgPath, $this->type); //生成缩略图
$this->_thumb();
//添加水印图片
if (!empty($this->waterMarkPath)) $this->_addWaterMark();
//输出图片
$this->_outputImg();
} /**
*图片输出
*/
private function _outputImg()
{
switch ($this->type) {
case 1: // GIF
imagegif($this->newImg, $this->imgPath);
break;
case 2: // JPG
if (intval($this->quality) < 0 || intval($this->quality) > 100) $this->quality = 75;
imagejpeg($this->newImg, $this->imgPath, $this->quality);
break;
case 3: // PNG
imagepng($this->newImg, $this->imgPath);
break;
}
imagedestroy($this->newImg);
imagedestroy($this->img);
} /**
* 添加水印
*/
private function _addWaterMark()
{
$ratio = 1 / 5; //水印缩放比率 $Width = imagesx($this->newImg);
$Height = imagesy($this->newImg); $n_width = $Width * $ratio;
$n_height = $Width * $ratio; list($markWidth, $markHeight, $markType) = getimagesize($this->waterMarkPath); if ($n_width > $markWidth) $n_width = $markWidth;
if ($n_height > $markHeight) $n_height = $markHeight; $Img = $this->_loadImg($this->waterMarkPath, $markType);
$Img = $this->_thumb1($Img, $markWidth, $markHeight, $markType, $n_width, $n_height);
$markWidth = imagesx($Img);
$markHeight = imagesy($Img);
imagecopyresampled($this->newImg, $Img, $Width - $markWidth - 10, $Height - $markHeight - 10, 0, 0, $markWidth, $markHeight, $markWidth, $markHeight);
imagedestroy($Img);
} /**
* 缩略图(按等比例,根据设置的宽度和高度进行裁剪)
*/
private function _thumb()
{ //如果原图本身小于缩略图,按原图长高
if ($this->newWidth > $this->width) $this->newWidth = $this->width;
if ($this->newHeight > $this->height) $this->newHeight = $this->height; //背景图长高
$gd_width = $this->newWidth;
$gd_height = $this->newHeight; //如果缩略图宽高,其中有一边等于原图的宽高,就直接裁剪
if ($gd_width == $this->width || $gd_height == $this->height) {
$this->newWidth = $this->width;
$this->newHeight = $this->height;
} else { //计算缩放比率
$per = 1; if (($this->newHeight / $this->height) > ($this->newWidth / $this->width)) {
$per = $this->newHeight / $this->height;
} else {
$per = $this->newWidth / $this->width;
} if ($per < 1) {
$this->newWidth = $this->width * $per;
$this->newHeight = $this->height * $per;
}
} $this->newImg = $this->_CreateImg($gd_width, $gd_height, $this->type);
imagecopyresampled($this->newImg, $this->img, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height);
} /**
* 缩略图(按等比例)
* @param resource $img 图像流
* @param int $width
* @param int $height
* @param int $type
* @param int $new_width
* @param int $new_height
* @return resource
*/
private function _thumb1($img, $width, $height, $type, $new_width, $new_height)
{ if ($width < $height) {
$new_width = ($new_height / $height) * $width;
} else {
$new_height = ($new_width / $width) * $height;
} $newImg = $this->_CreateImg($new_width, $new_height, $type);
imagecopyresampled($newImg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
return $newImg;
} /**
* 加载图片
* @param string $imgPath
* @param int $type
* @return resource
*/
private function _loadImg($imgPath, $type)
{
switch ($type) {
case 1: // GIF
$img = imagecreatefromgif($imgPath);
break;
case 2: // JPG
$img = imagecreatefromjpeg($imgPath);
break;
case 3: // PNG
$img = imagecreatefrompng($imgPath);
break;
default: //其他类型
Tool::alertBack('不支持当前图片类型.' . $type);
break;
}
return $img;
} /**
* 创建一个背景图像
* @param int $width
* @param int $height
* @param int $type
* @return resource
*/
private function _CreateImg($width, $height, $type)
{
$img = imagecreatetruecolor($width, $height);
switch ($type) {
case 3: //png
imagecolortransparent($img, 0); //设置背景为透明的
imagealphablending($img, false);
imagesavealpha($img, true);
break;
case 4://gif
imagecolortransparent($img, 0);
break;
} return $img;
}
}

Image.class.php

6.调用

调用非常简单,在引入类后,直接new 并输入对应参数即可:

e.g.

new Image($_path, MARK, 400, 200, 100);

7.小结

这个Image 类能够生成缩略图,不出现黑边,添加水印图,能根据图片的大小缩放水印图。当然有个缺点,就是不能缩放GIF的动画,因为涉及到帧的处理,比较麻烦。

PHP-生成缩略图和添加水印图-学习笔记的更多相关文章

  1. WordPress 缩率图学习笔记

    WordPress 缩率图学习笔记 Wordpress在生成缩略图的过程中,有两种不同的规则 缩放模式:缩放模式就是将图片等比例缩小,且新生成的缩略图长度或高度两者之中,有一个是你设置的缩略图的尺寸 ...

  2. C# 动态生成word文档 [C#学习笔记3]关于Main(string[ ] args)中args命令行参数 实现DataTables搜索框查询结果高亮显示 二维码神器QRCoder Asp.net MVC 中 CodeFirst 开发模式实例

    C# 动态生成word文档 本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正. 在工程中引用word的动态库 在项目中,点击项目名称右键-- ...

  3. tensorflow拟合随机生成的三维数据【学习笔记】

    平台信息:PC:ubuntu18.04.i5.anaconda2.cuda9.0.cudnn7.0.5.tensorflow1.10.GTX1060 作者:庄泽彬(欢迎转载,请注明作者) 说明:感谢t ...

  4. 《容器化.NET应用架构指南》脑图学习笔记(第一部分)

    一.关于这本官方“圣经” 作为.NET程序员,对于微软官方推动的架构示例总是特别关注,从PetShop到MusicStore再到eShopOnContainers,每一次关注,都会了解到业界最新的架构 ...

  5. python生成个性二维码学习笔记

    在linux环境下进行编码 1.先进家目录,自行创建Code文件夹 cd Code 2.下载MyQR库 sudo pip3 install MyQR 3.下载所需资源文件并解压 Code/ $ wge ...

  6. 使用ImageProcessor、CodeCarvings.Piczard组件生成缩略图和添加水印

    技术栈: 1.ImageProcessor(专业图像处理,不能合成水印,NetCore中有它的升级版ImageSharp目前是预览包) 2.CodeCarvings.Piczard(缩略图,水印都能搞 ...

  7. E-R图学习笔记

    E-R图也称实体-联系图(Entity Relationship Diagram),提供了表示实体类型.属性和联系的方法,用来描述现实世界的概念模型.   方法 编辑 E-R方法是“实体-联系方法”( ...

  8. UML类图学习笔记

    http://note.youdao.com/noteshare?id=d5af220db7081dda73511fcb7b4da390

  9. Nodejs学习笔记(三)——一张图看懂Nodejs建站

    前言:一条线,竖着放,如果做不到精进至深,那就旋转90°,至少也图个幅度宽广. 通俗解释上面的胡言乱语:还没学会爬,就学起走了?! 继上篇<Nodejs学习笔记(二)——Eclipse中运行调试 ...

随机推荐

  1. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  2. ABP框架 - OData 集成

    文档目录 本节内容: 简介 安装 安装Nuget包 设置模块依赖 配置你的实体 创建控制器 示例 获取实体列表 请求 响应 获取单个实体 请求 响应 获取单个实体及导航属性 请求 响应 查询 请求 响 ...

  3. C++11特性——变量部分(using类型别名、constexpr常量表达式、auto类型推断、nullptr空指针等)

    #include <iostream> using namespace std; int main() { using cullptr = const unsigned long long ...

  4. Swift enum(枚举)使用范例

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  5. 用游标实现查询当前服务器所有数据库所有表的SQL

    declare @name varchar(100) DECLARE My_Cursor CURSOR --定义游标 FOR (SELECT Name FROM Master..SysDatabase ...

  6. 个人网站对xss跨站脚本攻击(重点是富文本编辑器情况)和sql注入攻击的防范

    昨天本博客受到了xss跨站脚本注入攻击,3分钟攻陷--其实攻击者进攻的手法很简单,没啥技术含量.只能感叹自己之前竟然完全没防范. 这是数据库里留下的一些记录.最后那人弄了一个无限循环弹出框的脚本,估计 ...

  7. java时间

    Calendar.getInstance().getTime() 获取当前时间(包括星期和时区 CST China Standard Time):  Fri Jan 06 21:03:36 CST 2 ...

  8. ubuntu 下安装scrapy

    1.把Scrapy签名的GPG密钥添加到APT的钥匙环中: sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 6272 ...

  9. java 线程 Lock 锁使用Condition实现线程的等待(await)与通知(signal)

    一.Condition 类 在前面我们学习与synchronized锁配合的线程等待(Object.wait)与线程通知(Object.notify),那么对于JDK1.5 的 java.util.c ...

  10. i++、++i 、i--、--i

    总结: i++ 先用后加, ++i先加后用: i--先用后减, --i先减后用: //int i = 1; //Console.WriteLine(i);//1 //Console.WriteLine ...