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'); |
|
|
|
|
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
|
|
|
|