Image.php
10.8 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
namespace Grafika\Gd;
use Grafika\Gd\Helper\GifHelper;
use Grafika\ImageType;
use Grafika\ImageInterface;
/**
* Image class for GD.
* @package Grafika\Gd
*/
final class Image implements ImageInterface {
/**
* @var resource GD resource ID.
*/
private $gd;
/**
* @var string File path to image.
*/
private $imageFile;
/**
* @var int Image width in pixels.
*/
private $width;
/**
* @var int Image height in pixels.
*/
private $height;
/**
* @var string Image type. See \Grafika\ImageType
*/
private $type;
/**
* @var string Contains array of animated GIF data.
*/
private $blocks;
/**
* @var bool True if animated GIF.
*/
private $animated;
/**
* Image constructor.
*
* @param resource $gd Must use GD's imagecreate* family of functions to create a GD resource.
* @param string $imageFile
* @param int $width
* @param int $height
* @param string $type
* @param string $blocks
* @param bool $animated
*/
public function __construct( $gd, $imageFile, $width, $height, $type, $blocks = '', $animated = false ) {
$this->gd = $gd;
$this->imageFile = $imageFile;
$this->width = $width;
$this->height = $height;
$this->type = $type;
$this->blocks = $blocks;
$this->animated = $animated;
}
/**
* Method called when 'clone' keyword is used.
*/
public function __clone()
{
$original = $this->gd;
$copy = imagecreatetruecolor($this->width, $this->height);
imagecopy($copy, $original, 0, 0, 0, 0, $this->width, $this->height);
$this->gd = $copy;
}
/**
* Output a binary raw dump of an image in a specified format.
*
* @param string|ImageType $type Image format of the dump.
*
* @throws \Exception When unsupported type.
*/
public function blob( $type = 'PNG' ) {
$type = strtoupper($type);
if ( ImageType::GIF == $type ) {
imagegif( $this->gd );
} else if ( ImageType::JPEG == $type ) {
imagejpeg( $this->gd );
} else if ( ImageType::PNG == $type ) {
imagepng( $this->gd );
} else if ( ImageType::WBMP == $type ) {
imagewbmp( $this->gd );
} else {
throw new \Exception( sprintf( 'File type "%s" not supported.', $type ) );
}
}
/**
* Create Image from image file.
*
* @param string $imageFile Path to image.
*
* @return Image
* @throws \Exception
*/
public static function createFromFile( $imageFile ) {
if ( ! file_exists( $imageFile ) ) {
throw new \Exception( sprintf( 'Could not open "%s". File does not exist.', $imageFile ) );
}
$type = self::_guessType( $imageFile );
if ( ImageType::GIF == $type ) {
return self::_createGif( $imageFile );
} else if ( ImageType::JPEG == $type ) {
return self::_createJpeg( $imageFile );
} else if ( ImageType::PNG == $type ) {
return self::_createPng( $imageFile );
} else if ( ImageType::WBMP == $type ) {
return self::_createWbmp( $imageFile );
} else {
throw new \Exception( sprintf( 'Could not open "%s". File type not supported.', $imageFile ) );
}
}
/**
* Create an Image from a GD resource. The file type defaults to unknown.
*
* @param resource $gd GD resource.
*
* @return Image
*/
public static function createFromCore( $gd ) {
return new self( $gd, '', imagesx( $gd ), imagesy( $gd ), ImageType::UNKNOWN );
}
/**
* Create a blank image.
*
* @param int $width Width in pixels.
* @param int $height Height in pixels.
*
* @return Image
*/
public static function createBlank($width = 1, $height = 1){
return new self(imagecreatetruecolor($width, $height), '', $width, $height, ImageType::UNKNOWN);
}
/**
* Set the blending mode for an image. Allows transparent overlays on top of an image.
*
* @param bool $flag True to enable blending mode.
* @return self
*/
public function alphaBlendingMode( $flag ){
imagealphablending( $this->gd, $flag );
return $this;
}
/**
* Enable/Disable transparency
*
* @param bool $flag True to enable alpha mode.
* @return self
*/
public function fullAlphaMode( $flag ){
if( true === $flag ){
$this->alphaBlendingMode( false ); // Must be false for full alpha mode to work
}
imagesavealpha( $this->gd, $flag );
return $this;
}
/**
* Returns animated flag.
*
* @return bool True if animated GIF.
*/
public function isAnimated() {
return $this->animated;
}
/**
* Get GD resource ID.
*
* @return resource
*/
public function getCore() {
return $this->gd;
}
/**
* Get image file path.
*
* @return string File path to image.
*/
public function getImageFile() {
return $this->imageFile;
}
/**
* Get image width in pixels.
*
* @return int
*/
public function getWidth() {
return $this->width;
}
/**
* Get image height in pixels.
*
* @return int
*/
public function getHeight() {
return $this->height;
}
/**
* Get image type.
*
* @return string
*/
public function getType() {
return $this->type;
}
/**
* Get blocks.
*
* @return string.
*/
public function getBlocks() {
return $this->blocks;
}
/**
* Get histogram from an entire image or its sub-region.
*
* @param array|null $slice Array of slice information. array( array( 0,0), array(100,50)) means x,y is 0,0 and width,height is 100,50
*
* @return array Returns array containing RGBA bins array('r'=>array(), 'g'=>array(), 'b'=>array(), 'a'=>array())
*/
public function histogram($slice = null)
{
$gd = $this->getCore();
if(null === $slice){
$sliceX = 0;
$sliceY = 0;
$sliceW = $this->getWidth();
$sliceH = $this->getHeight();
} else {
$sliceX = $slice[0][0];
$sliceY = $slice[0][1];
$sliceW = $slice[1][0];
$sliceH = $slice[1][1];
}
$rBin = array();
$gBin = array();
$bBin = array();
$aBin = array();
for ($y = $sliceY; $y < $sliceY+$sliceH; $y++) {
for ($x = $sliceX; $x < $sliceX+$sliceW; $x++) {
$rgb = imagecolorat($gd, $x, $y);
$a = ($rgb >> 24) & 0x7F; // 127 in hex. These are binary operations.
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if ( ! isset($rBin[$r])) {
$rBin[$r] = 1;
} else {
$rBin[$r]++;
}
if ( ! isset($gBin[$g])) {
$gBin[$g] = 1;
} else {
$gBin[$g]++;
}
if ( ! isset($bBin[$b])) {
$bBin[$b] = 1;
} else {
$bBin[$b]++;
}
if ( ! isset($aBin[$a])) {
$aBin[$a] = 1;
} else {
$aBin[$a]++;
}
}
}
return array(
'r' => $rBin,
'g' => $gBin,
'b' => $bBin,
'a' => $aBin
);
}
/**
* Load a GIF image.
*
* @param string $imageFile
*
* @return Image
* @throws \Exception
*/
private static function _createGif( $imageFile ){
$gift = new GifHelper();
$bytes = $gift->open($imageFile);
$animated = $gift->isAnimated($bytes);
$blocks = '';
if($animated){
$blocks = $gift->decode($bytes);
}
$gd = @imagecreatefromgif( $imageFile );
if(!$gd){
throw new \Exception( sprintf('Could not open "%s". Not a valid %s file.', $imageFile, ImageType::GIF) );
}
return new self(
$gd,
$imageFile,
imagesx( $gd ),
imagesy( $gd ),
ImageType::GIF,
$blocks,
$animated
);
}
/**
* Load a JPEG image.
*
* @param string $imageFile File path to image.
*
* @return Image
* @throws \Exception
*/
private static function _createJpeg( $imageFile ){
$gd = @imagecreatefromjpeg( $imageFile );
if(!$gd){
throw new \Exception( sprintf('Could not open "%s". Not a valid %s file.', $imageFile, ImageType::JPEG ) );
}
return new self( $gd, $imageFile, imagesx( $gd ), imagesy( $gd ), ImageType::JPEG );
}
/**
* Load a PNG image.
*
* @param string $imageFile File path to image.
*
* @return Image
* @throws \Exception
*/
private static function _createPng( $imageFile ){
$gd = @imagecreatefrompng( $imageFile );
if(!$gd){
throw new \Exception( sprintf('Could not open "%s". Not a valid %s file.', $imageFile, ImageType::PNG) );
}
$image = new self( $gd, $imageFile, imagesx( $gd ), imagesy( $gd ), ImageType::PNG );
$image->fullAlphaMode( true );
return $image;
}
/**
* Load a WBMP image.
*
* @param string $imageFile
*
* @return Image
* @throws \Exception
*/
private static function _createWbmp( $imageFile ){
$gd = @imagecreatefromwbmp( $imageFile );
if(!$gd){
throw new \Exception( sprintf('Could not open "%s". Not a valid %s file.', $imageFile, ImageType::WBMP) );
}
return new self( $gd, $imageFile, imagesx( $gd ), imagesy( $gd ), ImageType::WBMP );
}
/**
* @param $imageFile
*
* @return string
*/
private static function _guessType( $imageFile ){
// Values from http://php.net/manual/en/image.constants.php starting with IMAGETYPE_GIF.
// 0 - unknown,
// 1 - GIF,
// 2 - JPEG,
// 3 - PNG
// 15 - WBMP
list($width, $height, $type) = getimagesize( $imageFile );
unset($width, $height);
if ( 1 == $type) {
return ImageType::GIF;
} else if ( 2 == $type) {
return ImageType::JPEG;
} else if ( 3 == $type) {
return ImageType::PNG;
} else if ( 15 == $type) {
return ImageType::WBMP;
}
return ImageType::UNKNOWN;
}
}