Passed
Push — master ( 174a4a...437953 )
by compolom
01:54
created

Imagick   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 15

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A grayscale() 0 5 1
A watermark() 0 3 1
A resize() 0 6 1
A __toString() 0 3 1
A rotate() 0 6 1
A flip() 0 5 1
A crop() 0 6 1
A flop() 0 5 1
A save() 0 3 1
A newBackgroundImage() 0 7 1
A setSizes() 0 5 1
A tmp() 0 16 3
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->newBackgroundImage($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 newBackgroundImage(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
    public function resize(int $width, int $height): ImageInterface
59
    {
60
        $this->getImage()->scaleImage($width, $height, false);
61
        $this->setSizes();
62
63
        return $this;
64
    }
65
66
    public function rotate(int $angle = 90): ImageInterface
67
    {
68
        $this->image->rotateImage(new \ImagickPixel('transparent'), $angle);
69
        $this->setSizes();
70
71
        return $this;
72
    }
73
74
    public function watermark(): ImageInterface
75
    {
76
        return $this;
77
    }
78
79
    public function flip(): ImageInterface
80
    {
81
        $this->getImage()->flipImage();
82
83
        return $this;
84
    }
85
86
    public function flop(): ImageInterface
87
    {
88
        $this->getImage()->flopImage();
89
90
        return $this;
91
    }
92
93
    public function grayscale(): ImageInterface
94
    {
95
        $this->getImage()->modulateImage(100, 0, 100);
96
97
        return $this;
98
    }
99
100
    public function crop(int $width, int $height, int $startX, int $startY): ImageInterface
101
    {
102
        $this->getImage()->cropImage($width, $height, $startX, $startY);
103
        $this->setSizes();
104
105
        return $this;
106
    }
107
108
    public function save(string $filename): bool
109
    {
110
        return true;
111
    }
112
113
    public function __toString(): string
114
    {
115
        return $this->getImage()->getImageBlob();
116
    }
117
}
118