ImageManager.php
4 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
<?php
namespace common\helpers;
/**
* 极办公系统的图片资源管理器
* Class ImageManager
* @package app\wx\helpers
*/
class ImageManager
{
/**
* 图片样式定义(不指定样式则读取原图)
*/
public static $STYLE_90 = '90x90';
public static $STYLE_180 = '180x180';
public static $STYLE_280 = '280x280';
public static $STYLE_480 = '480x480';
/**
* 根据系统模块划分, 定义图片一级目录结构
*/
private static $SMART_ROOT_PATH = 'smart/'; // 激活库
private static $CHECK_ROOT_PATH = 'check/'; // 校验库
private static $TEMP_ROOT_PATH = 'tmp/'; // 临时文件库
/**
* 文件名拼接随机串再md5,避免重复
* @param $length
* @return null|string
*/
private static function rand($length)
{
$str = null;
$strPol = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$max = strlen($strPol) - 1;
for ($i = 0; $i < $length; $i++) {
$str .= $strPol[mt_rand(0, $max)];
}
return $str;
}
/**
* @param $path 数据库存储的图片相对路径
* @param null $style 不传递此参数或者null表示读取原图, 否则读取指定样式规格的图片
*/
public static function getUrl($path, $style = null)
{
if (empty($path)) return '';
return ImageServiceHelper::getUrl($path, $style);
}
public static function getVoiceUrl($path)
{
if (empty($path)) return '';
return ImageServiceHelper::getUrl($path);
}
/**
* 添加上传图片
* @param $tmpPath
* @param $savePath
*/
public static function add($tmpPath, $savePath)
{
return ImageServiceHelper::add($tmpPath, $savePath);
}
/**
* 替换图片
* @param $oldPath
* @param $newPath
* @param $tmpPath
*/
public static function replace($oldPath, $newPath, $tmpPath)
{
return ImageServiceHelper::replace($oldPath, $newPath, $tmpPath);
}
/**
* 移动图片存放位置
* @param $oldPath
* @param $newPath
* @return mixed
*/
public static function move($oldPath, $newPath)
{
ImageServiceHelper::move($oldPath, $newPath);
}
/**
* 删除旧图片
* @param $path
*/
public static function delete($path)
{
ImageServiceHelper::delete($path);
}
/**
* 获取[临时图片]的存储路径: tmp/20170509/[16位md5加密串].jpg
* @param $suffix 图片后缀, 如'.jpg'
* @return string
*/
public static function getTempImgPath($suffix)
{
$imageName = md5(time() . self::rand(5)) . '.' . $suffix;
$basePath = self::$TEMP_ROOT_PATH . date("Ymd");
$savePath = $basePath . '/' . $imageName;
return $savePath;
}
/**
* 商品图片存储路径: smart/20170509/[userid]/[16位md5加密串].jpg
* @param $userId
* @param $suffix 图片后缀, 如'.jpg'
* @param $fileName 图片另外的名称,商品图片存储路径
* @return string
*/
public static function getSmartImgPath($userId, $fileName = '', $suffix = 'jpg')
{
$imageName = md5(time() . $userId . $fileName . self::rand(5)) . '.' . $suffix;
$basePath = self::$SMART_ROOT_PATH . date("Ymd") . '/' . $userId;
$savePath = $basePath . '/' . $imageName;
return $savePath;
}
/**
* 商品图片存储路径: IMG_防伪标签uuid_时间戳.jpg
* @param $userId
* @param $suffix 图片后缀, 如'.jpg'
* @param $fileName 图片另外的名称,商品图片存储路径
* @return string
*/
public static function getSmartCheckImgPath($userId, $uuid = '', $suffix = 'jpg')
{
$imageName = "IMG_" . $uuid . "_" . time() . '.' . $suffix;
$basePath = self::$CHECK_ROOT_PATH . date("Ymd") . '/' . $userId;
$savePath = $basePath . '/' . $imageName;
return $savePath;
}
}