forked from onanying/image-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.php
More file actions
208 lines (189 loc) · 7.55 KB
/
Copy pathImage.php
File metadata and controls
208 lines (189 loc) · 7.55 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
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
<?php
/**
* ------------------------------------------
* 图片处理类 (GD2库)
* ------------------------------------------
*
* 根据实际需求设计的图片处理类, “居中剪裁”通常使用在用户头像、缩略图等场景, “等比缩放”通常用来压缩图片
*
* 1. 居中剪裁 (宽高自动)
* 2. 等比缩放 (宽高自动, 源图小于宽高的不做缩放)
* 3. 创建缩略图
* 4. 如果操作网络图片, 会在根目录生成"tmp.jpg" (用于测试)
*
*/
class Image
{
protected $source_image = ''; // 源文件路径
protected $width = ''; // 要设置的宽度
protected $height = ''; // 要设置的高度
protected $create_thumb = ''; // 是否创建缩略图
protected $thumb_marker = ''; // 缩略图后缀
public function __construct($config = array())
{
empty($config) or $this->initialize($config);
}
// 初始化配置
public function initialize($config)
{
$this->clear(); // 清除之前的配置
foreach ($config as $key => $val) {
if (isset($this->$key)) {
$this->$key = $val;
}
}
}
// 清除配置
public function clear()
{
$this->source_image = '';
$this->width = '';
$this->height = '';
$this->create_thumb = false;
$this->thumb_marker = '_thumb';
}
// 等比缩放
public function resize()
{
$source_path = $this->source_image;
$target_width = $this->width;
$target_height = $this->height;
list($source_width, $source_height) = $imagesize = getimagesize($source_path);
$source_mime = $imagesize['mime'];
switch ($source_mime) {
case 'image/gif':
$source_func = 'imagecreatefromgif';
$output_func = 'imagegif';
$suffix = '.gif';
break;
case 'image/png':
$source_func = 'imagecreatefrompng';
$output_func = 'imagepng';
$suffix = '.png';
break;
case 'image/jpeg':
$source_func = 'imagecreatefromjpeg';
$output_func = 'imagejpeg';
$suffix = '.jpg';
break;
default:
$source_func = 'imagecreatefromjpeg'; // 兼容app, 许多app上传的图片无mime信息
$output_func = 'imagejpeg';
$suffix = '.jpg';
break;
}
$source_image = $source_func($source_path);
$width_ratio = $target_width / $source_width;
$height_ratio = $target_height / $source_height;
// 源图宽高均小于要设置的值
if ($width_ratio >= 1 && $height_ratio >= 1) {
$target_image = $source_image;
} else {
// 根据缩放倍率小的宽或者高缩放
if ($width_ratio < $height_ratio) {
$zoom_width = $target_width;
$zoom_height = $source_height * ($target_width / $source_width);
} else {
$zoom_height = $target_height;
$zoom_width = $source_width * ($target_height / $source_height);
}
// 声明图片资源
$target_image = imagecreatetruecolor($zoom_width, $zoom_height);
// 保留png透明色
imagealphablending($target_image, false);
imagesavealpha($target_image, true);
// 缩放
imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $zoom_width, $zoom_height, $source_width, $source_height);
}
// 图片地址为url
if (strpos($source_path, 'http') !== false) {
$output_func($target_image, __DIR__ . '/tmp' . $suffix);
} else {
if ($this->create_thumb) {
$source_path = str_replace('.', $this->thumb_marker . '.', $source_path);
}
$output_func($target_image, $source_path);
}
//销毁资源
imagedestroy($source_image);
@imagedestroy($target_image);
}
// 居中剪裁
public function crop()
{
$source_path = $this->source_image;
$target_width = $this->width;
$target_height = $this->height;
list($source_width, $source_height) = $imagesize = getimagesize($source_path);
$source_mime = $imagesize['mime'];
$source_ratio = $source_height / $source_width;
$target_ratio = $target_height / $target_width;
if ($source_ratio > $target_ratio) {
// 源图过高
$cropped_width = $source_width;
$cropped_height = $source_width * $target_ratio;
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2;
} elseif ($source_ratio < $target_ratio) {
// 源图过宽
$cropped_width = $source_height / $target_ratio;
$cropped_height = $source_height;
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
} else {
// 源图适中
$cropped_width = $source_width;
$cropped_height = $source_height;
$source_x = 0;
$source_y = 0;
}
switch ($source_mime) {
case 'image/gif':
$source_func = 'imagecreatefromgif';
$output_func = 'imagegif';
$suffix = '.gif';
break;
case 'image/png':
$source_func = 'imagecreatefrompng';
$output_func = 'imagepng';
$suffix = '.png';
break;
case 'image/jpeg':
$source_func = 'imagecreatefromjpeg';
$output_func = 'imagejpeg';
$suffix = '.jpg';
break;
default:
$source_func = 'imagecreatefromjpeg'; // 兼容app, 许多app上传的图片无mime信息
$output_func = 'imagejpeg';
$suffix = '.jpg';
break;
}
$source_image = $source_func($source_path);
// 声明图片资源
$target_image = imagecreatetruecolor($target_width, $target_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
// 保留png透明色
imagealphablending($target_image, false);
imagesavealpha($target_image, true);
imagealphablending($cropped_image, false);
imagesavealpha($cropped_image, true);
// 裁剪
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
// 缩放
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
// 图片地址为url
if (strpos($source_path, 'http') !== false) {
$output_func($target_image, __DIR__ . '/tmp' . $suffix);
} else {
if ($this->create_thumb) {
$source_path = str_replace('.', $this->thumb_marker . '.', $source_path);
}
$output_func($target_image, $source_path);
}
// 销毁资源
imagedestroy($source_image);
imagedestroy($target_image);
imagedestroy($cropped_image);
}
}