Passed
Push — master ( 437953...d92f5b )
by compolom
01:45
created

Imagick::crop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 6
rs 9.4285
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 crop(int $width, int $height, int $startX, int $startY): ImageInterface
107
    {
108
        $this->getImage()->cropImage($width, $height, $startX, $startY);
109
        $this->setSizes();
110
111
        return $this;
112
    }
113
114
    public function save(string $filename): bool
115
    {
116
        return true;
117
    }
118
119
    public function __toString(): string
120
    {
121
        return $this->getImage()->getImageBlob();
122
    }
123
}
124