Completed
Push — master ( 1316ca...59eabb )
by Daniel
11:50
created

Image::setColorProfile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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\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\Imagick
19
 */
20
class Image extends AbstractImage
21
{
22
    /**
23
     * @var \Imagick
24
     */
25
    protected $imagick;
26
27
    /**
28
     * @var array
29
     */
30
    protected static $colorspaceMapping = [
31
        ColorSpace::COLOR_SPACE_RGB => \Imagick::COLORSPACE_RGB,
32
        ColorSpace::COLOR_SPACE_CMYK => \Imagick::COLORSPACE_CMYK,
33
        ColorSpace::COLOR_SPACE_GRAYSCALE => \Imagick::COLORSPACE_GRAY
34
    ];
35
36
    /**
37
     * Create image object.
38
     *
39
     * @param \Imagick $imagick
40
     */
41
    public function __construct(\Imagick $imagick)
42
    {
43
        $this->imagick = $imagick;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 View Code Duplication
    public function getColorSpace()
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...
50
    {
51
        switch ($this->imagick->getImageColorspace()) {
52
            case \Imagick::COLORSPACE_RGB:
53
            case \Imagick::COLORSPACE_SRGB:
54
                return ColorSpace::COLOR_SPACE_RGB;
55
            case \Imagick::COLORSPACE_CMYK:
56
                return ColorSpace::COLOR_SPACE_CMYK;
57
            case \Imagick::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)
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...
68
    {
69
        if (!isset(static::$colorspaceMapping[$colorspace])) {
70
            throw new RuntimeException('Only RGB, grayscale and CMYK colorspace are currently supported');
71
        }
72
73
        $this->imagick->setImageColorspace(static::$colorspaceMapping[$colorspace]);
74
75
        return $this;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getColorProfile()
82
    {
83
        if (!in_array('icc', $this->imagick->getImageProfiles('*', false))) {
84
            return null;
85
        }
86
87
        return new ColorProfile($this->imagick->getImageProfile('icc'));
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function setColorProfile(ColorProfile $colorProfile)
94
    {
95
        $this->imagick->setImageProfile('icc', $colorProfile->getData());
96
97
        return $this;
98
    }
99
}
100