Skip to content

Commit f27b219

Browse files
authored
Merge pull request #188 from SherlockRen/feature-imageProcess
添加图片处理链接统一拼接方法
2 parents 1b72ee5 + 0227c39 commit f27b219

File tree

5 files changed

+716
-1
lines changed

5 files changed

+716
-1
lines changed

examples/image_url_builder.php

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

0 commit comments

Comments
 (0)