Skip to content

Commit 34c9eb6

Browse files
author
任旺
committed
添加图片处理链接统一拼接方法
1 parent bc9a61e commit 34c9eb6

File tree

4 files changed

+597
-0
lines changed

4 files changed

+597
-0
lines changed

examples/image_process.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
require_once __DIR__ . '/../autoload.php';
3+
4+
// 引入图片处理类
5+
use Qiniu\Processing\ImageProcess;
6+
7+
$imageProcess = new ImageProcess();
8+
9+
// 要处理图片
10+
$url = 'http://78re52.com1.z0.glb.clouddn.com/resource/gogopher.jpg';
11+
$url2 = 'http://78re52.com1.z0.glb.clouddn.com/resource/gogopher.jpg?watermark/1/gravity/SouthEast/dx/0/dy/0/image/aHR0cDovL2Fkcy1jZG4uY2h1Y2h1amllLmNvbS9Ga1R6bnpIY2RLdmRBUFc5cHZZZ3pTc21UY0tB';
12+
$waterImage = 'http://developer.qiniu.com/resource/logo-2.jpg';
13+
14+
/**
15+
* 缩略图链接拼接
16+
*
17+
* @param string $url 图片链接
18+
* @param int $mode 缩略模式
19+
* @param int $width 宽度
20+
* @param int $height 长度
21+
* @param string $format 输出类型 [可选]
22+
* @param int $quality 图片质量 [可选]
23+
* @param int $interlace 是否支持渐进显示 [可选]
24+
* @param int $ignoreError 忽略结果 [可选]
25+
* @return string
26+
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html
27+
* @author Sherlock Ren <[email protected]>
28+
*/
29+
$thumbLink = $imageProcess->thumbnail($url, 1, 100, 100);
30+
31+
// 函数方式调用 也可拼接多个操作参数 图片+水印
32+
$thumbLink2 = \Qiniu\thumbnail($url2, 1, 100, 100);
33+
var_dump($thumbLink, $thumbLink2);
34+
35+
/**
36+
* 图片水印
37+
*
38+
* @param string $url 图片链接
39+
* @param string $image 水印图片链接
40+
* @param numeric $dissolve 透明度 [可选]
41+
* @param string $gravity 水印位置 [可选]
42+
* @param numeric $dx 横轴边距 [可选]
43+
* @param numeric $dy 纵轴边距 [可选]
44+
* @param numeric $watermarkScale 自适应原图的短边比例 [可选]
45+
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html
46+
* @return string
47+
* @author Sherlock Ren <[email protected]>
48+
*/
49+
$waterLink = $imageProcess->waterImg($url, $waterImage);
50+
// 函数调用方法
51+
//$waterLink = \Qiniu\waterImg($url, $waterImage);
52+
var_dump($waterLink);
53+
54+
/**
55+
* 文字水印
56+
*
57+
* @param string $url 图片链接
58+
* @param string $text 文字
59+
* @param string $font 文字字体
60+
* @param string $fontSize 文字字号
61+
* @param string $fontColor 文字颜色 [可选]
62+
* @param numeric $dissolve 透明度 [可选]
63+
* @param string $gravity 水印位置 [可选]
64+
* @param numeric $dx 横轴边距 [可选]
65+
* @param numeric $dy 纵轴边距 [可选]
66+
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html#text-watermark
67+
* @return string
68+
* @author Sherlock Ren <[email protected]>
69+
*/
70+
$textLink = $imageProcess->waterText($url, '你瞅啥', '微软雅黑', 300);
71+
// 函数调用方法
72+
// $textLink = \Qiniu\waterText($url, '你瞅啥', '微软雅黑', 300);
73+
var_dump($textLink);

src/Qiniu/Processing/ImageProcess.php

