Image::withColorProfile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
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 GuzzleHttp\Stream\Utils as StreamUtils;
16
17
/**
18
 * The image class.
19
 *
20
 * @package GravityMedia\Magickly\Imagick
21
 */
22
class Image extends AbstractImage
23
{
24
    /**
25
     * @var \Imagick
26
     */
27
    private $imagick;
28
29
    /**
30
     * @var array
31
     */
32
    protected static $typeMapping = [
33
        Type::TYPE_BILEVEL => \Imagick::IMGTYPE_BILEVEL,
34
        Type::TYPE_GRAYSCALE => \Imagick::IMGTYPE_GRAYSCALE,
35
        Type::TYPE_PALETTE => \Imagick::IMGTYPE_PALETTE,
36
        Type::TYPE_TRUECOLOR => \Imagick::IMGTYPE_TRUECOLOR,
37
        Type::TYPE_COLORSEPARATION => \Imagick::IMGTYPE_COLORSEPARATION,
38
    ];
39
40
    /**
41
     * @var array
42
     */
43
    protected static $colorSpaceMapping = [
44
        ColorSpace::COLOR_SPACE_RGB => \Imagick::COLORSPACE_RGB,
45
        ColorSpace::COLOR_SPACE_CMYK => \Imagick::COLORSPACE_CMYK,
46
        ColorSpace::COLOR_SPACE_GRAYSCALE => \Imagick::COLORSPACE_GRAY,
47
    ];
48
49
    /**
50
     * Create image object.
51
     *
52
     * @param \Imagick $imagick
53
     */
54 32
    public function __construct(\Imagick $imagick)
55
    {
56 32
        $this->imagick = $imagick;
57 32
    }
58
59
    /**
60
     * Clone image object.
61
     */
62 2
    public function __clone()
63
    {
64 2
        $this->imagick = clone $this->imagick;
65 2
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getStream()
71
    {
72
        return StreamUtils::create($this->imagick->getImagesBlob());
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 30 View Code Duplication
    public function getFormat()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
79
    {
80 30
        $format = $this->imagick->getImageFormat();
81 30
        if (!in_array($format, static::$supportedFormats)) {
82
            throw new RuntimeException('Unsupported image format');
83
        }
84
85 30
        return $format;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 View Code Duplication
    public function withFormat($format)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
92
    {
93
        if (!in_array($format, static::$supportedFormats)) {
94
            throw new RuntimeException('Unsupported image format');
95
        }
96
97
        $image = clone $this;
98
        $image->imagick->setImageFormat($format);
99
100
        return $image;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 30
    public function getType()
107
    {
108 30
        switch ($this->imagick->getImageType()) {
109 30
            case \Imagick::IMGTYPE_BILEVEL:
110
                return Type::TYPE_BILEVEL;
111 30
            case \Imagick::IMGTYPE_GRAYSCALE:
112 28
            case \Imagick::IMGTYPE_GRAYSCALEMATTE:
113 6
                return Type::TYPE_GRAYSCALE;
114 24
            case \Imagick::IMGTYPE_PALETTE:
115 22
            case \Imagick::IMGTYPE_PALETTEMATTE:
116 6
                if ('JPEG' === $this->getFormat() && ColorSpace::COLOR_SPACE_GRAYSCALE === $this->getColorSpace()) {
117 4
                    return Type::TYPE_GRAYSCALE;
118
                }
119 2
                return Type::TYPE_PALETTE;
120 18
            case \Imagick::IMGTYPE_TRUECOLOR:
121 14
            case \Imagick::IMGTYPE_TRUECOLORMATTE:
122 10
                return Type::TYPE_TRUECOLOR;
123 8
            case \Imagick::IMGTYPE_COLORSEPARATION:
124 4
            case \Imagick::IMGTYPE_COLORSEPARATIONMATTE:
125 8
                return Type::TYPE_COLORSEPARATION;
126
            default:
127
                throw new RuntimeException('Unsupported image type');
128
        }
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 View Code Duplication
    public function withType($type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
135
    {
136
        if (!isset(static::$typeMapping[$type])) {
137
            throw new RuntimeException('Unsupported image type');
138
        }
139
140
        $image = clone $this;
141
        $image->imagick->setImageType(static::$typeMapping[$type]);
142
143
        return $image;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 32
    public function getColorSpace()
150
    {
151 32
        switch ($this->imagick->getImageColorspace()) {
152 32
            case \Imagick::COLORSPACE_RGB:
153 32
            case \Imagick::COLORSPACE_SRGB:
154 14
                return ColorSpace::COLOR_SPACE_RGB;
155 20
            case \Imagick::COLORSPACE_CMYK:
156 10
                return ColorSpace::COLOR_SPACE_CMYK;
157 10
            case \Imagick::COLORSPACE_GRAY:
158 10
                return ColorSpace::COLOR_SPACE_GRAYSCALE;
159
            default:
160
                throw new RuntimeException('Only RGB, grayscale and CMYK color space are currently supported');
161
        }
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 2 View Code Duplication
    public function withColorSpace($colorSpace)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
168
    {
169 2
        if (!isset(static::$colorSpaceMapping[$colorSpace])) {
170
            throw new RuntimeException('Only RGB, grayscale and CMYK color space are currently supported');
171
        }
172
173 2
        $image = clone $this;
174 2
        $image->imagick->setImageColorspace(static::$colorSpaceMapping[$colorSpace]);
175
176 2
        return $image;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function getColorProfile()
183
    {
184
        if (!in_array('icc', $this->imagick->getImageProfiles('*', false))) {
185
            return null;
186
        }
187
188
        $data = $this->imagick->getImageProfile('icc');
189
190
        return new ColorProfile(StreamUtils::create($data));
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function withColorProfile(ColorProfile $colorProfile)
197
    {
198
        $image = clone $this;
199
        $image->imagick->setImageProfile('icc', $colorProfile->getData());
200
201
        return $image;
202
    }
203
}
204