/
Image.php
247 lines (186 loc) · 6.79 KB
/
Image.php
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
<?php
namespace Imagine\Gd;
use Imagine\Exception\InvalidArgumentException;
use Imagine\Exception\OutOfBoundsException;
use Imagine\Exception\RuntimeException;
use Imagine\Color;
use Imagine\ImageInterface;
use Imagine\ImageMetadataInterface;
class Image implements ImageInterface, ImageMetadataInterface
{
protected $width, $height;
protected $resource;
/**
* Constructs a new Image instance using the result of
* imagecreatetruecolor()
*
* @param resource $resource
*/
public function __construct($resource)
{
$this->resource = $resource;
$this->width = imagesx($resource);
$this->height = imagesy($resource);
}
public function __destruct()
{
imagedestroy($this->resource);
}
final public function getHeight()
{
return $this->height;
}
final public function getWidth()
{
return $this->width;
}
final public function copy()
{
$copy = new BlankImage($this->width, $this->height);
if (!imagecopy($copy->resource, $this->resource, 0, 0, 0, 0,
$this->width, $this->height)) {
throw new RuntimeException('Image copy operation failed');
}
return $copy;
}
final public function crop($x, $y, $width, $height)
{
$dest = imagecreatetruecolor();
if ($x < 0 || $y < 0 || $width < 1 || $height < 1 ||
$this->width - ($x + $width) < 0 ||
$this->height - ($y + $height) < 0) {
throw new OutOfBoundsException('Crop coordinates must start at '.
'minimum 0, 0 position from top left corner, crop height and '.
'width must be positive integers and must not exceed the '.
'current image borders');
}
if (!imagecopy($dest, $this->resource, 0, 0, $x, $y, $width, $height)) {
throw new RuntimeException('Image crop operation failed');
}
imagedestroy($this->resource);
$this->resource = $dest;
return $this;
}
final public function paste(ImageInterface $image, $x, $y)
{
if (!$image instanceof self) {
throw new InvalidArgumentException(sprintf('Gd\Image can only '.
'paste() Gd\Image instances, %s given', get_class($image)));
}
$widthDiff = $this->width - ($x + $image->getWidth());
$heightDiff = $this->height - ($y + $image->getHeight());
if ($widthDiff < 0 || $heightDiff < 0) {
throw new OutOfBoundsException(sprintf('Cannot paste image '.
'of width %d and height %d at the x position of %d and y '.
'position of %d, as it exceeds the parent image\'s width by '.
'%d in width and %d in height', $image->getWidth(),
$image->getHeight(), $x, $y, abs($widthDiff), abs($heightDiff)
));
}
if (!imagecopy($this->resource, $image->resource, $x, $y, 0, 0,
$image->getWidth(), $image->getHeight())) {
throw new RuntimeException('Image paste operation failed');
}
return $this;
}
final public function resize($width, $height)
{
$dest = imagecreatetruecolor($width, $height);
if (!imagecopyresampled($dest, $this->resource, 0, 0, 0, 0, $width,
$height, $this->width, $this->height)) {
throw new RuntimeException('Image resize operation failed');
}
imagedestroy($this->resource);
$this->width = $width;
$this->height = $height;
$this->resource = $dest;
return $this;
}
final public function rotate($angle, Color $background = null)
{
$color = $background ? $background : new Color('fff');
if (!($resource = imagerotate($this->resource, $angle, imagecolorexact(
$this->resource, $color->getRed(), $color->getGreen(),
$color->getBlue())))) {
throw new RuntimeException('Image rotate operation failed');
}
imagedestroy($this->resource);
$this->resource = $resource;
return $this;
}
final public function save($path, array $options = array())
{
$info = pathinfo($path);
$format = $info['extension'];
$this->saveOrOutput($format, $options, $path);
return $this;
}
final public function show($format, array $options = array())
{
$this->saveOrOutput($format, $options);
return $this;
}
private function saveOrOutput($format, array $options, $filename = null)
{
if (!$this->supported($format)) {
throw new InvalidArgumentException(sprintf('Saving image in "%s" '.
'format is not supported, please use one of the following '.
'extension: "%s"', $format, implode('", "', $this->supported())
));
}
$save = 'image'.$format;
$args = array($this->resource);
if (null !== $filename) {
$args[] = $filename;
}
if (($format === 'jpeg' || $format === 'png') && isset($options['quality'])) {
$args[] = $options['quality'];
}
if ($format === 'png' && isset($options['filters'])) {
$args[] = $options['filters'];
}
if (($format === 'wbmp' || $format === 'xbm') && isset($options['foreground'])) {
$args[] = $options['foreground'];
}
if (!call_user_func_array($save, $args)) {
throw new RuntimeException('Save operation failed');
}
}
final public function flipHorizontally()
{
$dest = imagecreatetruecolor($this->width, $this->height);
for ($i = 0; $i < $this->width; $i++) {
if (!imagecopy($dest, $this->resource, $i, 0,
($this->width - 1) - $i, 0, 1, $this->height)) {
throw new RuntimeException('Horizontal flip operation failed');
}
}
imagedestroy($this->resource);
$this->resource = $dest;
return $this;
}
final public function flipVertically()
{
$dest = imagecreatetruecolor($this->width, $this->height);
for ($i = 0; $i < $this->height; $i++) {
if (!imagecopy($dest, $this->resource, 0, $i,
0, ($this->height - 1) - $i, $this->width, 1)) {
throw new RuntimeException('Vartical flip operation failed');
}
}
imagedestroy($this->resource);
$this->resource = $dest;
return $this;
}
protected function supported(&$format = null)
{
$formats = array('gif', 'jpeg', 'png', 'wbmp', 'xbm');
if (null === $format) {
return $formats;
}
if ('jpg' === $format) {
$format = 'jpeg';
}
return in_array($format, $formats);
}
}