-
Notifications
You must be signed in to change notification settings - Fork 530
/
Image.php
794 lines (687 loc) · 25 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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Gd;
use Imagine\Driver\InfoProvider;
use Imagine\Exception\InvalidArgumentException;
use Imagine\Exception\OutOfBoundsException;
use Imagine\Exception\RuntimeException;
use Imagine\Factory\ClassFactoryInterface;
use Imagine\Image\AbstractImage;
use Imagine\Image\BoxInterface;
use Imagine\Image\Fill\FillInterface;
use Imagine\Image\Format;
use Imagine\Image\ImageInterface;
use Imagine\Image\Metadata\MetadataBag;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Image\Palette\Color\RGB as RGBColor;
use Imagine\Image\Palette\PaletteInterface;
use Imagine\Image\Point;
use Imagine\Image\PointInterface;
use Imagine\Image\ProfileInterface;
use Imagine\Utils\ErrorHandling;
/**
* Image implementation using the GD library.
*/
final class Image extends AbstractImage implements InfoProvider
{
/**
* @var resource|\GdImage
*/
private $resource;
/**
* @var \Imagine\Gd\Layers|null
*/
private $layers;
/**
* @var \Imagine\Image\Palette\PaletteInterface
*/
private $palette;
/**
* Constructs a new Image instance.
*
* @param resource|\GdImage $resource
* @param \Imagine\Image\Palette\PaletteInterface $palette
* @param \Imagine\Image\Metadata\MetadataBag $metadata
*/
public function __construct($resource, PaletteInterface $palette, MetadataBag $metadata)
{
$this->metadata = $metadata;
$this->palette = $palette;
$this->resource = $resource;
}
/**
* Makes sure the current image resource is destroyed.
*/
public function __destruct()
{
if ($this->resource) {
if (is_resource($this->resource) && get_resource_type($this->resource) === 'gd' || $this->resource instanceof \GdImage) {
imagedestroy($this->resource);
}
$this->resource = null;
}
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\AbstractImage::__clone()
*/
public function __clone()
{
parent::__clone();
$size = $this->getSize();
$copy = $this->createImage($size, 'copy');
if (imagecopy($copy, $this->resource, 0, 0, 0, 0, $size->getWidth(), $size->getHeight()) === false) {
imagedestroy($copy);
throw new RuntimeException('Image copy operation failed');
}
$this->resource = $copy;
$this->palette = clone $this->palette;
if ($this->layers !== null) {
$this->layers = $this->getClassFactory()->createLayers(ClassFactoryInterface::HANDLE_GD, $this, $this->layers->key());
}
}
/**
* {@inheritdoc}
*
* @see \Imagine\Driver\InfoProvider::getDriverInfo()
* @since 1.3.0
*/
public static function getDriverInfo($required = true)
{
return DriverInfo::get($required);
}
/**
* Returns Gd resource.
*
* @return resource|\GdImage
*/
public function getGdResource()
{
return $this->resource;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ManipulatorInterface::copy()
*/
final public function copy()
{
return clone $this;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ManipulatorInterface::crop()
*/
final public function crop(PointInterface $start, BoxInterface $size)
{
if (!$start->in($this->getSize())) {
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');
}
$width = $size->getWidth();
$height = $size->getHeight();
$dest = $this->createImage($size, 'crop');
if (imagecopy($dest, $this->resource, 0, 0, $start->getX(), $start->getY(), $width, $height) === false) {
imagedestroy($dest);
throw new RuntimeException('Image crop operation failed');
}
imagedestroy($this->resource);
$this->resource = $dest;
return $this;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ManipulatorInterface::paste()
*/
final public function paste(ImageInterface $image, PointInterface $start, $alpha = 100)
{
if (!$image instanceof self) {
throw new InvalidArgumentException(sprintf('Gd\Image can only paste() Gd\Image instances, %s given', get_class($image)));
}
$alpha = (int) round($alpha);
if ($alpha < 0 || $alpha > 100) {
throw new InvalidArgumentException(sprintf('The %1$s argument can range from %2$d to %3$d, but you specified %4$d.', '$alpha', 0, 100, $alpha));
}
$size = $image->getSize();
if ($alpha === 100) {
imagealphablending($this->resource, true);
imagealphablending($image->resource, true);
$success = imagecopy($this->resource, $image->resource, $start->getX(), $start->getY(), 0, 0, $size->getWidth(), $size->getHeight());
imagealphablending($this->resource, false);
imagealphablending($image->resource, false);
if ($success === false) {
throw new RuntimeException('Image paste operation failed');
}
} elseif ($alpha > 0) {
if (imagecopymerge(/*dst_im*/$this->resource, /*src_im*/$image->resource, /*dst_x*/$start->getX(), /*dst_y*/$start->getY(), /*src_x*/0, /*src_y*/0, /*src_w*/$size->getWidth(), /*src_h*/$size->getHeight(), /*pct*/$alpha) === false) {
throw new RuntimeException('Image paste operation failed');
}
}
return $this;
}
/**
* {@inheritdoc}
*
* Please remark that GD doesn't support different filters, so the $filter argument is ignored.
*
* @see \Imagine\Image\ManipulatorInterface::resize()
*/
final public function resize(BoxInterface $size, $filter = ImageInterface::FILTER_UNDEFINED)
{
if (!in_array($filter, static::getAllFilterValues(), true)) {
throw new InvalidArgumentException('Unsupported filter type');
}
$width = $size->getWidth();
$height = $size->getHeight();
$dest = $this->createImage($size, 'resize');
imagealphablending($this->resource, true);
imagealphablending($dest, true);
$success = imagecopyresampled($dest, $this->resource, 0, 0, 0, 0, $width, $height, imagesx($this->resource), imagesy($this->resource));
imagealphablending($this->resource, false);
imagealphablending($dest, false);
if ($success === false) {
imagedestroy($dest);
throw new RuntimeException('Image resize operation failed');
}
imagedestroy($this->resource);
$this->resource = $dest;
return $this;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ManipulatorInterface::rotate()
*/
final public function rotate($angle, ColorInterface $background = null)
{
if ($background === null) {
$background = $this->palette->color('fff');
}
$color = $this->getColor($background);
$resource = imagerotate($this->resource, -1 * $angle, $color);
if ($resource === false) {
throw new RuntimeException('Image rotate operation failed');
}
imagedestroy($this->resource);
$this->resource = $resource;
return $this;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ManipulatorInterface::save()
*/
final public function save($path = null, array $options = array())
{
$path = $path === null ? (isset($this->metadata['filepath']) ? $this->metadata['filepath'] : $path) : $path;
if ($path === null) {
throw new RuntimeException('You can omit save path only if image has been open from a file');
}
if (isset($options['format'])) {
$format = $options['format'];
} elseif ('' !== $extension = pathinfo($path, \PATHINFO_EXTENSION)) {
$format = $extension;
} else {
$originalPath = isset($this->metadata['filepath']) ? $this->metadata['filepath'] : null;
$format = pathinfo($originalPath, \PATHINFO_EXTENSION);
}
$formatInfo = static::getDriverInfo()->getSupportedFormats()->find($format);
if ($formatInfo === null) {
throw new InvalidArgumentException(sprintf(
'Saving image in "%s" format is not supported, please use one of the following extensions: "%s"',
$format,
implode('", "', static::getDriverInfo()->getSupportedFormats()->getAllIDs())
));
}
$this->saveOrOutput($formatInfo, $options, $path);
return $this;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ManipulatorInterface::show()
*/
public function show($format, array $options = array())
{
$formatInfo = static::getDriverInfo()->getSupportedFormats()->find($format);
if ($formatInfo === null) {
throw new InvalidArgumentException(sprintf(
'Displaying an image in "%s" format is not supported, please use one of the following formats: "%s"',
$format,
implode('", "', static::getDriverInfo()->getSupportedFormats()->getAllIDs())
));
}
header('Content-type: ' . $formatInfo->getMimeType());
$this->saveOrOutput($formatInfo, $options);
return $this;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::get()
*/
public function get($format, array $options = array())
{
$formatInfo = static::getDriverInfo()->getSupportedFormats()->find($format);
if ($formatInfo === null) {
throw new InvalidArgumentException(sprintf(
'Creating an image in "%s" format is not supported, please use one of the following formats: "%s"',
$format,
implode('", "', static::getDriverInfo()->getSupportedFormats()->getAllIDs())
));
}
ob_start();
$this->saveOrOutput($formatInfo, $options);
return ob_get_clean();
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::__toString()
*/
public function __toString()
{
return $this->get(Format::ID_PNG);
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ManipulatorInterface::flipHorizontally()
*/
final public function flipHorizontally()
{
if (function_exists('imageflip')) {
imageflip($this->resource, IMG_FLIP_HORIZONTAL);
} else {
$size = $this->getSize();
$width = $size->getWidth();
$height = $size->getHeight();
$dest = $this->createImage($size, 'flip');
for ($i = 0; $i < $width; $i++) {
if (imagecopy($dest, $this->resource, $i, 0, ($width - 1) - $i, 0, 1, $height) === false) {
imagedestroy($dest);
throw new RuntimeException('Horizontal flip operation failed');
}
}
imagedestroy($this->resource);
$this->resource = $dest;
}
return $this;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ManipulatorInterface::flipVertically()
*/
final public function flipVertically()
{
if (function_exists('imageflip')) {
imageflip($this->resource, IMG_FLIP_VERTICAL);
} else {
$size = $this->getSize();
$width = $size->getWidth();
$height = $size->getHeight();
$dest = $this->createImage($size, 'flip');
for ($i = 0; $i < $height; $i++) {
if (imagecopy($dest, $this->resource, 0, $i, 0, ($height - 1) - $i, $width, 1) === false) {
imagedestroy($dest);
throw new RuntimeException('Vertical flip operation failed');
}
}
imagedestroy($this->resource);
$this->resource = $dest;
}
return $this;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ManipulatorInterface::strip()
*/
final public function strip()
{
// GD strips profiles and comment, so there's nothing to do here
return $this;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::draw()
*/
public function draw()
{
return $this->getClassFactory()->createDrawer(ClassFactoryInterface::HANDLE_GD, $this->resource);
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::effects()
*/
public function effects()
{
return $this->getClassFactory()->createEffects(ClassFactoryInterface::HANDLE_GD, $this->resource);
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::getSize()
*/
public function getSize()
{
return $this->getClassFactory()->createBox(imagesx($this->resource), imagesy($this->resource));
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ManipulatorInterface::applyMask()
*/
public function applyMask(ImageInterface $mask)
{
if (!$mask instanceof self) {
throw new InvalidArgumentException('Cannot mask non-gd images');
}
$size = $this->getSize();
$maskSize = $mask->getSize();
if ($size != $maskSize) {
throw new InvalidArgumentException(sprintf('The given mask doesn\'t match current image\'s size, Current mask\'s dimensions are %s, while image\'s dimensions are %s', $maskSize, $size));
}
for ($x = 0, $width = $size->getWidth(); $x < $width; $x++) {
for ($y = 0, $height = $size->getHeight(); $y < $height; $y++) {
$position = new Point($x, $y);
$color = $this->getColorAt($position);
$maskColor = $mask->getColorAt($position);
$delta = (int) round($color->getAlpha() * $maskColor->getRed() / 255) * -1;
if (imagesetpixel($this->resource, $x, $y, $this->getColor($color->dissolve($delta))) === false) {
throw new RuntimeException('Apply mask operation failed');
}
}
}
return $this;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ManipulatorInterface::fill()
*/
public function fill(FillInterface $fill)
{
$size = $this->getSize();
for ($x = 0, $width = $size->getWidth(); $x < $width; $x++) {
for ($y = 0, $height = $size->getHeight(); $y < $height; $y++) {
if (imagesetpixel($this->resource, $x, $y, $this->getColor($fill->getColor(new Point($x, $y)))) === false) {
throw new RuntimeException('Fill operation failed');
}
}
}
return $this;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::mask()
*/
public function mask()
{
$mask = $this->copy();
if (imagefilter($mask->resource, IMG_FILTER_GRAYSCALE) === false) {
throw new RuntimeException('Mask operation failed');
}
return $mask;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::histogram()
*/
public function histogram()
{
$size = $this->getSize();
$colors = array();
for ($x = 0, $width = $size->getWidth(); $x < $width; $x++) {
for ($y = 0, $height = $size->getHeight(); $y < $height; $y++) {
$colors[] = $this->getColorAt(new Point($x, $y));
}
}
return array_values(array_unique($colors));
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::getColorAt()
*/
public function getColorAt(PointInterface $point)
{
if (!$point->in($this->getSize())) {
throw new RuntimeException(sprintf('Error getting color at point [%s,%s]. The point must be inside the image of size [%s,%s]', $point->getX(), $point->getY(), $this->getSize()->getWidth(), $this->getSize()->getHeight()));
}
$index = imagecolorat($this->resource, $point->getX(), $point->getY());
$info = imagecolorsforindex($this->resource, $index);
return $this->palette->color(array($info['red'], $info['green'], $info['blue']), max(min(100 - (int) round($info['alpha'] / 127 * 100), 100), 0));
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::layers()
*/
public function layers()
{
if ($this->layers === null) {
$this->layers = $this->getClassFactory()->createLayers(ClassFactoryInterface::HANDLE_GD, $this);
}
return $this->layers;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::interlace()
*/
public function interlace($scheme)
{
static $supportedInterlaceSchemes = array(
ImageInterface::INTERLACE_NONE => 0,
ImageInterface::INTERLACE_LINE => 1,
ImageInterface::INTERLACE_PLANE => 1,
ImageInterface::INTERLACE_PARTITION => 1,
);
if (!array_key_exists($scheme, $supportedInterlaceSchemes)) {
throw new InvalidArgumentException('Unsupported interlace type');
}
imageinterlace($this->resource, $supportedInterlaceSchemes[$scheme]);
return $this;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::palette()
*/
public function palette()
{
return $this->palette;
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::profile()
*/
public function profile(ProfileInterface $profile)
{
static::getDriverInfo()->requireFeature(DriverInfo::FEATURE_COLORPROFILES);
}
/**
* {@inheritdoc}
*
* @see \Imagine\Image\ImageInterface::usePalette()
*/
public function usePalette(PaletteInterface $palette)
{
if ($this->palette->name() === $palette->name()) {
return $this;
}
static::getDriverInfo()->requirePaletteSupport($palette);
$this->palette = $palette;
return $this;
}
/**
* Performs save or show operation using one of GD's image... functions.
*
* @param \Imagine\Image\Format $format
* @param array $options
* @param string $filename
*
* @throws \Imagine\Exception\InvalidArgumentException
* @throws \Imagine\Exception\RuntimeException
*/
private function saveOrOutput(Format $format, array $options, $filename = null)
{
switch ($format->getID()) {
default:
$saveFunction = 'image' . $format->getID();
break;
}
$args = array(&$this->resource, $filename);
switch ($format->getID()) {
case Format::ID_AVIF:
// ranges from 0 (worst quality, smaller file) to 100 (best quality, larger file). If -1 is provided, the default value is used
$quality = -1;
// ranges from 0 (slow, smaller file) to 10 (fast, larger file). If -1 is provided, the default value is used
$speed = -1;
if (!empty($options['avif_lossless'])) {
$quality = 100;
} else {
if (!isset($options['avif_quality'])) {
if (isset($options['quality'])) {
$options['avif_quality'] = $options['quality'];
}
}
if (isset($options['avif_quality'])) {
$quality = max(0, min(100, $options['avif_quality']));
}
}
$args[] = $quality;
$args[] = $speed;
break;
case Format::ID_BMP:
if (isset($options['compressed'])) {
$args[] = (bool) $options['compressed'];
}
break;
case Format::ID_JPEG:
if (!isset($options['jpeg_quality'])) {
if (isset($options['quality'])) {
$options['jpeg_quality'] = $options['quality'];
}
}
if (isset($options['jpeg_quality'])) {
$args[] = $options['jpeg_quality'];
}
break;
case Format::ID_PNG:
if (!isset($options['png_compression_level'])) {
if (isset($options['quality'])) {
$options['png_compression_level'] = round((100 - $options['quality']) * 9 / 100);
}
}
if (isset($options['png_compression_level'])) {
if ($options['png_compression_level'] < 0 || $options['png_compression_level'] > 9) {
throw new InvalidArgumentException('png_compression_level option should be an integer from 0 to 9');
}
$args[] = $options['png_compression_level'];
} else {
$args[] = -1; // use default level
}
if (!isset($options['png_compression_filter'])) {
if (isset($options['filters'])) {
$options['png_compression_filter'] = $options['filters'];
}
}
if (isset($options['png_compression_filter'])) {
if (~PNG_ALL_FILTERS & $options['png_compression_filter']) {
throw new InvalidArgumentException('png_compression_filter option should be a combination of the PNG_FILTER_XXX constants');
}
$args[] = $options['png_compression_filter'];
}
break;
case Format::ID_WBMP:
case Format::ID_XBM:
if (isset($options['foreground'])) {
$args[] = $options['foreground'];
}
break;
case Format::ID_WEBP:
if (!isset($options['webp_quality'])) {
if (isset($options['quality'])) {
$options['webp_quality'] = $options['quality'];
}
}
if (isset($options['webp_quality'])) {
if ($options['webp_quality'] < 0 || $options['webp_quality'] > 100) {
throw new InvalidArgumentException('webp_quality option should be an integer from 0 to 100');
}
$args[] = $options['webp_quality'];
}
break;
}
ErrorHandling::throwingRuntimeException(E_WARNING | E_NOTICE, function () use ($saveFunction, $args) {
if (call_user_func_array($saveFunction, $args) === false) {
throw new RuntimeException('Save operation failed');
}
});
}
/**
* Generates a GD image.
*
* @param \Imagine\Image\BoxInterface $size
* @param string $operation the operation initiating the creation
*
* @throws \Imagine\Exception\RuntimeException
*
* @return resource|\GdImage
*/
private function createImage(BoxInterface $size, $operation)
{
$resource = imagecreatetruecolor($size->getWidth(), $size->getHeight());
if ($resource === false) {
throw new RuntimeException('Image ' . $operation . ' failed');
}
if (imagealphablending($resource, false) === false || imagesavealpha($resource, true) === false) {
throw new RuntimeException('Image ' . $operation . ' failed');
}
if (function_exists('imageantialias')) {
imageantialias($resource, true);
}
$transparent = imagecolorallocatealpha($resource, 255, 255, 255, 127);
imagefill($resource, 0, 0, $transparent);
imagecolortransparent($resource, $transparent);
return $resource;
}
/**
* Generates a GD color from Color instance.
*
* @param \Imagine\Image\Palette\Color\ColorInterface $color
*
* @throws \Imagine\Exception\RuntimeException
* @throws \Imagine\Exception\InvalidArgumentException
*
* @return int A color identifier
*/
private function getColor(ColorInterface $color)
{
if (!$color instanceof RGBColor) {
throw new InvalidArgumentException('GD driver only supports RGB colors');
}
$index = imagecolorallocatealpha($this->resource, $color->getRed(), $color->getGreen(), $color->getBlue(), round(127 * (100 - $color->getAlpha()) / 100));
if ($index === false) {
throw new RuntimeException(sprintf('Unable to allocate color "RGB(%s, %s, %s)" with transparency of %d percent', $color->getRed(), $color->getGreen(), $color->getBlue(), $color->getAlpha()));
}
return $index;
}
}