Repository navigation
Expand file tree
/
Copy pathImage.php
More file actions
345 lines (288 loc) · 9.28 KB
/
Copy pathImage.php
File metadata and controls
345 lines (288 loc) · 9.28 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
namespace Magicfier;
use Magicfier\Trans\Resources;
/**
* Class Image
* @package magein
*/
class Image
{
/**
* 图片资源
* @var resource
*/
protected $resource = null;
/**
* 图片宽度
* @var $width
*/
protected $width = 750;
/**
* 图片高度
* @var $height
*/
protected $height = 1334;
/**
* 图片颜色 默认黑色
* @var $color
*/
protected $color = [0, 0, 0];
/**
* 图片alpha
* @var $alpha
*/
protected $alpha = 127;
/**
* Image constructor.
* @param null $image
* @param null $width
* @param null $height
* @param null $color
* @param null $alpha
*/
public function __construct($image = null, $width = null, $height = null, $color = null, $alpha = null)
{
isset($width) && $this->width = $width;
isset($height) && $this->height = $height;
isset($color) && $this->color = $color;
isset($alpha) && $this->alpha = $alpha;
$this->init($image);
}
/**
* @param $image
*/
public function init($image)
{
if ($image !== null && ! is_resource($image)) {
$image = Resources::instance()->get($image);
} else {
$image = Resources::instance()->create($this->width, $this->height);
}
$this->resource = $image;
}
/**
* @return resource
*/
public function get()
{
return $this->resource;
}
/**
* @param bool $download 是否下载 默认否
* @param null $extend
*/
public function output($extend = null, $download = false)
{
if ($download) {
header('Content-type', 'charset=utf-8;application/x-' . $extend);
header('Content-Disposition:attachment');
}
switch ($extend) {
case 'png':
header('Content-type:image/png');
imagepng($this->resource);
break;
case 'gif':
header('Content-type:image/gif');
imagegif($this->resource);
break;
default:
header('Content-type:image/jpeg');
imagejpeg($this->resource);
break;
}
imagedestroy($this->resource);
exit();
}
/**
* @param $filename
* @param null $extend
* @param int $quality
* @return bool
*/
public function save($filename, $extend = null, $quality = 100)
{
switch ($extend) {
case 'png':
imagepng($this->resource, $filename, $quality);
break;
case 'gif':
imagegif($this->resource, $filename);
break;
default:
imagejpeg($this->resource, $filename, $quality);
break;
}
imagedestroy($this->resource);
if (is_file($filename)) {
return true;
}
return false;
}
/**
* 略缩图
* @param int $width
* @param null $height
* @return resource
*/
public function thumb($width = 100, $height = null)
{
$resource_width = imagesx($this->resource);
$resource_height = imagesy($this->resource);
//算出另一个边的长度,得到缩放后的图片宽度和高度
if ($resource_width > $resource_height) {
$image_width = $width;
$image_height = $height ? $height : $resource_height * ($width / $resource_width);
} else {
$image_height = $width;
$image_width = $height ? $height : $resource_width * ($width / $resource_height);
}
// 缩放后的大小
$resource = imagecreatetruecolor($image_width, $image_height);
//目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h
imagecopyresampled($resource, $this->resource, 0, 0, 0, 0, $image_width, $image_height, $resource_width, $resource_height);
return $this->resource = $resource;
}
/**
* 裁剪
* @param int $x
* @param int $y
* @param int $width
* @param int $height
* @return resource
*/
public function cut($x = 0, $y = 0, $width = 100, $height = 100)
{
$canvas = imagecrop($this->resource, ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height]);
return $this->resource = $canvas;
}
/**
* 圆形处理
* @return resource
*/
public function circle()
{
// 获取图片的大小
$width = imagesx($this->resource);
$height = imagesy($this->resource);
$width = min($width, $height);
$height = $width;
// 创建一个全透明的背景图
$resource = Resources::instance()->create($width, $height);
// 将源图片的中的每一个像素取出来填充到创建的图片中(在圆半径内)
$r = $width / 2; //圆半径
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
$rgbColor = imagecolorat($this->resource, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($resource, $x, $y, $rgbColor);
}
}
}
$this->resource = $resource;
return $this->resource;
}
/**
* 添加水印
* @param $image
* @param string $x
* @param string $y
* @return resource
*/
public function water($image, $x = '100%', $y = '100%')
{
if ( ! is_resource($image)) {
$image = Resources::instance()->get($image);
}
$resourceWidth = imagesx($this->resource);
$resourceHeight = imagesy($this->resource);
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
if ( ! is_int($x) && ! is_int($y)) {
$x = intval($x);
$y = intval($y);
$x = $x > 100 ? 100 : $x;
$y = $y > 100 ? 100 : $x;
$x = $resourceWidth * ($x / 100) - $imageWidth;
$y = $resourceHeight * ($y / 100) - $imageHeight;
}
imagecopy($this->resource, $image, $x, $y, 0, 0, $imageWidth, $imageHeight);
return $this->resource;
}
/**
* 合成图片
* @param null $image 需要合成到底图的图片
* @param int $width 目标图片宽度 (合成到底图上时, 根据目标宽高, 可能会对原图拉伸或压缩)
* @param int $height 目标图片高度
* @param int $top 坐标y
* @param int $left 坐标x
* @param null $originWidth 原始图片宽度 (指需要合成到底图的图片) 默认跟随目标宽高
* @param null $originHeight 原始图片高度
* @param null $extend 后缀名或网络类型标识
* @return resource 底图
* @author fisher
* @date 2018/10/26 上午10:38
*/
public function mergeImage($image = null, $extend = null, $width = 0, $height = 0, $top = 0, $left = 0, $originWidth = null, $originHeight = null)
{
if ( ! is_resource($image)) {
$image = Resources::instance()->get($image, $extend);
}
$originHeight = isset($originHeight) ? $originHeight : $height;
$originWidth = isset($originWidth) ? $originWidth : $width;
imagecopyresized($this->resource, $image, $left, $top, 0, 0, $width, $height, $originWidth, $originHeight);
return $this->resource;
}
/**
* 合成文字 - 在底图基础上添加文字
* @param $text //文字内容
* @param null $font //字体文件地址 绝对路径 必传
* @param int $size 文字大小
* @param int $top 坐标y
* @param int $left 坐标x
* @param array $color 颜色值 rgb数组
* @param int $rotation 旋转角度 默认0不旋转
* @return resource 底图
* @author fisher
* @date 2018/10/26 上午10:38
*/
public function mergeText($text, $font = null, $size = 16, $top = 0, $left = 0, $color = [], $rotation = 0)
{
$color = empty($color) ? $this->color : $color;
$color = imagecolorallocate($this->resource, $color['0'], $color['1'], $color['2']);
imagefttext($this->resource, $size, $rotation, $left, $top, $color, $font, $text);
return $this->resource;
}
/**
* 输出字符串 默认base64
* @param string $extend
* @param string $encode
* @param int $quality for jpg
* @return string
* @author fisher
* @date 2018/10/26 上午11:43
*/
public function toString($extend = 'png', $encode = 'base64', $quality = 100)
{
ob_start();
switch ($extend) {
case 'jpg':
case 'jpeg':
imagejpeg($this->resource, null, $quality);
break;
case 'gif':
imagegif($this->resource);
break;
default:
imagepng($this->resource);
break;
}
$resource = ob_get_clean();
if ($encode === 'base64') {
$prefix = 'data:image/' . $extend . ';base64,';
$resource = $prefix . base64_encode($resource);
}
imagedestroy($this->resource);
return $resource;
}
}