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
|
|
|
|