# 图像处理
我们继续前面的文件上传部分,并调用图像扩展类库对上传的图像文件进行相关的处理和保存。
- - [安装扩展](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u5B89u88C5u6269u5C55)
- [示例代码](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u793Au4F8Bu4EE3u7801)
- [控制器定义](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u63A7u5236u5668u5B9Au4E49)
- [模板定义](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u6A21u677Fu5B9Au4E49)
- [示例演示](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u793Au4F8Bu6F14u793A)
- [读取图片](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u8BFBu53D6u56FEu7247)
- [图片裁剪](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u56FEu7247u88C1u526A)
- [生成缩略图](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u751Fu6210u7F29u7565u56FE)
- [图像翻转](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u56FEu50CFu7FFBu8F6C)
- [图片旋转](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u56FEu7247u65CBu8F6C)
- [添加水印](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u6DFBu52A0u6C34u5370)
- [文字水印](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u6587u5B57u6C34u5370)
- [图片保存](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/179623#u56FEu7247u4FDDu5B58)
## 安装扩展
首先使用`Composer`安装`ThinkPHP5`的图像处理类库:
```
<pre class="calibre18">
```
composer <span class="hljs-keyword">require</span> topthink/think-image
```
```
## 示例代码
本示例使用下面的示例代码进行图片上传和处理。
### 控制器定义
然后修改之前创建的`Upload`控制器,添加`picture`方法如下:
```
<pre class="calibre18">
```
<span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Image</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Request</span>;
<span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Upload</span> <span class="hljs-keyword">extends</span> \<span class="hljs-title">think</span>\<span class="hljs-title">Controller</span></span>{
<span class="hljs-comment">// 文件上传表单</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">index</span><span class="hljs-number">()</span></span>{
<span class="hljs-keyword">return</span> <span class="hljs-regexp">$this</span>->fetch();
}
<span class="hljs-comment">// 图片上传处理</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">picture</span><span class="hljs-number">(Request <span class="hljs-regexp">$request</span>)</span></span>{
<span class="hljs-comment">// 获取表单上传文件</span><span class="hljs-regexp">$file</span> = <span class="hljs-regexp">$request</span>->file(<span class="hljs-string">'image'</span>);
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">true</span> !== <span class="hljs-regexp">$this</span>->validate([<span class="hljs-string">'image'</span> => <span class="hljs-regexp">$file</span>], [<span class="hljs-string">'image'</span> => <span class="hljs-string">'require|image'</span>])) {
<span class="hljs-regexp">$this</span>->error(<span class="hljs-string">'请选择图像文件'</span>);
} <span class="hljs-keyword">else</span> {
<span class="hljs-comment">// 读取图片</span><span class="hljs-regexp">$image</span> = Image::open(<span class="hljs-regexp">$file</span>);
<span class="hljs-comment">// 图片处理</span><span class="hljs-keyword">switch</span> (<span class="hljs-regexp">$request</span>->param(<span class="hljs-string">'type'</span>)) {
<span class="hljs-keyword">case</span> <span class="hljs-number">1</span>: <span class="hljs-comment">// 图片裁剪</span><span class="hljs-regexp">$image</span>->crop(<span class="hljs-number">300</span>, <span class="hljs-number">300</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-number">2</span>: <span class="hljs-comment">// 缩略图</span><span class="hljs-regexp">$image</span>->thumb(<span class="hljs-number">150</span>, <span class="hljs-number">150</span>, Image::THUMB_CENTER);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-number">3</span>: <span class="hljs-comment">// 垂直翻转</span><span class="hljs-regexp">$image</span>->flip();
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-number">4</span>: <span class="hljs-comment">// 水平翻转</span><span class="hljs-regexp">$image</span>->flip(Image::FLIP_Y);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-number">5</span>: <span class="hljs-comment">// 图片旋转</span><span class="hljs-regexp">$image</span>->rotate();
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-number">6</span>: <span class="hljs-comment">// 图片水印</span><span class="hljs-regexp">$image</span>->water(<span class="hljs-string">'./logo.png'</span>, Image::WATER_NORTHWEST, <span class="hljs-number">50</span>);
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> <span class="hljs-number">7</span>: <span class="hljs-comment">// 文字水印</span><span class="hljs-regexp">$image</span>->text(<span class="hljs-string">'ThinkPHP'</span>, VENDOR_PATH . <span class="hljs-string">'topthink/think-captcha/assets/ttfs/1.ttf'</span>, <span class="hljs-number">20</span>, <span class="hljs-string">'#ffffff'</span>);
<span class="hljs-keyword">break</span>;
}
<span class="hljs-comment">// 保存图片(以当前时间戳)</span><span class="hljs-regexp">$saveName</span> = <span class="hljs-regexp">$request</span>->time() . <span class="hljs-string">'.png'</span>;
<span class="hljs-regexp">$image</span>->save(ROOT_PATH . <span class="hljs-string">'public/uploads/'</span> . <span class="hljs-regexp">$saveName</span>);
<span class="hljs-regexp">$this</span>->success(<span class="hljs-string">'图片处理完毕...'</span>, <span class="hljs-string">'/uploads/'</span> . <span class="hljs-regexp">$saveName</span>, <span class="hljs-number">1</span>);
}
}
}</span>
```
```
### 模板定义
配合控制器的上传页面模板修改为:
```
<pre class="calibre18">
```
<span class="hljs-regexp"><!doctype html></span><span class="hljs-regexp"><<span class="hljs-operator">html</span>></span><span class="hljs-regexp"><<span class="hljs-operator">head</span>></span><span class="hljs-regexp"><<span class="hljs-operator">meta</span> <span class="hljs-operator">charset</span>=<span class="hljs-string">"UTF-8"</span>></span><span class="hljs-regexp"><<span class="hljs-operator">title</span>></span>图像上传和处理示例<span class="hljs-regexp"></<span class="hljs-operator">title</span>></span><span class="hljs-regexp"><<span class="hljs-operator">style</span>></span><span class="css"><span class="hljs-regexp">body</span> <span class="hljs-operator">{
<span class="hljs-operator"><span class="hljs-title1">font-family</span>:<span class="hljs-string"><span class="hljs-operator">"Microsoft Yahei"</span>,<span class="hljs-operator">"Helvetica Neue"</span>,Helvetica,Arial,sans-serif</span></span>;
<span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"><span class="hljs-number">16px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"><span class="hljs-number">5px</span></span></span>;
}</span><span class="hljs-regexp">.form</span><span class="hljs-operator">{
<span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">15px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"> <span class="hljs-number">16px</span></span></span>;
}</span><span class="hljs-regexp">.form</span> <span class="hljs-regexp">.text</span> <span class="hljs-operator">{
<span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">3px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"><span class="hljs-number">2px</span> <span class="hljs-number">10px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">width</span>:<span class="hljs-string"> <span class="hljs-number">240px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">height</span>:<span class="hljs-string"> <span class="hljs-number">24px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">line-height</span>:<span class="hljs-string"> <span class="hljs-number">28px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">border</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#D4D4D4</span></span></span>;
}</span><span class="hljs-regexp">.form</span> <span class="hljs-regexp">select</span> <span class="hljs-operator">{
<span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">5px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"><span class="hljs-number">2px</span> <span class="hljs-number">10px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">width</span>:<span class="hljs-string"> <span class="hljs-number">150px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">height</span>:<span class="hljs-string"> <span class="hljs-number">30px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">line-height</span>:<span class="hljs-string"> <span class="hljs-number">30px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">border</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#D4D4D4</span></span></span>;
}</span><span class="hljs-regexp">.form</span> <span class="hljs-regexp">.btn</span><span class="hljs-operator">{
<span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"><span class="hljs-number">6px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">6px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">width</span>:<span class="hljs-string"> <span class="hljs-number">120px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"> <span class="hljs-number">16px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">border</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#D4D4D4</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">cursor</span>:<span class="hljs-string"> pointer</span></span>;
<span class="hljs-operator"><span class="hljs-title1">background</span>:<span class="hljs-string"><span class="hljs-title">#eee</span></span></span>;
}</span><span class="hljs-regexp">.form</span> <span class="hljs-regexp">.file</span><span class="hljs-operator">{
<span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"><span class="hljs-number">6px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">6px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">width</span>:<span class="hljs-string"> <span class="hljs-number">220px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"> <span class="hljs-number">16px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">border</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#D4D4D4</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">cursor</span>:<span class="hljs-string"> pointer</span></span>;
<span class="hljs-operator"><span class="hljs-title1">background</span>:<span class="hljs-string"><span class="hljs-title">#eee</span></span></span>;
}</span><span class="hljs-regexp">a</span><span class="hljs-operator">{
<span class="hljs-operator"><span class="hljs-title1">color</span>:<span class="hljs-string"> <span class="hljs-title">#868686</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">cursor</span>:<span class="hljs-string"> pointer</span></span>;
}</span><span class="hljs-operator"><span class="hljs-title1">a</span>:<span class="hljs-string">hover{
text-decoration: underline</span></span>;
}
<span class="hljs-regexp">h2</span><span class="hljs-operator">{
<span class="hljs-operator"><span class="hljs-title1">color</span>:<span class="hljs-string"> <span class="hljs-title">#4288ce</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">font-weight</span>:<span class="hljs-string"> <span class="hljs-number">400</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">6px</span> <span class="hljs-number">0</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"> <span class="hljs-number">6px</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"> <span class="hljs-number">28px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">border-bottom</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#eee</span></span></span>;
}</span><span class="hljs-regexp">div</span><span class="hljs-operator">{
<span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"><span class="hljs-number">8px</span></span></span>;
}</span><span class="hljs-regexp">.info</span><span class="hljs-operator">{
<span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">12px</span> <span class="hljs-number">0</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">border-bottom</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#eee</span></span></span>;
}</span><span class="hljs-regexp">.copyright</span><span class="hljs-operator">{
<span class="hljs-operator"><span class="hljs-title1">margin-top</span>:<span class="hljs-string"> <span class="hljs-number">24px</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">12px</span> <span class="hljs-number">0</span></span></span>;
<span class="hljs-operator"><span class="hljs-title1">border-top</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#eee</span></span></span>;
}</span></span><span class="hljs-regexp"></<span class="hljs-operator">style</span>></span><span class="hljs-regexp"></<span class="hljs-operator">head</span>></span><span class="hljs-regexp"><<span class="hljs-operator">body</span>></span><span class="hljs-regexp"><<span class="hljs-operator">h2</span>></span>图像上传和处理示例<span class="hljs-regexp"></<span class="hljs-operator">h2</span>></span><span class="hljs-regexp"><<span class="hljs-operator">form</span> <span class="hljs-operator">method</span>=<span class="hljs-string">"post"</span> <span class="hljs-operator">enctype</span>=<span class="hljs-string">"multipart/form-data"</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"form"</span> <span class="hljs-operator">action</span>=<span class="hljs-string">"{:url('picture')}"</span>></span>
选择图像文件:<span class="hljs-regexp"><<span class="hljs-operator">input</span> <span class="hljs-operator">type</span>=<span class="hljs-string">"file"</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"file"</span> <span class="hljs-operator">name</span>=<span class="hljs-string">"image"</span>></span><span class="hljs-regexp"><<span class="hljs-operator">br</span>/></span>
选择处理类型:<span class="hljs-regexp"><<span class="hljs-operator">select</span> <span class="hljs-operator">name</span>=<span class="hljs-string">"type"</span>></span><span class="hljs-regexp"><<span class="hljs-operator">option</span> <span class="hljs-operator">value</span>=<span class="hljs-string">"1"</span> <span class="hljs-operator">selected</span>></span>图片裁剪
<span class="hljs-regexp"><<span class="hljs-operator">option</span> <span class="hljs-operator">value</span>=<span class="hljs-string">"2"</span>></span>生成缩略图
<span class="hljs-regexp"><<span class="hljs-operator">option</span> <span class="hljs-operator">value</span>=<span class="hljs-string">"3"</span>></span>垂直翻转
<span class="hljs-regexp"><<span class="hljs-operator">option</span> <span class="hljs-operator">value</span>=<span class="hljs-string">"4"</span>></span>水平翻转
<span class="hljs-regexp"><<span class="hljs-operator">option</span> <span class="hljs-operator">value</span>=<span class="hljs-string">"5"</span>></span>图片旋转
<span class="hljs-regexp"><<span class="hljs-operator">option</span> <span class="hljs-operator">value</span>=<span class="hljs-string">"6"</span>></span>添加图片水印
<span class="hljs-regexp"><<span class="hljs-operator">option</span> <span class="hljs-operator">value</span>=<span class="hljs-string">"7"</span>></span>添加文字水印
<span class="hljs-regexp"></<span class="hljs-operator">select</span>></span><span class="hljs-regexp"><<span class="hljs-operator">br</span>/></span><span class="hljs-regexp"><<span class="hljs-operator">input</span> <span class="hljs-operator">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"btn"</span> <span class="hljs-operator">value</span>=<span class="hljs-string">" 提交 "</span>></span><span class="hljs-regexp"></<span class="hljs-operator">form</span>></span><span class="hljs-regexp"><<span class="hljs-operator">div</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"copyright"</span>></span><span class="hljs-regexp"><<span class="hljs-operator">a</span> <span class="hljs-operator">title</span>=<span class="hljs-string">"官方网站"</span> <span class="hljs-operator">href</span>=<span class="hljs-string">"http://www.thinkphp.cn"</span>></span>ThinkPHP<span class="hljs-regexp"></<span class="hljs-operator">a</span>></span><span class="hljs-regexp"><<span class="hljs-operator">span</span>></span>V5<span class="hljs-regexp"></<span class="hljs-operator">span</span>></span><span class="hljs-regexp"><<span class="hljs-operator">span</span>></span>{ 十年磨一剑-为API开发设计的高性能框架 }<span class="hljs-regexp"></<span class="hljs-operator">span</span>></span><span class="hljs-regexp"></<span class="hljs-operator">div</span>></span><span class="hljs-regexp"></<span class="hljs-operator">body</span>></span><span class="hljs-regexp"></<span class="hljs-operator">html</span>></span>
```
```
### 示例演示
访问下面的URL地址:
```
<pre class="calibre18">
```
<span class="hljs-string">http:</span>
<span class="hljs-comment">//tp5.com/index/upload</span>
```
```
页面显示:
![](https://img.kancloud.cn/65/98/65989118aa274ef6a62231f73d1daa52_665x333.png)
这里仅能上传`png`和`jpg`类型的图像文件,没有选择或者选择其他类型的文件都会提示:
![](https://img.kancloud.cn/ca/b2/cab260060af76f5477a06e0e49a6c4ae_494x273.png)
本节示例选择上传的图片文件为:
![](https://img.kancloud.cn/8e/8a/8e8aafeba7ba3bc7c98af1b3f45b3fc5_768x432.png)
点击上传并提交后,页面显示:
![](https://img.kancloud.cn/d0/39/d03994a4b28c28853e4d132382903d79_448x267.png)
之后会显示处理之后的图片:
![](https://img.kancloud.cn/59/90/5990d2058292213b866ea0c64da75820_300x300.png)
下面来具体讲下各种图片处理操作的用法。
## 读取图片
`think\Image`类提供了`open`方法打开图片文件
```
<pre class="calibre18">
```
<span class="hljs-comment">// 获取上传文件</span><span class="hljs-regexp">$file</span> = <span class="hljs-regexp">$request</span>->file(<span class="hljs-string">'image'</span>);
<span class="hljs-comment">// 读取图片文件</span><span class="hljs-regexp">$image</span> = Image::open(<span class="hljs-regexp">$file</span>);
```
```
这里的`$file`是一个`\think\File`对象(其实可以支持任何`SplFileInfo`或者子对象),也可以直接传入一个图片文件地址:
```
<pre class="calibre18">
```
<span class="hljs-comment">// 读取图片文件</span><span class="hljs-regexp">$image</span> = Image::open(<span class="hljs-string">'./logo.png'</span>);
```
```
> `open`方法不支持读取远程图片
使用open方法读取图片文件后,可以读取图片相关信息:
```
<pre class="calibre18">
```
<span class="hljs-comment">// 返回图片的宽度</span><span class="hljs-regexp">$width</span> = <span class="hljs-regexp">$image</span>->width();
<span class="hljs-comment">// 返回图片的高度</span><span class="hljs-regexp">$height</span> = <span class="hljs-regexp">$image</span>->height();
<span class="hljs-comment">// 返回图片的类型</span><span class="hljs-regexp">$type</span> = <span class="hljs-regexp">$image</span>->type();
<span class="hljs-comment">// 返回图片的mime类型</span><span class="hljs-regexp">$mime</span> = <span class="hljs-regexp">$image</span>->mime();
<span class="hljs-comment">// 返回图片的尺寸数组 [ 图片宽度 , 图片高度 ]</span><span class="hljs-regexp">$size</span> = <span class="hljs-regexp">$image</span>->size();
```
```
## 图片裁剪
图片剪裁使用`crop`方法,用法:
> #### crop(剪裁宽度,剪裁高度,X坐标(默认0),Y坐标(默认0))
示例中图片剪裁的主要代码为
```
<pre class="calibre18">
```
<span class="hljs-regexp">$image</span>->crop(<span class="hljs-number">300</span>, <span class="hljs-number">300</span>);
```
```
表示从左上角开始剪裁宽高都是300的图片,如果需要改变剪裁的坐标位置,可以用:
```
<pre class="calibre18">
```
$image->crop(<span class="hljs-number">300</span>, <span class="hljs-number">300</span>, <span class="hljs-number">100</span>, <span class="hljs-number">50</span>);
```
```
最终的效果变成:
![](https://img.kancloud.cn/e6/22/e622a5167b5a485363cdc4ec81cf56d4_300x300.png)
## 生成缩略图
生成图片缩略图使用`thumb`方法,用法:
> #### thumb(最大宽度,最大高度,裁剪类型)
缩略图剪裁类型包括如下:
剪裁类型(常量=值) 描述 Image::THUMB\_SCALING = 1 等比例缩放(默认类型) Image::THUMB\_FILLED = 2 缩放后填充 Image::THUMB\_CENTER = 3 居中裁剪 Image::THUMB\_NORTHWEST = 4 左上角裁剪 Image::THUMB\_SOUTHEAST = 5 右下角裁剪 Image::THUMB\_FIXED = 6 固定尺寸缩放示例代码中的缩略图效果如图:
![](https://img.kancloud.cn/0a/80/0a803db66f271b201a35e155444fc7b8_150x150.png)
## 图像翻转
图片翻转使用`flip`方法,用法如下:
> #### flip(翻转方式)
翻转方式 常量值 垂直翻转 Image::FLIP\_X=1 水平翻转 Image::FLIP\_Y=2垂直翻转效果:
![](https://img.kancloud.cn/04/2f/042f5a4678a5fc5d1764cc10ac594cac_768x432.png)
水平翻转效果:
![](https://img.kancloud.cn/1f/ae/1fae8deeda771e9625ac6f79ae06be10_768x432.png)
## 图片旋转
图片翻转使用`rotate`方法,用法如下:
> #### rotate(顺时针旋转的度数)
示例采用90旋转的效果如图:
![](https://img.kancloud.cn/80/ee/80eecc9d9e39ed7adf702a3c08a14d36_432x768.png)
## 添加水印
使用`water`方法添加图片水印
> #### water(水印图片,水印位置常量(默认右下角),水印透明度(默认100))
水印位置常量如下:
水印位置 常量值 左上角 Image::WATER\_NORTHWEST=1 上居中 Image::WATER\_NORTH=2 右上角 Image::WATER\_NORTHEAST=3 左居中 Image::WATER\_WEST=4 居中 Image::WATER\_CENTER=5 右居中 Image::WATER\_EAST=6 左下角 Image::WATER\_SOUTHWEST=7 下居中 Image::WATER\_SOUTH=8 右下角 Image::WATER\_SOUTHEAST=9示例代码:
```
<pre class="calibre18">
```
<span class="hljs-regexp">$image</span>->water(<span class="hljs-string">'./logo.png'</span>, Image::WATER_NORTHWEST, <span class="hljs-number">50</span>);
```
```
示例水印效果如图:
![](https://img.kancloud.cn/f7/ea/f7ea5774036c5921ef0490683191af78_768x432.png)
> 为了示例效果,在操作之前,请把下面的logo图片保存到public目录下面。
>
> ![](https://img.kancloud.cn/66/bf/66bfc4f1dec8278b649c7968db986c8d_80x80.png)
## 文字水印
使用`text`方法给图片添加文字(水印)
> #### text(水印文字,字体文件路径,文字大小,文字颜色,文字写入位置,偏移量,文字倾斜角度)
前面四个参数必须,颜色默认为 #000000 文字位置常量和水印位置一样。
示例代码
```
<pre class="calibre18">
```
<span class="hljs-regexp">$image</span>->text(<span class="hljs-string">'ThinkPHP'</span>, VENDOR_PATH . <span class="hljs-string">'topthink/think-captcha/assets/ttfs/1.ttf'</span>, <span class="hljs-number">20</span>, <span class="hljs-string">'#ffffff'</span>);
```
```
![](https://img.kancloud.cn/24/7b/247bf4387c97f84f81f011f66baba279_768x432.png)
> 为了配合演示,我们使用了验证码类库中的字体素材
## 图片保存
前面所有的操作都是对图片进行相关的处理,最后一步就是需要把处理过的图片文件保存下来。
这就需要调用`save`方法进行图片的保存操作
> #### save(保存文件名,图像类型,图像质量,隔行扫描)
示例中使用当前时间戳作为文件名保存图片文件,代码:
```
<pre class="calibre18">
```
<span class="hljs-comment">// 保存图片(以当前时间戳)</span><span class="hljs-regexp">$saveName</span> = <span class="hljs-regexp">$request</span>->time() . <span class="hljs-string">'.png'</span>;
<span class="hljs-regexp">$image</span>->save(ROOT_PATH . <span class="hljs-string">'public/uploads/'</span> . <span class="hljs-regexp">$saveName</span>);
```
```
默认图片保存的质量是`80`,如果希望采用最高的质量保存,可以使用下面的代码:
```
<pre class="calibre18">
```
<span class="hljs-comment">// 保存图片(以当前时间戳)</span><span class="hljs-regexp">$saveName</span> = <span class="hljs-regexp">$request</span>->time() . <span class="hljs-string">'.png'</span>;
<span class="hljs-comment">// 采用最高质量保存图片</span><span class="hljs-regexp">$image</span>->save(ROOT_PATH . <span class="hljs-string">'public/uploads/'</span> . <span class="hljs-regexp">$saveName</span>,<span class="hljs-string">'png'</span>,<span class="hljs-number">100</span>);
```
```
> save方法的第四个参数仅针对jpg格式的图像类型。
- 脕茫隆垄脨貌脩脭
- 脕茫隆垄脨貌脩脭
- 脪禄隆垄禄霉麓隆
- 脪禄隆垄禄霉麓隆
- 露镁隆垄URL潞脥脗路脫脡
- 露镁隆垄URL潞脥脗路脫脡
- 脠媒隆垄脟毛脟贸潞脥脧矛脫娄
- 脠媒隆垄脟毛脟贸潞脥脧矛脫娄
- 脣脛隆垄脢媒戮脻驴芒
- 脣脛隆垄脢媒戮脻驴芒
- 脦氓隆垄虏茅脩炉脫茂脩脭
- 脦氓隆垄虏茅脩炉脫茂脩脭
- 脕霉隆垄脛拢脨脥潞脥鹿脴脕陋
- 拢篓1拢漏脛拢脨脥露篓脪氓
- 拢篓2拢漏禄霉麓隆虏脵脳梅
- 拢篓3拢漏露脕脠隆脝梅潞脥脨脼赂脛脝梅
- 拢篓4拢漏脌脿脨脥脳陋禄禄潞脥脳脭露炉脥锚鲁脡
- 拢篓5拢漏虏茅脩炉路露脦搂
- 拢篓6拢漏脢盲脠毛潞脥脩茅脰陇
- 拢篓7拢漏鹿脴脕陋
- 拢篓8拢漏脛拢脨脥脢盲鲁枚
- 脝脽隆垄脢脫脥录潞脥脛拢掳氓
- 脝脽隆垄脢脫脥录潞脥脛拢掳氓
- 掳脣隆垄碌梅脢脭潞脥脠脮脰戮
- 掳脣隆垄碌梅脢脭潞脥脠脮脰戮
- 戮脜隆垄API驴陋路垄
- 戮脜隆垄API驴陋路垄
- 脢庐隆垄脙眉脕卯脨脨鹿陇戮脽
- 脢庐隆垄脙眉脕卯脨脨鹿陇戮脽
- 脢庐脪禄隆垄脌漏脮鹿
- 脢庐脪禄隆垄脌漏脮鹿
- 脢庐露镁隆垄脭脫脧卯
- Cookie
- Session
- 碌楼脭陋虏芒脢脭
- 脥录脧帽麓娄脌铆
- 脦脛录镁脡脧麓芦
- 脩茅脰陇脗毛
- 赂陆脗录
- A隆垄鲁拢录没脦脢脤芒录炉
- B隆垄3.2潞脥5.0脟酶卤冒
- C隆垄脰煤脢脰潞炉脢媒
- 路卢脥芒脝陋拢潞脩搂脧掳ThinkPHP5碌脛脮媒脠路脳脣脢脝
- 路卢脥芒脝陋拢潞脩搂脧掳ThinkPHP5碌脛脮媒脠路脳脣脢脝