-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageEditor.php
More file actions
277 lines (253 loc) · 7.2 KB
/
ImageEditor.php
File metadata and controls
277 lines (253 loc) · 7.2 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
<?php
/**
* This is a simple image editor
* @author wapmorgan (wapmorgan@gmail.com)
*/
class ImageEditor extends CComponent {
/**
* 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);
}
/**
* 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
*/
public function copyFrom(ImageEditor $source) {
imagecopyresampled($this->_image, $source->resource, 0, 0, 0, 0, $this->width, $this->height, $source->width, $source->height);
}
/**
* Returns an image resource
*/
public function getResource() {
return $this->_image;
}
/**
* Crops an image within sizes
* @param $x First x
* @param $y First y
* @param $x2 Second x
* @param $y2 Second y
*/
public function crop($x, $y, $x2, $y2) {
// var_dump(func_get_args()); //#DEBUG
$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;
}
/**
* Crops a one of four sides (top, right, bottom, left)
* @param string $side Side to be cropped
* If top or bottom, image height will be changed; if left or right, image width will be changed.
* If top, deletion process will start from the bottom.
* @param int $size New side size
*/
public function cropSide($side, $size) {
$x = $y = 0;
$x2 = $this->width;
$y2 = $this->height;
// var_dump($x, $y, $x2, $y2); //#DEBUG
switch ($side) {
case 'top':
$y2 -= $y2 - $size;
break;
case 'right':
$x += $size;
break;
case 'bottom':
$y += $size;
break;
case 'left':
$x2 -= $size;
break;
}
// var_dump($x, $y, $x2, $y2); //#DEBUG
// return -1; //#DEBUG
$this->crop($x, $y, $x2, $y2);
}
/**
* Resizes an image
* @param int $width New image width
* @param int $height New image height
*/
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;
}
/**
* Zooms an image by width
* @param int $size New value of width
*/
public function zoomWidthTo($size) {
if ($this->width <= $size)
return;
$ratio = round($this->width / $size, 3);
$this->resize($this->width / $ratio, $this->height / $ratio);
}
/**
* Zooms an image by height
* @param int $size New value of height
*/
public function zoomHeightTo($size) {
if ($this->height <= $size)
return;
$ratio = round($this->height / $size, 3);
$this->resize($this->width / $ratio, $this->height / $ratio);
}
/**
* Adds an image to current image.
* @param string $side Side
* @param ImageEditor $appendix
*/
public function appendImageTo($side, ImageEditor $appendix) {
switch ($side) {
case 'top':
case 'bottom':
//#TODO: what will be if $appendix->width is NOT equal $this->width
$image = imagecreatetruecolor($this->width, $this->height + $appendix->height);
break;
case 'left':
case 'right':
//#TODO: what will be if $appendix->height is NOT equal $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 '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;
}
imagedestroy($this->_image);
$this->_image = $image;
}
/**
* Places another image atop current image
* @param int $x X-position
* @param int $y Y-position
* @param ImageEditor $image Image to place
*/
public function placeImageAt($x, $y, ImageEditor $image) {
imagecopy($this->_image, $image->resource, $x, $y, 0, 0, $image->width, $image->height);
}
/**
* Places another image in the center of current image
* @param ImageEditor $image Image to place
*/
public function placeImageAtCenter(ImageEditor $image) {
$this->placeImageAt($this->width / 2 - $image->width / 2, $this->height / 2 - $image->height / 2, $image);
}
/**
* 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}
* @todo Check functionality
*/
public function rotate($angle, $bgColor = -1) {
if ($angle === true)
$angle = 90;
elseif ($angle === false)
$angle = -90;
imagerotate($this->_image, $angle, $bgColor);
}
/**
* Saves an image to file
* @param string $filename Filename
* @param strng $format Image format (e.g. jpeg, png)
* @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.
*/
public function saveToFile($filename, $format, $quality = null) {
switch ($format) {
case 'jpeg':
if ($quality === null)
imagejpeg($this->_image, $filename);
else
imagejpeg($this->_image, $filename, $quality);
break;
case 'png':
if ($quality === null)
imagepng($this->_image, $filename);
else {
$quality = 9 - floor($quality / 11);
var_dump($quality);
imagepng($this->_image, $filename, $quality);
}
break;
default:
throw new Exception('Unknown (format) "'.$format.'"!');
}
}
/**
* 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;
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);
}
}