Image::withType()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 11
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 6
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\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\Gmagick
21
 */
22
class Image extends AbstractImage
23
{
24
    /**
25
     * @var \Gmagick
26
     */
27
    private $gmagick;
28
29
    /**
30
     * @var array
31
     */
32
    protected static $typeMapping = [
33
        Type::TYPE_BILEVEL => \Gmagick::IMGTYPE_BILEVEL,
34
        Type::TYPE_GRAYSCALE => \Gmagick::IMGTYPE_GRAYSCALE,
35
        Type::TYPE_PALETTE => \Gmagick::IMGTYPE_PALETTE,
36
        Type::TYPE_TRUECOLOR => \Gmagick::IMGTYPE_TRUECOLOR,
37
        Type::TYPE_COLORSEPARATION => \Gmagick::IMGTYPE_COLORSEPARATION,
38
    ];
39
40
    /**
41
     * @var array
42
     */
43
    protected static $colorSpaceMapping = [
44
        ColorSpace::COLOR_SPACE_RGB => \Gmagick::COLORSPACE_RGB,
45
        ColorSpace::COLOR_SPACE_CMYK => \Gmagick::COLORSPACE_CMYK,
46
        ColorSpace::COLOR_SPACE_GRAYSCALE => \Gmagick::COLORSPACE_GRAY,
47
    ];
48
49
    /**
50
     * Create image object.
51
     *
52
     * @param \Gmagick $gmagick
53
     */
54 32
    public function __construct(\Gmagick $gmagick)
55
    {
56 32
        $this->gmagick = $gmagick;
57 32
    }
58
59
    /**
60
     * Clone image object.
61
     */
62 2
    public function __clone()
63
    {
64 2
        $this->gmagick = clone $this->gmagick;
65 2
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getStream()
71
    {
72
        return StreamUtils::create($this->gmagick->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->gmagick->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->gmagick->setimageformat($format);
99
100
        return $image;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 32
    public function getType()
107
    {
108 32
        switch ($this->gmagick->getimagetype()) {
109 32
            case \Gmagick::IMGTYPE_BILEVEL:
110
                return Type::TYPE_BILEVEL;
111 32
            case \Gmagick::IMGTYPE_GRAYSCALE:
112 28
            case \Gmagick::IMGTYPE_GRAYSCALEMATTE:
113 10
                return Type::TYPE_GRAYSCALE;
114 22
            case \Gmagick::IMGTYPE_PALETTE:
115 22
            case \Gmagick::IMGTYPE_PALETTEMATTE:
116 2
                return Type::TYPE_PALETTE;
117 20
            case \Gmagick::IMGTYPE_TRUECOLOR:
118 15
            case \Gmagick::IMGTYPE_TRUECOLORMATTE:
119 12
                return Type::TYPE_TRUECOLOR;
120 8
            case \Gmagick::IMGTYPE_COLORSEPARATION:
121 4
            case \Gmagick::IMGTYPE_COLORSEPARATIONMATTE:
122 8
                return Type::TYPE_COLORSEPARATION;
123
            default:
124
                throw new RuntimeException('Unsupported image type');
125
        }
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 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...
132
    {
133
        if (!isset(static::$typeMapping[$type])) {
134
            throw new RuntimeException('Unsupported image type');
135
        }
136
137
        $image = clone $this;
138
        $image->gmagick->setimagetype(static::$typeMapping[$type]);
139
140
        return $image;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 32
    public function getColorSpace()
147
    {
148 32
        switch ($this->gmagick->getimagecolorspace()) {
149 32
            case \Gmagick::COLORSPACE_RGB:
150 21
            case \Gmagick::COLORSPACE_SRGB:
151 24
                if (Type::TYPE_GRAYSCALE === $this->getType()) {
152 10
                    return ColorSpace::COLOR_SPACE_GRAYSCALE;
153
                }
154 14
                return ColorSpace::COLOR_SPACE_RGB;
155 10
            case \Gmagick::COLORSPACE_CMYK:
156 10
                return ColorSpace::COLOR_SPACE_CMYK;
157
            case \Gmagick::COLORSPACE_GRAY:
158
                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->gmagick->setimagecolorspace(static::$colorSpaceMapping[$colorSpace]);
175
176 2
        return $image;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function getColorProfile()
183
    {
184
        try {
185
            $data = $this->gmagick->getimageprofile('ICM');
186
        } catch (\GmagickException $exception) {
0 ignored issues
show
Bug introduced by
The class GmagickException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
187
            return null;
188
        }
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->gmagick->setimageprofile('ICM', $colorProfile->getData());
200
201
        return $image;
202
    }
203
}
204