Passed
Branch master (d4e5f4)
by compolom
03:14 queued 24s
created

Imagick::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Compolomus\Compomage;
4
5
use Compolomus\Compomage\Interfaces\ImageInterface;
6
7
class Imagick extends AbstractImage implements ImageInterface
8
{
9
    /**
10
     * Imagick constructor.
11
     * @param string $image
12
     * @throws \Exception
13
     */
14
    public function __construct(string $image)
15
    {
16
        $this->init($image);
17
    }
18
19
    /**
20
     * @param string $source
21
     * @return ImageInterface
22
     * @throws \Exception
23
     */
24
    protected function tmp(string $source): ImageInterface
25
    {
26
        $image = new \Imagick;
27
        if ($image->readImageBlob($source)) {
28
            if ($image->getImageAlphaChannel() !== \Imagick::ALPHACHANNEL_ACTIVATE) {
29
                $image->setImageAlphaChannel(\Imagick::ALPHACHANNEL_SET);  // 8
30
                #$image->setImageAlphaChannel(\Imagick::ALPHACHANNEL_OPAQUE); // 6
31
            }
32
        }
33
        $background = $this->newImage($image->getImageWidth(), $image->getImageHeight());
34
        $image->compositeImage($background, \imagick::COMPOSITE_OVER, 0, 0); //Imagick::COMPOSITE_DISSOLVE
35
        $this->setImage($image);
36
        $this->getImage()->setFormat('png'); // save transparent
37
        $this->setSizes();
38
39
        return $this;
40
    }
41
42
    protected function setSizes(): void
43
    {
44
        $args = $this->getImage()->getImageGeometry();
45
        $this->setWidth($args['width']);
46
        $this->setHeight($args['height']);
47
    }
48
49
    private function newImage(int $width, int $height)
50
    {
51
        $background = new \Imagick;
52
        $background->newImage($width, $height, new \ImagickPixel('transparent'));
53
        $background->setImageBackgroundColor(new \ImagickPixel('transparent'));
54
55
        return $background;
56
    }
57
58
    /**
59
     * @param int $width
60
     * @param int $height
61
     * @return ImageInterface
62
     * @throws \ImagickException
63
     */
64
    public function resize(int $width, int $height): ImageInterface
65
    {
66
        $this->getImage()->scaleImage($width, $height, false);
67
        $this->setSizes();
68
69
        return $this;
70
    }
71
72
    public function rotate(int $angle = 90): ImageInterface
73
    {
74
        $this->getImage()->rotateImage(new \ImagickPixel('transparent'), $angle);
75
        $this->setSizes();
76
77
        return $this;
78
    }
79
80
    public function watermark(): ImageInterface
81
    {
82
        return $this;
83
    }
84
85
    public function flip(): ImageInterface
86
    {
87
        $this->getImage()->flipImage();
88
89
        return $this;
90
    }
91
92
    public function flop(): ImageInterface
93
    {
94
        $this->getImage()->flopImage();
95
96
        return $this;
97
    }
98
99
    public function grayscale(): ImageInterface
100
    {
101
        $this->getImage()->modulateImage(100, 0, 100);
102
103
        return $this;
104
    }
105
106
    public function getFontsList(): array
107
    {
108
        return $this->getImage()->queryFonts();
109
    }
110
111
    /**
112
     * @param string $text
113
     * @param string $position
114
     * @param string $font
115
     * @return $this
116
     * @throws \Exception
117
     */
118
    public function copyright(string $text, string $font = 'Courier', string $position = 'SouthWest')
119
    {
120
        $positions = [
121
            'NORTHWEST' => \Imagick::GRAVITY_NORTHWEST,
122
            'NORTH' => \Imagick::GRAVITY_NORTH,
123
            'NORTHEAST' => \Imagick::GRAVITY_NORTHEAST,
124
            'WEST' => \Imagick::GRAVITY_WEST,
125
            'CENTER' => \Imagick::GRAVITY_CENTER,
126
            'SOUTHWEST' => \Imagick::GRAVITY_SOUTHWEST,
127
            'SOUTH' => \Imagick::GRAVITY_SOUTH,
128
            'SOUTHEAST' => \Imagick::GRAVITY_SOUTHEAST,
129
            'EAST' => \Imagick::GRAVITY_EAST
130
        ];
131
132
        if (!in_array($font, $this->getFontsList())) {
133
            throw new \Exception('Does not support font');
134
        }
135
        if (!array_key_exists(strtoupper($position), $positions)) {
136
            throw new \Exception('Wrong position');
137
        }
138
139
        $position = $positions[strtoupper($position)];
140
141
        $image = $this->prepareImage($text, $position, $font);
142
        $this->getImage()->compositeImage($image, \Imagick::COMPOSITE_DISSOLVE, 0, 0);
143
144
        return $this;
145
    }
146
147
148
    /**
149
     * @param string $text
150
     * @param int $position
151
     * @param string $font
152
     * @return \Imagick
153
     */
154
    private function prepareImage(string $text, int $position, string $font)
155
    {
156
        $image = new \Imagick();
157
        $mask = new \Imagick();
158
        $draw = new \ImagickDraw();
159
160
        $width = $this->getWidth();
161
        $height = $this->getHeight();
162
163
        $image->newImage($width, $height, new \ImagickPixel('grey30'));
164
        $mask->newImage($width, $height, new \ImagickPixel('black'));
165
166
        $draw->setFont($font);
167
        $draw->setFontSize(20);
168
        $draw->setFillColor('grey70');
0 ignored issues
show
Bug introduced by
'grey70' of type string is incompatible with the type ImagickPixel expected by parameter $fill_pixel of ImagickDraw::setFillColor(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
        $draw->setFillColor(/** @scrutinizer ignore-type */ 'grey70');
Loading history...
169
170
        $draw->setGravity($position);
171
172
        $image->annotateImage($draw, 10, 12, 0, $text);
173
        $draw->setFillColor('white');
174
        $mask->annotateImage($draw, 11, 13, 0, $text);
175
        $mask->annotateImage($draw, 10, 12, 0, $text);
176
        $draw->setFillColor('black');
177
        $mask->annotateImage($draw, 9, 11, 0, $text);
178
        $mask->setImageMatte(false);
179
180
        $image->compositeImage($mask, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
181
182
        return $image;
183
    }
184
185
    public function crop(int $width, int $height, int $startX, int $startY): ImageInterface
186
    {
187
        $this->getImage()->cropImage($width, $height, $startX, $startY);
188
        $this->setSizes();
189
190
        return $this;
191
    }
192
193
    public function save(string $filename): bool
194
    {
195
        return true;
196
    }
197
198
    public function __toString(): string
199
    {
200
        return $this->getImage()->getImageBlob();
201
    }
202
}
203