forked from bluezip/ImageEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageEditor.php
More file actions
457 lines (423 loc) · 12.9 KB
/
ImageEditor.php
File metadata and controls
457 lines (423 loc) · 12.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
if (!class_exists('CComponent', false))
require_once dirname(__FILE__).'/CComponent.php';
if (!function_exists('imagecreatefrombmp'))
require_once dirname(__FILE__).'/function.imagecreatefrombmp.php';
/**
* This is a simple image editor
* @author wapmorgan (wapmorgan@gmail.com)
* @link https://github.com/wapmorgan/ImageEditor
* @link https://github.com/wapmorgan/ImageEditor-test
* @link https://github.com/wapmorgan/ImageEditor-doc
* @link http://wapmorgan.github.io/ImageEditor-doc/classes/ImageEditor.html
*/
class ImageEditor extends CComponent {
const ZOOM_IF_LARGER = 1;
/**
* Resource
*/
private $_image;
/**
* Constructor.
* @param image_resource $image
*/
private function __construct($image) {
$this->_image = $image;
}
/**
* Destroys an image resource to free memory
*/
public function __destruct() {
imagedestroy($this->_image);
}
/**
* Creates a new gd-resource.
*/
public function __clone() {
$source = $this->_image;
$width = imagesx($source);
$height = imagesy($source);
$this->_image = imagecreatetruecolor($width, $height);
imagecopy($this->_image, $source, 0, 0, 0, 0, $width, $height);
}
/**
* Returns image width
* @return int
*/
public function getWidth() {
return imagesx($this->_image);
}
/**
* Returns image height
* @return int
*/
public function getHeight() {
return imagesy($this->_image);
}
/**
* Copies an image fron another instance with zoom
* @param ImageEditor $source An image that will be source for resampling
* @return ImageEditor this object
*/
public function copyFrom(ImageEditor $source) {
imagecopyresampled($this->_image, $source->resource, 0, 0, 0, 0, $this->width, $this->height, $source->width, $source->height);
return $this;
}
/**
* Returns an image resource
*/
public function getResource() {
return $this->_image;
}
/**
* Cuts a rectangular piece of image
* @param $x First x
* @param $y First y
* @param $x2 Second x
* @param $y2 Second y
* @return ImageEditor this object
*/
public function crop($x, $y, $x2, $y2) {
$width = $x2 - $x;
$height = $y2 - $y;
$image = imagecreatetruecolor($width, $height);
imagecopy($image, $this->_image, 0, 0, $x, $y, $width, $height);
imagedestroy($this->_image);
$this->_image = $image;
return $this;
}
/**
* Deletes a piece of image from specific side.
* For example, if side=top and size=100, 100px from top will be deleted.
* @param string $side Side to cut
* @param int $size Pixels
* @return ImageEditor this object
*/
public function decreaseSide($side, $size) {
$x = $y = 1;
$x2 = $this->width;
$y2 = $this->height;
switch ($side) {
case 'top':
$y = $size;
break;
case 'right':
$x2 -= $size;
break;
case 'bottom':
$y2 -= $size;
break;
case 'left':
$x = $size;
break;
}
$this->crop($x, $y, $x2, $y2);
return $this;
}
/**
* Resizes an image to new width & height
* @param int $width New image width
* @param int $height New image height
* @return ImageEditor this object
*/
public function resize($width, $height) {
$image = imagecreatetruecolor($width, $height);
imagecopyresampled($image, $this->_image, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
imagedestroy($this->_image);
$this->_image = $image;
return $this;
}
/**
* Changes proportionally image width to new size
* @param int $size New value of width
* @return ImageEditor this object
*/
public function zoomWidthTo($size) {
$ratio = round($this->width / $size, 3);
$this->resize($size, $this->height / $ratio);
return $this;
}
/**
* Changes proportionally image height to new size
* @param int $size New value of height
* @return ImageEditor this object
*/
public function zoomHeightTo($size) {
$ratio = round($this->height / $size, 3);
$this->resize($this->width / $ratio, $size);
return $this;
}
/**
* Decreases proportionally image width to new size, if needed
* @param int $size New value of width
* @return ImageEditor this object
*/
public function decreaseWidthTo($size) {
if ($this->width <= $size)
return;
$this->zoomWidthTo($size);
return $this;
}
/**
* Decreases proportionally image height to new size, if needed
* @param int $size New value of height
* @return ImageEditor this object
*/
public function decreaseHeightTo($size) {
if ($this->height <= $size)
return;
$this->zoomHeightTo($size);
return $this;
}
/**
* Decreases proportionally larger side to new size, if needed
* @param int $size New max value of sides
* @return ImageEditor this object
*/
public function decreaseTo($size) {
$currentSize = max($this->height, $this->width);
if ($currentSize <= $size)
return;
$ratio = round($currentSize / $size, 3);
$this->resize($this->width / $ratio, $this->height / $ratio);
return $this;
}
/**
* Appends an image to current image.
* @param string $side Side
* @param ImageEditor $appendix
* @param int $modifiers Set of modifiers.
* Modifiers:
* + ImageEditor::ZOOM_IF_LARGER - appendix height will be zoomed (not resized) if it's larger than current image's one (when appending to left or right side); appendix width will be zoomed (not resized) if it's larger than current image's one (when appending to top or bottom side);
* @return ImageEditor this object
*/
public function appendImageTo($side, ImageEditor $appendix, $modifiers = 0) {
$appendix = clone $appendix;
switch ($side) {
case 'top':
case 'bottom':
// fix appendix width if needed
if ($appendix->width != $this->width) {
if ($appendix->width > $this->width || $modifiers & self::ZOOM_IF_LARGER)
$appendix->zoomWidthTo($this->width);
elseif ($appendix->width < $this->width)
$appendix->resize($this->width, $appendix->height);
}
$image = imagecreatetruecolor($this->width, $this->height + $appendix->height);
break;
case 'left':
case 'right':
// fix appendix height if needed
if ($appendix->height != $this->height) {
if ($appendix->height > $this->height || $modifiers & self::ZOOM_IF_LARGER)
$appendix->zoomHeightTo($this->height);
else
$appendix->resize($appendix->width, $this->height);
}
$image = imagecreatetruecolor($this->width + $appendix->width, $this->height);
break;
}
// imagecopyresampled(dst_image, src_image, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
switch ($side) {
case 'top':
imagecopyresampled($image, $appendix->resource, 0, 0, 0, 0, $appendix->width, $appendix->height, $appendix->width, $appendix->height);
imagecopyresampled($image, $this->_image, 0, $appendix->height, 0, 0, $this->width, $this->height, $this->width, $this->height);
break;
case 'bottom':
imagecopyresampled($image, $this->_image, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
imagecopyresampled($image, $appendix->resource, 0, $this->height, 0, 0, $appendix->width, $appendix->height, $appendix->width, $appendix->height);
break;
case 'left':
imagecopyresampled($image, $appendix->resource, 0, 0, 0, 0, $appendix->width, $appendix->height, $appendix->width, $appendix->height);
imagecopyresampled($image, $this->_image, $appendix->width, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
break;
case 'right':
imagecopyresampled($image, $this->_image, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
imagecopyresampled($image, $appendix->resource, $this->width, 0, 0, 0, $appendix->width, $appendix->height, $appendix->width, $appendix->height);
break;
}
imagedestroy($this->_image);
$this->_image = $image;
return $this;
}
/**
* Places another image atop current image
* @param int $x X-position
* @param int $y Y-position
* @param ImageEditor $image Image to place
* @return ImageEditor this object
*/
public function placeImageAt($x, $y, ImageEditor $image) {
imagecopy($this->_image, $image->resource, $x, $y, 0, 0, $image->width, $image->height);
return $this;
}
/**
* Places another image in the center of current image
* @param ImageEditor $image Image to place
* @return ImageEditor this object
*/
public function placeImageAtCenter(ImageEditor $image) {
$this->placeImageAt($this->width / 2 - $image->width / 2, $this->height / 2 - $image->height / 2, $image);
return $this;
}
/**
* Rotates an image
* @param mixed $angle An angle to rotate in degrees.
* Also you can pass true or false to rotate
* 90 degress and -90 degrees.
* @param int $bgColor Color of uncovered zone {@see http://www.php.net/manual/en/function.imagerotate.php}
* @return ImageEditor this object
*/
public function rotate($angle, $bgColor = 0) {
if ($angle === true)
$angle = 90;
elseif ($angle === false)
$angle = -90;
$this->_image = imagerotate($this->_image, 360 - $angle, $bgColor);
return $this;
}
/**
* Saves an image to file
* @param string $filename Filename
* @param mixed $format Image format. Can be either a string or a defined constant IMAGETYPE_xxx
* Formats:
* + jpeg (or jpg)
* + png
* + gif
* + wbmp
* @param int $quality Image quality. An abstract value between 0 (worst) and 100 (best).
* When you save to png, it automatically transfers to appliable value.
* If not set, save function will be called without passing quality param.
* @return ImageEditor this object
*/
public function saveToFile($filename, $format, $quality = null) {
switch ($format) {
case IMAGETYPE_JPEG:
case 'jpeg':
case 'jpg':
if ($quality === null)
imagejpeg($this->_image, $filename);
else
imagejpeg($this->_image, $filename, $quality);
break;
case IMAGETYPE_PNG:
case 'png':
if ($quality === null)
imagepng($this->_image, $filename);
else {
$quality = 9 - floor($quality / 11);
imagepng($this->_image, $filename, $quality);
}
break;
case IMAGETYPE_GIF:
case 'gif':
imagegif($this->_image, $filename);
break;
case IMAGETYPE_WBMP:
case 'wbmp':
imagewbmp($this->_image, $filename);
break;
default:
throw new Exception('Unknown (format) "'.$format.'"!');
}
return $this;
}
/**
* Flips an image horizontally.
* @return ImageEditor this object
*/
public function horizontalFlip() {
if (function_exists('imageflip')) {
imageflip($this->_image, IMG_FLIP_HORIZONTAL);
} else {
$image = imagecreatetruecolor($this->width, $this->height);
$dst_y = 0;
$src_y = 0;
$coordinate = ($this->width - 1);
foreach (range($this->width, 0) as $range) {
$src_x = $range;
$dst_x = $coordinate - $range;
imagecopy($image, $this->_image, $dst_x, $dst_y, $src_x, $src_y, 1, $this->height);
}
imagedestroy($this->_image);
$this->_image = $image;
}
return $this;
}
/**
* Flips an image vertically.
* @return ImageEditor this object
*/
public function verticalFlip() {
if (function_exists('imageflip')) {
imageflip($this->_image, IMG_FLIP_VERTICAL);
} else {
$image = imagecreatetruecolor($this->width, $this->height);
$dst_x = 0;
$src_x = 0;
$coordinate = ($this->height - 1);
foreach (range($this->height, 0) as $range) {
$src_y = $range;
$dst_y = $coordinate - $range;
imagecopy($image, $this->_image, $dst_x, $dst_y, $src_x, $src_y, $this->width, 1);
}
imagedestroy($this->_image);
$this->_image = $image;
}
return $this;
}
/**
* Creates an instance from existing file
* @param string $filename
* Note that gif animation will be destroyed.
*/
static public function createFromFile($filename) {
if (!file_exists($filename))
throw new Exception('Invalid (filename) parameter! File does not exist!');
$image_info = getimagesize($filename);
if ($image_info === false)
throw new Exception('Invalid (filename) parameter! File is corrupted!');
switch ($image_info[2]) {
case IMAGETYPE_JPEG:
case IMAGETYPE_JPEG2000:
$image = imagecreatefromjpeg($filename);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($filename);
break;
case IMAGETYPE_GIF:
$image = imagecreatefromgif($filename);
break;
case IMAGETYPE_WBMP:
$image = imagecreatefromwbmp($filename);
break;
case IMAGETYPE_BMP:
$image = imagecreatefrombmp($filename);
break;
default:
throw new Exception('Unknown image format!');
break;
}
return new self($image);
}
/**
* Creates an instance with specified size
* @param int $width Image width
* @param int $height Image height
*/
static public function createWithSize($width, $height) {
$image = imagecreatetruecolor($width, $height);
return new self($image);
}
/**
* Creates an instace from gd-resource
* @param resource $resource Image resource
*/
static public function createFromResource($resource) {
if (!is_resource($resource))
throw new Exception('Invalid (resource) parameter! This is not a resource!');
if (get_resource_type($resource) != 'gd')
throw new Exception('Invalid (resource) parameter! Resource is not a gd resource!');
return new self($resource);
}
}