1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Magickly project. |
4
|
|
|
* |
5
|
|
|
* @author Daniel Schröder <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace GravityMedia\Magickly\Imagick; |
9
|
|
|
|
10
|
|
|
use GravityMedia\Magickly\Enum\ColorSpace; |
11
|
|
|
use GravityMedia\Magickly\Enum\Type; |
12
|
|
|
use GravityMedia\Magickly\Exception\RuntimeException; |
13
|
|
|
use GravityMedia\Magickly\Image\AbstractImage; |
14
|
|
|
use GravityMedia\Magickly\Image\ColorProfile; |
15
|
|
|
use GravityMedia\Magickly\Image\ImageInterface; |
16
|
|
|
use GuzzleHttp\Stream\Utils as StreamUtils; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The image class. |
20
|
|
|
* |
21
|
|
|
* @package GravityMedia\Magickly\Imagick |
22
|
|
|
*/ |
23
|
|
|
class Image extends AbstractImage |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \Imagick |
27
|
|
|
*/ |
28
|
|
|
private $imagick; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected static $typeMapping = [ |
34
|
|
|
Type::TYPE_BILEVEL => \Imagick::IMGTYPE_BILEVEL, |
35
|
|
|
Type::TYPE_GRAYSCALE => \Imagick::IMGTYPE_GRAYSCALE, |
36
|
|
|
Type::TYPE_PALETTE => \Imagick::IMGTYPE_PALETTE, |
37
|
|
|
Type::TYPE_TRUECOLOR => \Imagick::IMGTYPE_TRUECOLOR, |
38
|
|
|
Type::TYPE_COLORSEPARATION => \Imagick::IMGTYPE_COLORSEPARATION, |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected static $colorSpaceMapping = [ |
45
|
|
|
ColorSpace::COLOR_SPACE_RGB => \Imagick::COLORSPACE_RGB, |
46
|
|
|
ColorSpace::COLOR_SPACE_CMYK => \Imagick::COLORSPACE_CMYK, |
47
|
|
|
ColorSpace::COLOR_SPACE_GRAYSCALE => \Imagick::COLORSPACE_GRAY, |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create image object. |
52
|
|
|
* |
53
|
|
|
* @param \Imagick $imagick |
54
|
|
|
*/ |
55
|
32 |
|
public function __construct(\Imagick $imagick) |
56
|
|
|
{ |
57
|
32 |
|
$this->imagick = $imagick; |
58
|
32 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Clone image object. |
62
|
|
|
*/ |
63
|
2 |
|
public function __clone() |
64
|
|
|
{ |
65
|
2 |
|
$this->imagick = clone $this->imagick; |
66
|
2 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
30 |
View Code Duplication |
public function getFormat() |
|
|
|
|
72
|
|
|
{ |
73
|
30 |
|
$format = $this->imagick->getImageFormat(); |
74
|
30 |
|
if (!in_array($format, static::$supportedFormats)) { |
75
|
|
|
throw new RuntimeException('Unsupported image format'); |
76
|
|
|
} |
77
|
|
|
|
78
|
30 |
|
return $format; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function withFormat($format) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
if (!in_array($format, static::$supportedFormats)) { |
87
|
|
|
throw new RuntimeException('Unsupported image format'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$image = clone $this; |
91
|
|
|
$image->imagick->setImageFormat($format); |
92
|
|
|
|
93
|
|
|
return $image; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
30 |
|
public function getType() |
100
|
|
|
{ |
101
|
30 |
|
switch ($this->imagick->getImageType()) { |
102
|
30 |
|
case \Imagick::IMGTYPE_BILEVEL: |
103
|
|
|
return Type::TYPE_BILEVEL; |
104
|
30 |
|
case \Imagick::IMGTYPE_GRAYSCALE: |
105
|
28 |
|
case \Imagick::IMGTYPE_GRAYSCALEMATTE: |
106
|
6 |
|
return Type::TYPE_GRAYSCALE; |
107
|
24 |
|
case \Imagick::IMGTYPE_PALETTE: |
108
|
22 |
|
case \Imagick::IMGTYPE_PALETTEMATTE: |
109
|
6 |
|
if ('JPEG' === $this->getFormat() && ColorSpace::COLOR_SPACE_GRAYSCALE === $this->getColorSpace()) { |
110
|
4 |
|
return Type::TYPE_GRAYSCALE; |
111
|
|
|
} |
112
|
2 |
|
return Type::TYPE_PALETTE; |
113
|
18 |
|
case \Imagick::IMGTYPE_TRUECOLOR: |
114
|
14 |
|
case \Imagick::IMGTYPE_TRUECOLORMATTE: |
115
|
10 |
|
return Type::TYPE_TRUECOLOR; |
116
|
8 |
|
case \Imagick::IMGTYPE_COLORSEPARATION: |
117
|
4 |
|
case \Imagick::IMGTYPE_COLORSEPARATIONMATTE: |
118
|
8 |
|
return Type::TYPE_COLORSEPARATION; |
119
|
|
|
default: |
120
|
|
|
throw new RuntimeException('Unsupported image type'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function withType($type) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
if (!isset(static::$typeMapping[$type])) { |
130
|
|
|
throw new RuntimeException('Unsupported image type'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$image = clone $this; |
134
|
|
|
$image->imagick->setImageType(static::$typeMapping[$type]); |
135
|
|
|
|
136
|
|
|
return $image; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
32 |
|
public function getColorSpace() |
143
|
|
|
{ |
144
|
32 |
|
switch ($this->imagick->getImageColorspace()) { |
145
|
32 |
|
case \Imagick::COLORSPACE_RGB: |
146
|
32 |
|
case \Imagick::COLORSPACE_SRGB: |
147
|
14 |
|
return ColorSpace::COLOR_SPACE_RGB; |
148
|
20 |
|
case \Imagick::COLORSPACE_CMYK: |
149
|
10 |
|
return ColorSpace::COLOR_SPACE_CMYK; |
150
|
10 |
|
case \Imagick::COLORSPACE_GRAY: |
151
|
10 |
|
return ColorSpace::COLOR_SPACE_GRAYSCALE; |
152
|
|
|
default: |
153
|
|
|
throw new RuntimeException('Only RGB, grayscale and CMYK color space are currently supported'); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
2 |
View Code Duplication |
public function withColorSpace($colorSpace) |
|
|
|
|
161
|
|
|
{ |
162
|
2 |
|
if (!isset(static::$colorSpaceMapping[$colorSpace])) { |
163
|
|
|
throw new RuntimeException('Only RGB, grayscale and CMYK color space are currently supported'); |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
$image = clone $this; |
167
|
2 |
|
$image->imagick->setImageColorspace(static::$colorSpaceMapping[$colorSpace]); |
168
|
|
|
|
169
|
2 |
|
return $image; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function getColorProfile() |
176
|
|
|
{ |
177
|
|
|
if (!in_array('icc', $this->imagick->getImageProfiles('*', false))) { |
178
|
|
|
return null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$data = $this->imagick->getImageProfile('icc'); |
182
|
|
|
|
183
|
|
|
return new ColorProfile(StreamUtils::create($data)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
*/ |
189
|
|
|
public function withColorProfile(ColorProfile $colorProfile) |
190
|
|
|
{ |
191
|
|
|
$image = clone $this; |
192
|
|
|
$image->imagick->setImageProfile('icc', $colorProfile->getData()); |
193
|
|
|
|
194
|
|
|
return $image; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.