ImageManager.php
4.96 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
<?php
namespace common\helpers;
use Yii;
/**
* 系统的图片资源管理器
* 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';
/**
* 根据系统模块划分, 定义图片一级目录结构
*/
public static $MAINTENANCE_PATH = 'maintenance/'; // 维修厂用户注册的图片路径
public static $ORDER_BROKEN_PATH = 'broken_order/'; // 订单里面车损坏的图片
public static $ORDER_FINISH_PATH = 'finish_order/'; // 维修好车的时候的图片
public static $ORDER_COMMENT_PATH = 'comment_order/'; // 维修评论图片
/**
* @param $path 数据库存储的图片相对路径
* @param null $style 不传递此参数或者null表示读取原图
*/
public static function getUrl($path, $style = null)
{
$baseUrl = Yii::$app->params['frontEndBaseUrl'];
$fullPath = $baseUrl.$path;
if ('min' == $style) {
$fileArr = explode('.', $path);
$end = array_pop($fileArr);
$newPath = implode('.', $fileArr);
$fullPath = $baseUrl.$newPath.'_min.'.$end;
}
return $fullPath;
}
/**
* @param $licensePic
* @param $fileTypePath
* @return string
*/
public static function mvUploadImage($licensePic, $uuid, $fileTypePath = '')
{
$dir = Yii::getAlias('@site') . "/tmp";
$tmpFilePath = $dir.'/'.$licensePic;
$fileArr = explode('.', $licensePic);
$minFileName = $fileArr[0].'_min'.'.'.end($fileArr);
$tmpMinFilePath = $dir.'/'.$minFileName;
$desPathFile = self::genImagePath($uuid, $fileTypePath);
$aimPath = Yii::getAlias('@site')."/".$desPathFile .$licensePic;
FileUtil::moveFile($tmpFilePath, $aimPath,true);
$aimTPath = Yii::getAlias('@site')."/".$desPathFile .$minFileName;
FileUtil::moveFile($tmpMinFilePath, $aimTPath,true);
return [$desPathFile, $licensePic, $minFileName];
}
/**
* @param $uuid
* @param string $fileTypePath
* @return string
*/
public static function genImagePath($uuid, $fileTypePath = '')
{
if (empty($fileTypePath)) {
$catPath = self::$MAINTENANCE_PATH;
} else {
$catPath = $fileTypePath;
}
$desPathFile = "upload/".$catPath.$uuid.'/';
return $desPathFile;
}
private static function _imageCreateFromPath($imgPath)
{
list($width, $height, $type, $attr) = getimagesize($imgPath);
switch ($type) {
case 3: // png
return imagecreatefrompng($imgPath);
case 2: // jpeg
return imagecreatefromjpeg($imgPath);
default:
return null;
}
}
/**
* 上传到服务器临时文件区
* @param $fileInfo $_FILE['file']
* @return array
*/
public static function uploadImg($fileInfo)
{
$dir = Yii::getAlias('@site') . "/tmp";
$fileArr = explode('.', $fileInfo['name']);
$tt = time();
$filename = 'auto_'.$tt.md5($fileInfo['name']).'.'.end($fileArr);
$minFileName = 'auto_'.$tt.md5($fileInfo['name']).'_min'.'.'.end($fileArr);
$saveFilePath = $dir.'/'.$filename;
move_uploaded_file($fileInfo['tmp_name'], $saveFilePath);
$tmpUrl = $tmpMinFile = 'tmp/'.$filename;
$imgSource = self::_imageCreateFromPath($saveFilePath);
if ($imgSource) {
ImageUtils::resizeImage($imgSource, 100, 100, $dir.'/'.$minFileName);
$tmpMinFile = 'tmp/'.$minFileName;
}
return [$filename, $minFileName, $tmpUrl, $tmpMinFile];
}
/**
* @param $imageStr
* @param string $tmpPath
* @return array
*/
public static function saveBase64ImgFileToLocal($imageStr)
{
$somePath = $dir = Yii::getAlias('@site') . "/tmp";
if (!is_dir($somePath)) {
mkdir($somePath, 0777, true);
}
if (!is_writable($somePath)) {
chmod($somePath, 0777);
}
$tt = time();
$image = imagecreatefromstring($imageStr);
$fileNameId = 'auto_'.$tt.md5(date('YmdHis').'_'.mt_rand(100000, 999999));
$minFileName = $fileNameId.'_min.jpg';
$filename = $fileNameId.'.jpg';
$saveFilePath = $somePath."/" .$filename;
imagejpeg($image, $saveFilePath);
$tmpUrl = $tmpMinFile = 'tmp/'.$filename;
$imgSource = self::_imageCreateFromPath($saveFilePath);
if ($imgSource) {
ImageUtils::resizeImage($imgSource, 100, 100, $dir.'/'.$minFileName);
$tmpMinFile = 'tmp/'.$minFileName;
}
return [$filename, $minFileName, $tmpUrl, $tmpMinFile];
}
}