Completed
Push — master ( b80fb6...2b95a6 )
by
unknown
11:10
created

Image::withFormat()   A

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
dl 11
loc 11
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
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 GravityMedia\Magickly\Image\ImageInterface;
16
use GuzzleHttp\Stream\Utils as StreamUtils;
17
18
/**
19
 * The image class.
20
 *
21
 * @package GravityMedia\Magickly\Gmagick
22
 */
23
class Image extends AbstractImage
24
{
25
    /**
26
     * @var \Gmagick
27
     */
28
    private $gmagick;
29
30
    /**
31
     * @var array
32
     */
33
    protected static $typeMapping = [
34
        Type::TYPE_BILEVEL => \Gmagick::IMGTYPE_BILEVEL,
35
        Type::TYPE_GRAYSCALE => \Gmagick::IMGTYPE_GRAYSCALE,
36
        Type::TYPE_PALETTE => \Gmagick::IMGTYPE_PALETTE,
37
        Type::TYPE_TRUECOLOR => \Gmagick::IMGTYPE_TRUECOLOR,
38
        Type::TYPE_COLORSEPARATION => \Gmagick::IMGTYPE_COLORSEPARATION,
39
    ];
40
41
    /**
42
     * @var array
43
     */
44
    protected static $colorSpaceMapping = [
45
        ColorSpace::COLOR_SPACE_RGB => \Gmagick::COLORSPACE_RGB,
46
        ColorSpace::COLOR_SPACE_CMYK => \Gmagick::COLORSPACE_CMYK,
47
        ColorSpace::COLOR_SPACE_GRAYSCALE => \Gmagick::COLORSPACE_GRAY,
48
    ];
49
50
    /**
51
     * Create image object.
52
     *
53
     * @param \Gmagick $gmagick
54
     */
55 32
    public function __construct(\Gmagick $gmagick)
56
    {
57 32
        $this->gmagick = $gmagick;
58 32
    }
59
60
    /**
61
     * Clone image object.
62
     */
63 2
    public function __clone()
64
    {
65 2
        $this->gmagick = clone $this->gmagick;
66 2
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 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...
72
    {
73 30
        $format = $this->gmagick->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)
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...
85
    {
86
        if (!in_array($format, static::$supportedFormats)) {
87
            throw new RuntimeException('Unsupported image format');
88
        }
89
90
        $image = clone $this;
91
        $image->gmagick->setimageformat($format);
92
93
        return $image;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 32
    public function getType()
100
    {
101 32
        switch ($this->gmagick->getimagetype()) {
102 32
            case \Gmagick::IMGTYPE_BILEVEL:
103
                return Type::TYPE_BILEVEL;
104 32
            case \Gmagick::IMGTYPE_GRAYSCALE:
105 28
            case \Gmagick::IMGTYPE_GRAYSCALEMATTE:
106 10
                return Type::TYPE_GRAYSCALE;
107 22
            case \Gmagick::IMGTYPE_PALETTE:
108 22
            case \Gmagick::IMGTYPE_PALETTEMATTE:
109 2
                return Type::TYPE_PALETTE;
110 20
            case \Gmagick::IMGTYPE_TRUECOLOR:
111 15
            case \Gmagick::IMGTYPE_TRUECOLORMATTE:
112 12
                return Type::TYPE_TRUECOLOR;
113 8
            case \Gmagick::IMGTYPE_COLORSEPARATION:
114 4
            case \Gmagick::IMGTYPE_COLORSEPARATIONMATTE:
115 8
                return Type::TYPE_COLORSEPARATION;
116
            default:
117
                throw new RuntimeException('Unsupported image type');
118
        }
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 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...
125
    {
126
        if (!isset(static::$typeMapping[$type])) {
127
            throw new RuntimeException('Unsupported image type');
128
        }
129
130
        $image = clone $this;
131
        $image->gmagick->setimagetype(static::$typeMapping[$type]);
132
133
        return $image;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 32
    public function getColorSpace()
140
    {
141 32
        switch ($this->gmagick->getimagecolorspace()) {
142 32
            case \Gmagick::COLORSPACE_RGB:
143 21
            case \Gmagick::COLORSPACE_SRGB:
144 24
                if (Type::TYPE_GRAYSCALE === $this->getType()) {
145 10
                    return ColorSpace::COLOR_SPACE_GRAYSCALE;
146
                }
147 14
                return ColorSpace::COLOR_SPACE_RGB;
148 10
            case \Gmagick::COLORSPACE_CMYK:
149 10
                return ColorSpace::COLOR_SPACE_CMYK;
150
            case \Gmagick::COLORSPACE_GRAY:
151
                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)
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...
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->gmagick->setimagecolorspace(static::$colorSpaceMapping[$colorSpace]);
168
169 2
        return $image;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function getColorProfile()
176
    {
177
        try {
178
            $data = $this->gmagick->getimageprofile('ICM');
179
        } 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...
180
            return null;
181
        }
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->gmagick->setimageprofile('ICM', $colorProfile->getData());
193
194
        return $image;
195
    }
196
}
197