forked from fecshop/yii2_fecshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.php
263 lines (236 loc) · 8.93 KB
/
Image.php
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\services;
use fec\helpers\CDir;
use Yii;
use yii\base\InvalidValueException;
/**
* Image services.
* @author Terry Zhao <[email protected]>
* @since 1.0
*/
class Image extends Service
{
/**
* absolute image save floder.
*/
public $imageFloder = 'media/upload';
/**
* upload image max size (MB).
*/
public $maxUploadMSize = 2;
/**
* allow image type.
*/
public $allowImgType = [
'image/jpeg',
'image/gif',
'image/png',
'image/jpg',
'image/pjpeg',
];
protected $_maxUploadSize;
public $appbase;
/**
* @property $str | String 图片的相对路径
* @property $app | String @appimage下面的文件夹的名称。各个名称对应各个入口的名字,譬如common appfront appadmin等
* @return 返回图片的绝对路径。
*/
protected function actionGetImgDir($str = '', $app = 'common')
{
if ($appbase = $this->appbase) {
if (isset($appbase[$app]['basedir'])) {
if ($str) {
return Yii::getAlias($appbase[$app]['basedir'].'/'.$str);
}
return Yii::getAlias($appbase[$app]['basedir']);
}
}
}
/**
* @property $str | String 图片的相对路径
* @property $app | String @appimage下面的文件夹的名称。各个名称对应各个入口的名字,譬如common appfront appadmin等
* @return 返回图片的完整URL
*/
protected function actionGetImgUrl($str, $app = 'common')
{
//echo "$str,$app";
if ($appbase = $this->appbase) {
if (isset($appbase[$app]['basedomain'])) {
if ($str) {
return $appbase[$app]['basedomain'].'/'.$str;
}
return $appbase[$app]['basedomain'];
}
}
}
/**
* @property $app | String @appimage下面的文件夹的名称。各个名称对应各个入口的名字,譬如common appfront appadmin等
* @return 返回图片存放目录的绝对路径。
*/
protected function actionGetBaseImgDir($app = 'common')
{
return $this->getImgDir('', $app);
}
/**
* @property $app | String @appimage下面的文件夹的名称。各个名称对应各个入口的名字,譬如common appfront appadmin等
* @return 返回图片存放目录的URL
*/
protected function actionGetBaseImgUrl($app = 'common')
{
return $this->getImgUrl('', $app);
}
/**
* @property $uploadSize | Int , 多少MB
* 设置上传图片的最大的size. 参数单位为MB
*/
protected function actionSetMaxUploadSize($uploadSize)
{
$this->_maxUploadSize = $uploadSize * 1024 * 1024;
}
/**
* 得到上传图片的最大的size.
*/
protected function actionGetMaxUploadSize()
{
if (!$this->_maxUploadSize) {
if ($this->maxUploadMSize) {
$this->_maxUploadSize = $this->maxUploadMSize * 1024 * 1024;
}
}
return $this->_maxUploadSize;
}
/**
* 得到(上传)保存图片所在相对根目录的url路径.
*/
protected function actionGetCurrentBaseImgUrl()
{
return $this->GetImgUrl($this->imageFloder, 'common');
}
/**
* 得到(上传)保存图片所在相对根目录的文件夹路径.
*/
protected function actionGetCurrentBaseImgDir()
{
return $this->GetImgDir($this->imageFloder, 'common');
}
/**
* @property $str | String , 图片的相对路径字符串
* 通过图片的相对路径得到产品图片的url.
*/
protected function actionGetUrlByRelativePath($str)
{
return $this->GetImgUrl($this->imageFloder.$str, 'common');
}
/**
* @property $str | String , 图片的相对路径字符串
* 通过图片的相对路径得到产品图片的绝对路径.
*/
protected function actionGetDirByRelativePath($str)
{
return $this->GetImgDir($this->imageFloder.$str, 'common');
}
/**
* @property $name | String , 图片的原始名字,也就是图片上传的时候的名字。
* @property $length | String , 生成图片随机字符的长度。
* 随机生成图片的新名字,因为有的图片名字可能是中文或者其他语言,而fecshop在保存名字的时候会取名字的前2个字母生成2层文件夹
* 这样中文名字就会出现问题,因此需要使用随机生成的名字(生成2层文件夹,是为了让文件夹下面不至于太多的文件,linux文件夹下的文件超过几万个,查找文件就会有点慢,这样做是为了避免这个文件。)
*/
protected function generateImgName( $name,$length = 15 ) {
$arr = explode('.',$name);
$fileType = '.'.$arr[count($arr)-1];
// 密码字符集,可任意添加你需要的字符
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
$str ='';
for ( $i = 0; $i < $length; $i++ )
{
// 这里提供两种字符获取方式
// 第一种是使用 substr 截取$chars中的任意一位字符;
// 第二种是取字符数组 $chars 的任意元素
// $str .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
$str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
$str .= time();
return $str.$fileType;
}
/**
* @property $param_img_file | Array .
* 上传产品图片,
* 如果成功,保存产品相对路径,譬如: '/b/i/big.jpg'
* 如果失败,reutrn false;
*/
protected function actionSaveUploadImg($FILE)
{
$size = $FILE['size'];
$file = $FILE['tmp_name'];
$name = $FILE['name'];
$name = $this->generateImgName($name);
if ($size > $this->getMaxUploadSize()) {
throw new InvalidValueException('upload image is to max than'. $this->getMaxUploadSize().' MB');
} elseif (!($img = getimagesize($file))) {
throw new InvalidValueException('file type is empty.');
} elseif ($img = getimagesize($file)) {
$imgType = $img['mime'];
if (!in_array($imgType, $this->allowImgType)) {
throw new InvalidValueException('image type is not allow for '.$imgType);
}
}
// process image name.
$imgSavedRelativePath = $this->getImgSavedRelativePath($name);
$isMoved = @move_uploaded_file($file, $this->GetCurrentBaseImgDir().$imgSavedRelativePath);
if ($isMoved) {
$imgUrl = $this->getUrlByRelativePath($imgSavedRelativePath);
$imgPath = $this->getDirByRelativePath($imgSavedRelativePath);
return [$imgSavedRelativePath, $imgUrl, $imgPath];
} else {
return false;
}
}
/**
* get Image save file path, if floder is not exist, this function will create floder.
* if image file is exsit , image file name will be change to a not existed file name( by add radom string to file name ).
* return image saved relative path , like /a/d/advert.jpg.
*/
protected function getImgSavedRelativePath($name)
{
list($imgName, $imgType) = explode('.', $name);
if (!$imgName || !$imgType) {
throw new InvalidValueException('image file name and type is not correct');
}
if (strlen($imgName) < 2) {
$imgName .= time(). mt_rand(100, 999);
}
$first_str = substr($imgName, 0, 1);
$two_str = substr($imgName, 1, 2);
$imgSaveFloder = CDir::createFloder($this->GetCurrentBaseImgDir(), [$first_str, $two_str]);
if ($imgSaveFloder) {
$imgName = $this->getUniqueImgNameInPath($imgSaveFloder, $imgName, $imgType);
$relative_floder = '/'.$first_str.'/'.$two_str.'/';
return $relative_floder.$imgName;
}
return false;
}
/**
* @property $imgSaveFloder|string image save Floder absolute Path
* @property $name|string , image file name ,not contain image suffix.
* @property $imageType|string , image file suffix. like '.gif','jpg'
* return saved Image Name.
* 得到产品保存的唯一路径,因为可能存在名字重复的问题,因此使用该函数确保图片路径唯一。
*/
protected function getUniqueImgNameInPath($imgSaveFloder, $name, $imageType, $randStr = '')
{
$imagePath = $imgSaveFloder.'/'.$name.$randStr.'.'.$imageType;
if (!file_exists($imagePath)) {
return $name.$randStr.'.'.$imageType;
} else {
$randStr = time().rand(10000, 99999);
return $this->getUniqueImgNameInPath($imgSaveFloder, $name, $imageType, $randStr);
}
}
}