+292
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
<?php
2+
namespace Qiniu\Processing;
3+
4+
use Qiniu;
5+
6+
/**
7+
* 主要涉及图片的处理
8+
*
9+
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html
10+
*/
11+
final class ImageProcess
12+
{
13+
/**
14+
* mode合法范围值
15+
*
16+
* @var array
17+
*/
18+
protected $modeArr = [0, 1, 2, 3, 4, 5];
19+
20+
/**
21+
* format合法值
22+
*
23+
* @var array
24+
*/
25+
protected $formatArr = ['psd', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'bmp'];
26+
27+
/**
28+
* 水印图片位置合法值
29+
*
30+
* @var array
31+
*/
32+
protected $gravityArr = ['NorthWest', 'North', 'NorthEast', 'West', 'Center', 'East', 'SouthWest', 'South', 'SouthEast'];
33+
34+
/**
35+
* 缩略图链接拼接
36+
*
37+
* @param string $url 图片链接
38+
* @param int $mode 缩略模式
39+
* @param int $width 宽度
40+
* @param int $height 长度
41+
* @param string $format 输出类型
42+
* @param int $quality 图片质量
43+
* @param int $interlace 是否支持渐进显示
44+
* @param int $ignoreError 忽略结果
45+
* @return string
46+
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html
47+
* @author Sherlock Ren <[email protected]>
48+
*/
49+
public function thumbnail(
50+
$url,
51+
$mode,
52+
$width,
53+
$height,
54+
$format = null,
55+
$interlace = null,
56+
$quality = null,
57+
$ignoreError = 1
58+
)
59+
{
60+
// url合法效验
61+
if ( ! $this->isUrl($url)) {
62+
return $url;
63+
}
64+
65+
// 参数合法性效验
66+
if ( ! in_array(intval($mode), $this->modeArr, true)) {
67+
return $url;
68+
}
69+
70+
if ( ! $width || ! $height) {
71+
return $url;
72+
}
73+
74+
$thumbStr = 'imageView2/' . $mode . '/w/' . $width . '/h/' . $height . '/';
75+
76+
// 拼接输出格式
77+
if (
78+
! is_null($format)
79+
&& in_array($format, $this->formatArr)
80+
) {
81+
$thumbStr .= 'format/' . $format . '/';
82+
}
83+
84+
// 拼接渐进显示
85+
if (
86+
! is_null($interlace)
87+
&& in_array(intval($interlace), [0, 1], true)
88+
) {
89+
$thumbStr .= 'interlace/' . $interlace . '/';
90+
}
91+
92+
// 拼接图片质量
93+
if (
94+
! is_null($quality)
95+
&& intval($quality) >= 0
96+
&& intval($quality) <= 100
97+
) {
98+
$thumbStr .= 'q/' . $quality . '/';
99+
}
100+
101+
$thumbStr .= 'ignore-error/' . $ignoreError . '/';
102+
103+
// 如果有query_string用|线分割实现多参数
104+
return $url . ($this->hasQuery($url) ? '|' : '?') . $thumbStr;
105+
}
106+
107+
/**
108+
* 图片水印
109+
*
110+
* @param string $url 图片链接
111+
* @param string $image 水印图片链接
112+
* @param numeric $dissolve 透明度
113+
* @param string $gravity 水印位置
114+
* @param numeric $dx 横轴边距
115+
* @param numeric $dy 纵轴边距
116+
* @param numeric $watermarkScale 自适应原图的短边比例
117+
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html
118+
* @return string
119+
* @author Sherlock Ren <[email protected]>
120+
*/
121+
public function waterImg(
122+
$url,
123+
$image,
124+
$dissolve = 100,
125+
$gravity = 'SouthEast',
126+
$dx = null,
127+
$dy = null,
128+
$watermarkScale = null
129+
) {
130+
// url合法效验
131+
if ( ! $this->isUrl($url)) {
132+
return $url;
133+
}
134+
135+
$waterStr = 'watermark/1/image/' . \Qiniu\base64_urlSafeEncode($image) . '/';
136+
137+
// 拼接水印透明度
138+
if (
139+
is_numeric($dissolve)
140+
&& $dissolve <= 100
141+
) {
142+
$waterStr .= 'dissolve/' . $dissolve . '/';
143+
}
144+
145+
// 拼接水印位置
146+
if (in_array($gravity, $this->gravityArr, true)) {
147+
$waterStr .= 'gravity/' . $gravity . '/';
148+
}
149+
150+
// 拼接横轴边距
151+
if (
152+
! is_null($dx)
153+
&& is_numeric($dx)
154+
) {
155+
$waterStr .= 'dx/' . $dx . '/';
156+
}
157+
158+
// 拼接纵轴边距
159+
if (
160+
! is_null($dy)
161+
&& is_numeric($dy)
162+
) {
163+
$waterStr .= 'dy/' . $dy . '/';
164+
}
165+
166+
// 拼接自适应原图的短边比例
167+
if (
168+
! is_null($watermarkScale)
169+
&& is_numeric($watermarkScale)
170+
&& $watermarkScale > 0
171+
&& $watermarkScale < 1
172+
) {
173+
$waterStr .= 'ws/' . $watermarkScale . '/';
174+
}
175+
176+
// 如果有query_string用|线分割实现多参数
177+
return $url . ($this->hasQuery($url) ? '|' : '?') . $waterStr;
178+
}
179+
180+
/**
181+
* 文字水印
182+
*
183+
* @param string $url 图片链接
184+
* @param string $text 文字
185+
* @param string $font 文字字体
186+
* @param string $fontSize 文字字号
187+
* @param string $fontColor 文字颜色
188+
* @param numeric $dissolve 透明度
189+
* @param string $gravity 水印位置
190+
* @param numeric $dx 横轴边距
191+
* @param numeric $dy 纵轴边距
192+
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html#text-watermark
193+
* @return string
194+
* @author Sherlock Ren <[email protected]>
195+
*/
196+
public function waterText(
197+
$url,
198+
$text,
199+
$font = '黑体',
200+
$fontSize = 0,
201+
$fontColor = null,
202+
$dissolve = 100,
203+
$gravity = 'SouthEast',
204+
$dx = null,
205+
$dy = null
206+
) {
207+
// url合法效验
208+
if ( ! $this->isUrl($url)) {
209+
return $url;
210+
}
211+
212+
$waterStr = 'watermark/2/text/'
213+
. \Qiniu\base64_urlSafeEncode($text) . '/font/'
214+
. \Qiniu\base64_urlSafeEncode($font) . '/';
215+
216+
// 拼接文字大小
217+
if (is_int($fontSize)) {
218+
$waterStr .= 'fontsize/' . $fontSize . '/';
219+
}
220+
221+
// 拼接文字颜色
222+
if (
223+
! is_null($fontColor)
224+
&& $fontColor
225+
) {
226+
$waterStr .= 'fill/' . \Qiniu\base64_urlSafeEncode($fontColor) . '/';
227+
}
228+
229+
// 拼接水印透明度
230+
if (
231+
is_numeric($dissolve)
232+
&& $dissolve <= 100
233+
) {
234+
$waterStr .= 'dissolve/' . $dissolve . '/';
235+
}
236+
237+
// 拼接水印位置
238+
if (in_array($gravity, $this->gravityArr, true)) {
239+
$waterStr .= 'gravity/' . $gravity . '/';
240+
}
241+
242+
// 拼接横轴边距
243+
if (
244+
! is_null($dx)
245+
&& is_numeric($dx)
246+
) {
247+
$waterStr .= 'dx/' . $dx . '/';
248+
}
249+
250+
// 拼接纵轴边距
251+
if (
252+
! is_null($dy)
253+
&& is_numeric($dy)
254+
) {
255+
$waterStr .= 'dy/' . $dy . '/';
256+
}
257+
258+
// 如果有query_string用|线分割实现多参数
259+
return $url . ($this->hasQuery($url) ? '|' : '?') . $waterStr;
260+
}
261+
262+
/**
263+
* 效验url合法性
264+
*
265+
* @param string $url url链接
266+
* @return string
267+
* @author Sherlock Ren <[email protected]>
268+
*/
269+
protected function isUrl($url)
270+
{
271+
$urlArr = parse_url($url);
272+
273+
return $urlArr['scheme']
274+
&& in_array($urlArr['scheme'], ['http', 'https'])
275+
&& $urlArr['host']
276+
&& $urlArr['path'];
277+
}
278+
279+
/**
280+
* 检测是否有query
281+
*
282+
* @param string $url url链接
283+
* @return string
284+
* @author Sherlock Ren <[email protected]>
285+
*/
286+
protected function hasQuery($url)
287+
{
288+
$urlArr = parse_url($url);
289+
290+
return ! empty($urlArr['query']);
291+
}
292+
}

0 commit comments

Comments
 (0)