Images.php
4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* User: Zip
* Date: 15/10/28
* Time: 下午13:08
*/
namespace Plugin;
class Images
{
private static $domain = '.static.yhbimg.com';
private static $default_image = '/2015/08/25/02/01dd632a6e07bfef457ce4beda21dd6413.png';
private static $domainList = array(
'01' => array(
'img10.static.yhbimg.com',
'img11.static.yhbimg.com',
),
'02' => array(
'img12.static.yhbimg.com',
'img13.static.yhbimg.com'
),
'yhb-head' => 'head.static.yhbimg.com'
);
private static $staticDomain = array(
'bucket' => ''
);
private static $qiniuDomain = 'yhfair.qiniudn.com';
/**
* 缩略图模板
* @param $fileName
* @param $bucket
* @param string $position ()
* @param string $background
* @return string
*/
static function template($fileName, $bucket = 'yhfair', $mode = 1)
{
return self::url($fileName, $bucket, $mode);
}
/**
*
* @param unknown $fileName
* @param string $bucket
* @return string
*/
public static function getSourceUrl($fileName, $bucket = 'yhfair')
{
if(preg_match('|http://|', $fileName)){
return $fileName;
}
$domain = self::getDomain($bucket, $fileName);
return 'http://' . $domain . '/' . $bucket . $fileName;
}
/**
* 根据尺寸获得图片url
* @param unknown $fileName
* @param unknown $width
* @param unknown $height
* @param number $mode
* @param string $bucket
* @return mixed
*/
public static function getImageUrl($fileName, $width, $height, $mode = 1, $bucket = 'yhfair'){
if(!preg_match('|http://|', $fileName)){
$fileName = self::template($fileName, $bucket, $mode);
}
return str_replace('{width}', $width, str_replace('{height}', $height, str_replace('{mode}', $mode, $fileName)));
}
/**
* 缩略图模板
* @param $fileName
* @param $bucket
* @param string $position ()
* @param string $background
* @return string
*/
static function template2($fileName, $bucket, $position = 'center', $background = 'd2hpdGU=')
{
$domain = self::getDomain($bucket, $fileName);
$key = $bucket . $fileName;
return self::MakeBaseUrl($domain, $key) . '?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/' . $background . '/position/' . $position.'/quality/90';
}
/**
* 获取图片URL模板
* @param $fileName
* @param int $mode
* @param string $bucket
* @return string
*/
public static function url($fileName, $bucket = 'yhfair', $mode = 1)
{
$domain = self::getDomain($bucket, $fileName);
return self::getImgTemplateUrl($bucket . $fileName, $mode, $domain);
}
public static function getDomain($bucket, $fileName)
{
$domain = '';
if(!empty(self::$domainList[$bucket])){
$domain = self::$domainList[$bucket];
}else{
$node = mb_substr($fileName, 15, 2);
if (!empty(self::$domainList[$node])) {
$domainList = self::$domainList[$node];
$nodeNum = sprintf('%u', crc32($fileName)) % count($domainList);
$domain = $domainList[$nodeNum];
}
}
return $domain;
}
/**
* 获取模板的图片地址
* @param $fileName
* @param int $width
* @param int $height
* @param int $mode
* @param null $domain
* @return string
*/
private static function getImgTemplateUrl($fileName, $mode = 1, $domain = null)
{
if ($domain == null) {
$domain = self::$qiniuDomain;
}
$baseUrl = self::MakeBaseUrl($domain, $fileName);
return self::MakeTemplateRequest($baseUrl);
}
private static function MakeBaseUrl($domain, $key) // => $baseUrl
{
$keyEsc = str_replace("%2F", "/", rawurlencode($key));
return "http://$domain/$keyEsc";
}
private static function MakeTemplateRequest($url)
{
$ops = array();
$ops[] = '{mode}';
$ops[] = 'w/{width}';
$ops[] = 'h/{height}';
if (empty($ops)) {
return $url;
}
return $url . "?imageView/" . implode('/', $ops);
}
/**
* 获取老的图片地址
* @param $fileName
* @param $bucket
* @param $width
* @param $height
* @param string $position
* @param string $background
* @return mixed
*/
public static function getUrl($fileName,$width,$height,$bucket, $position = 'center', $background = 'd2hpdGU=')
{
if(empty($fileName)){
$fileName = self::$default_image;
}
$url = self::template2($fileName, $bucket, $position, $background);
return str_replace(array('{width}','{height}'),array($width,$height),$url);
}
}