Passed
Branch NF-watermark (3592d3)
by compolom
02:27 queued 31s
created

Imagick::setSizes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 4
nop 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
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
    /**
81
     * @param Image $watermark
82
     * @param int $x
83
     * @param int $y
84
     * @return ImageInterface
85
     */
86
    protected function prepareWatermark(Image $watermark, int $x, int $y): ImageInterface
87
    {
88
        $watermark->getImage()->evaluateImage(\Imagick::EVALUATE_MULTIPLY, 1, \Imagick::CHANNEL_ALPHA);
0 ignored issues
show
Documentation Bug introduced by
The method getImage does not exist on object<Compolomus\Compomage\Image>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
89
        $this->getImage()->compositeImage($watermark->getImage(), \Imagick::COMPOSITE_DISSOLVE, $x, $y);
0 ignored issues
show
Documentation Bug introduced by
The method getImage does not exist on object<Compolomus\Compomage\Image>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
90
91
        return $this;
92
    }
93
94
    public function flip(): ImageInterface
95
    {
96
        $this->getImage()->flipImage();
97
98
        return $this;
99
    }
100
101
    public function flop(): ImageInterface
102
    {
103
        $this->getImage()->flopImage();
104
105
        return $this;
106
    }
107
108
    public function grayscale(): ImageInterface
109
    {
110
        $this->getImage()->modulateImage(100, 0, 100);
111
112
        return $this;
113
    }
114
115
    public function getFontsList(): array
116
    {
117
        return $this->getImage()->queryFonts();
118
    }
119
120
    /**
121
     * @param string $text
122
     * @param string $position
123
     * @param string $font
124
     * @return $this
125
     * @throws \Exception
126
     */
127
    public function copyright(string $text, string $font = 'Courier', string $position = 'SouthWest')
128
    {
129
        $positions = [
130
            'NORTHWEST' => \Imagick::GRAVITY_NORTHWEST,
131
            'NORTH' => \Imagick::GRAVITY_NORTH,
132
            'NORTHEAST' => \Imagick::GRAVITY_NORTHEAST,
133
            'WEST' => \Imagick::GRAVITY_WEST,
134
            'CENTER' => \Imagick::GRAVITY_CENTER,
135
            'SOUTHWEST' => \Imagick::GRAVITY_SOUTHWEST,
136
            'SOUTH' => \Imagick::GRAVITY_SOUTH,
137
            'SOUTHEAST' => \Imagick::GRAVITY_SOUTHEAST,
138
            'EAST' => \Imagick::GRAVITY_EAST
139
        ];
140
        if (!in_array($font, $this->getFontsList())) {
141
            throw new \Exception('Does not support font');
142
        }
143
        if (!array_key_exists(strtoupper($position), $positions)) {
144
            throw new \Exception('Wrong position');
145
        }
146
        $this->getImage()->compositeImage($this->prepareImage($text, $positions[strtoupper($position)], $font), \Imagick::COMPOSITE_DISSOLVE, 0, 0);
147
148
        return $this;
149
    }
150
151
152
    /**
153
     * @param string $text
154
     * @param int $position
155
     * @param string $font
156
     * @return \Imagick
157
     */
158
    private function prepareImage(string $text, int $position, string $font)
159
    {
160
        $image = new \Imagick();
161
        $mask = new \Imagick();
162
        $draw = new \ImagickDraw();
163
        $image->newImage($this->getWidth(), $this->getHeight(), new \ImagickPixel('grey30'));
164
        $mask->newImage($this->getWidth(), $this->getHeight(), new \ImagickPixel('black'));
165
        $draw->setFont($font);
166
        $draw->setFontSize(20);
167
        $draw->setFillColor(new \ImagickPixel('grey70'));
168
        $draw->setGravity($position);
169
        $image->annotateImage($draw, 10, 12, 0, $text);
170
        $draw->setFillColor(new \ImagickPixel('white'));
171
        $mask->annotateImage($draw, 11, 13, 0, $text);
172
        $mask->annotateImage($draw, 10, 12, 0, $text);
173
        $draw->setFillColor(new \ImagickPixel('black'));
174
        $mask->annotateImage($draw, 9, 11, 0, $text);
175
        $mask->setImageMatte(false);
176
        $image->compositeImage($mask, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
177
178
        return $image;
179
    }
180
181
    public function crop(int $width, int $height, int $startX, int $startY): ImageInterface
182
    {
183
        $this->getImage()->cropImage($width, $height, $startX, $startY);
184
        $this->setSizes();
185
186
        return $this;
187
    }
188
189
    public function save(string $filename): bool
190
    {
191
        return true;
192
    }
193
194
    public function __toString(): string
195
    {
196
        return $this->getImage()->getImageBlob();
197
    }
198
}
199