-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathImageMagick.php
More file actions
245 lines (202 loc) · 5.65 KB
/
ImageMagick.php
File metadata and controls
245 lines (202 loc) · 5.65 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
<?php
/**
* 图像操作类(可以操作 GIF),主要基于 PHP Imagick 扩展
* Created by thenbsp (thenbsp@gmail.com)
*/
class ImageMagick {
/**
* Imagick instance
*/
protected $im;
/**
* 显示错误消息
*/
protected $error;
/**
* 另存为名称
*/
protected $targetName;
/**
* 图像水印路径
*/
protected $watermarkFile = NULL;
/**
* 水印位置
*/
protected $watermarkPosition = 9;
/**
* 构造方法
*/
public function __construct($image = NULL) {
// This check is an alternative to the previous one.
if ( ! class_exists('Imagick') ) {
throw new Exception('Imagick class does not exist');
}
if( ! is_file($image) ) {
throw new Exception('Invalid file');
}
if( ! is_writable($image) ) {
throw new Exception('File unreadable');
}
if( is_null($this->targetName) ) {
$this->targetName = $image;
}
$this->im = new Imagick($image);
}
/**
* 析构方法
*/
public function __destruct() {
$this->clear();
}
/**
* 销毁对象
*/
public function clear() {
if( $this->im instanceof Imagick ) {
$this->im->clear();
}
}
/**
* 设置另存为名称
*/
public function setTargetName($targetName) {
$this->targetName = $targetName;
}
/**
* 设置图像水印
*/
public function setWatermarkFile($filepath) {
if( is_file($filepath) ) {
$this->watermarkFile = $filepath;
}
}
/**
* 获取错误消息
*/
public function getError() {
return $this->error;
}
/**
* 获取宽度
*/
public function getWidth() {
return $this->im->getImageWidth();
}
/**
* 获取高度
*/
public function getHeight() {
return $this->im->getImageHeight();
}
/**
* 获取文件大小(单位:字节)
*/
public function getLength() {
return $this->im->getImageLength();
}
/**
* 获取帧数
*/
public function getFrames() {
return $this->im->getNumberImages();
}
/**
* 获取格式
*/
public function getFormat($upper = FALSE) {
$format = $this->im->getImageFormat();
$format = strtolower($format);
// jpeg to jpg
if( $format === 'jpeg' ) {
$format = 'jpg';
}
return $upper ? strtoupper($format) : $format;
}
/**
* 检测是否为指定格式
*/
public function isFormat($format) {
return (strtolower($format) === $this->getFormat());
}
/**
* 根据宽度缩放
*/
public function resize($maxWidth, $minWidth = 0) {
$width = $this->getWidth();
// 如果在范围内就不缩放了
if( ($width <= $maxWidth) AND ($width >= $minWidth) ) {
return;
}
if( $width > $maxWidth ) {
$width = $maxWidth;
} else {
$width = (($minWidth > 0 ) AND ($width < $minWidth)) ? $minWidth : $width;
}
if( $this->getFrames() > 1 ) {
foreach($this->im AS $frame) {
$frame->resizeImage($width, NULL, Imagick::FILTER_SINC, NULL);
}
$this->im->writeImages($this->targetName, TRUE);
} else {
$this->im->resizeImage($width, NULL, Imagick::FILTER_SINC, NULL);
$this->im->writeImage($this->targetName);
}
}
/**
* 提取预览图
*/
public function preview() {
if( $this->getFrames() > 1 ) {
foreach($this->im AS $frame) {
$this->im->writeImage($this->targetName);
break;
}
} else {
$this->im->writeImage($this->targetName);
}
}
/**
* 添加水印
*/
public function watermark() {
if( ! is_file($this->watermarkFile) ) {
throw new Exception('Watermark file not found');
}
$watermark = new Imagick($this->watermarkFile);
if( $watermark->getImageWidth() > $this->getWidth() ) {
throw new Exception('Watermark file too width');
}
if( $watermark->getImageHeight() > $this->getHeight() ) {
throw new Exception('Watermark file too height');
}
$dw = new ImagickDraw();
$dw->setFillOpacity (0.8);
$dw->setGravity($this->watermarkPosition);
$dw->composite($watermark->getImageCompose(), 0, 0, 0, 0, $watermark);
if( $this->getFrames() > 1 ) {
foreach($this->im AS $frame) {
$this->im->drawImage($dw);
}
$this->im->writeImages($this->targetName, TRUE);
} else {
$this->im->drawImage($dw);
$this->im->writeImage($this->targetName);
}
}
/**
* 根据坐标裁剪
*/
public function crop($w, $h, $x, $y) {
if( $this->getFrames() > 1 ) {
foreach($this->im AS $frame) {
$frame->cropImage($w, $h, $x, $y);
$frame->setImagePage($w, $h, 0, 0);
}
$this->im->writeImages($this->targetName, TRUE);
} else {
$this->im->cropImage($w, $h, $x, $y);
$this->im->writeImage($this->targetName);
}
}
}