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\Gmagick; |
9
|
|
|
|
10
|
|
|
use GravityMedia\Magickly\Enum\ColorSpace; |
11
|
|
|
use GravityMedia\Magickly\Exception\RuntimeException; |
12
|
|
|
use GravityMedia\Magickly\Image\AbstractImage; |
13
|
|
|
use GravityMedia\Magickly\Image\ColorProfile; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The image class. |
17
|
|
|
* |
18
|
|
|
* @package GravityMedia\Magickly\Gmagick |
19
|
|
|
*/ |
20
|
|
|
class Image extends AbstractImage |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \Gmagick |
24
|
|
|
*/ |
25
|
|
|
protected $gmagick; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected static $colorspaceMapping = [ |
31
|
|
|
ColorSpace::COLOR_SPACE_RGB => \Gmagick::COLORSPACE_RGB, |
32
|
|
|
ColorSpace::COLOR_SPACE_CMYK => \Gmagick::COLORSPACE_CMYK, |
33
|
|
|
ColorSpace::COLOR_SPACE_GRAYSCALE => \Gmagick::COLORSPACE_GRAY |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create image object. |
38
|
|
|
* |
39
|
|
|
* @param \Gmagick $gmagick |
40
|
|
|
*/ |
41
|
|
|
public function __construct(\Gmagick $gmagick) |
42
|
|
|
{ |
43
|
|
|
$this->gmagick = $gmagick; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function getColorSpace() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
switch ($this->gmagick->getimagecolorspace()) { |
52
|
|
|
case \Gmagick::COLORSPACE_RGB: |
53
|
|
|
case \Gmagick::COLORSPACE_SRGB: |
54
|
|
|
return ColorSpace::COLOR_SPACE_RGB; |
55
|
|
|
case \Gmagick::COLORSPACE_CMYK: |
56
|
|
|
return ColorSpace::COLOR_SPACE_CMYK; |
57
|
|
|
case \Gmagick::COLORSPACE_GRAY: |
58
|
|
|
return ColorSpace::COLOR_SPACE_GRAYSCALE; |
59
|
|
|
default: |
60
|
|
|
throw new RuntimeException('Only RGB, grayscale and CMYK colorspace are currently supported'); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function setColorSpace($colorspace) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if (!isset(static::$colorspaceMapping[$colorspace])) { |
70
|
|
|
throw new RuntimeException('Only RGB, grayscale and CMYK colorspace are currently supported'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->gmagick->setimagecolorspace(static::$colorspaceMapping[$colorspace]); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function getColorProfile() |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
|
|
$data = $this->gmagick->getimageprofile('ICM'); |
85
|
|
|
} catch (\GmagickException $exception) { |
|
|
|
|
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return new ColorProfile($data); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function setColorProfile(ColorProfile $colorProfile) |
96
|
|
|
{ |
97
|
|
|
$this->gmagick->setimageprofile('ICM', $colorProfile->getData()); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.