|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Compolomus\Compomage; |
|
4
|
|
|
|
|
5
|
|
|
use Compolomus\Compomage\Interfaces\ImageInterface; |
|
6
|
|
|
|
|
7
|
|
|
class GD extends AbstractImage implements ImageInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* GD 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 = imagecreatefromstring($source); |
|
27
|
|
|
if (!is_resource($image)) { |
|
28
|
|
|
throw new \Exception('Image create failed'); |
|
29
|
|
|
} |
|
30
|
|
|
$this->setImage($image); |
|
31
|
|
|
$this->setSizes(); |
|
32
|
|
|
// save transparent |
|
33
|
|
|
imagesavealpha($this->getImage(), true); |
|
34
|
|
|
imagealphablending($this->getImage(), false); |
|
35
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function flip(): ImageInterface |
|
40
|
|
|
{ |
|
41
|
|
|
imageflip($this->getImage(), IMG_FLIP_VERTICAL); |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function flop(): ImageInterface |
|
47
|
|
|
{ |
|
48
|
|
|
imageflip($this->getImage(), IMG_FLIP_HORIZONTAL); |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function grayscale(): ImageInterface |
|
54
|
|
|
{ |
|
55
|
|
|
imagefilter($this->getImage(), IMG_FILTER_GRAYSCALE); |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $text |
|
62
|
|
|
* @param string $font |
|
63
|
|
|
* @param string $position |
|
64
|
|
|
* @return $this |
|
65
|
|
|
* @throws \Exception |
|
66
|
|
|
*/ |
|
67
|
|
|
public function copyright(string $text, string $font, string $position = 'SouthWest') |
|
68
|
|
|
{ |
|
69
|
|
|
if (!array_key_exists(strtoupper($position), $this->positions)) { |
|
70
|
|
|
throw new \Exception('Wrong position'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
imagecopymerge( |
|
74
|
|
|
$this->getImage(), |
|
75
|
|
|
$image = $this->prepareImage($text, $font), |
|
76
|
|
|
intval((($this->getWidth() - imagesx($image)) / 2) * $this->positions[strtoupper($position)]['x']) + $this->positions[strtoupper($position)]['padX'], |
|
77
|
|
|
intval((($this->getHeight() - imagesy($image)) / 2) * $this->positions[strtoupper($position)]['y']) + $this->positions[strtoupper($position)]['padY'], |
|
78
|
|
|
0, |
|
79
|
|
|
0, |
|
80
|
|
|
$this->getWidth(), |
|
81
|
|
|
$this->getHeight(), |
|
82
|
|
|
80 |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $text |
|
90
|
|
|
* @param string $font |
|
91
|
|
|
* @return resource |
|
92
|
|
|
* @throws \Exception |
|
93
|
|
|
*/ |
|
94
|
|
|
private function prepareImage(string $text, string $font) |
|
95
|
|
|
{ |
|
96
|
|
|
if (!$coordinates = imagettfbbox($fontSize = 15, 0, $font, $text)) { |
|
97
|
|
|
throw new \Exception('Does not support font'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$minX = min([$coordinates[0], $coordinates[2], $coordinates[4], $coordinates[6]]); |
|
101
|
|
|
$maxX = max([$coordinates[0], $coordinates[2], $coordinates[4], $coordinates[6]]); |
|
102
|
|
|
$minY = min([$coordinates[1], $coordinates[3], $coordinates[5], $coordinates[7]]); |
|
103
|
|
|
$maxY = max([$coordinates[1], $coordinates[3], $coordinates[5], $coordinates[7]]); |
|
104
|
|
|
$textX = intval(abs($minX)) + 1; |
|
105
|
|
|
$textY = intval(abs($minY)) + 1; |
|
106
|
|
|
$image = $this->newImage($maxX - $minX + 2, $maxY - $minY + 2); |
|
107
|
|
|
imagecolortransparent($image, $white = imagecolorallocate($image, 0, 0, 0)); |
|
108
|
|
|
imagefilledrectangle($image, 0, 0, $this->getWidth(), 20, $white); |
|
109
|
|
|
imagettftext($image, $fontSize, 0, $textX, $textY, $white, $font, $text); |
|
110
|
|
|
imagettftext($image, $fontSize, 0, $textX + 1, $textY + 1, $blue = imagecolorallocate($image, 0, 128, 128), $font, $text); |
|
111
|
|
|
imagettftext($image, $fontSize, 0, $textX + 1, $textY + 1, $red = imagecolorallocate($image, 255, 0, 0), $font, $text); |
|
112
|
|
|
imagettftext($image, $fontSize, 0, $textX + 2, $textY + 2, $black = imagecolorallocate($image, 255, 255, 255), $font, $text); |
|
113
|
|
|
imagettftext($image, $fontSize, 0, $textX + 2, $textY + 2, $gray = imagecolorallocate($image, 128, 128, 128), $font, $text); |
|
114
|
|
|
|
|
115
|
|
|
return $image; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected function setSizes(): void |
|
119
|
|
|
{ |
|
120
|
|
|
$this->setWidth(imagesx($this->getImage())); |
|
121
|
|
|
$this->setHeight(imagesy($this->getImage())); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
private function newImage(int $width, int $height) |
|
125
|
|
|
{ |
|
126
|
|
|
$newimg = imagecreatetruecolor($width, $height); |
|
127
|
|
|
$transparent = imagecolorallocatealpha($newimg, 255, 255, 255, 127); |
|
128
|
|
|
imagefill($newimg, 0, 0, $transparent); |
|
129
|
|
|
imagealphablending($newimg, true); |
|
130
|
|
|
imagesavealpha($newimg, true); |
|
131
|
|
|
|
|
132
|
|
|
return $newimg; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function resize(int $width, int $height): ImageInterface |
|
136
|
|
|
{ |
|
137
|
|
|
$newimage = $this->newImage($width, $height); |
|
138
|
|
|
imagecopyresampled($newimage, $this->getImage(), 0, 0, 0, 0, $width, $height, $this->getWidth() , $this->getHeight()); |
|
139
|
|
|
$this->setImage($newimage); |
|
140
|
|
|
$this->setSizes(); |
|
141
|
|
|
|
|
142
|
|
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function crop(int $width, int $height, int $x, int $y): ImageInterface |
|
146
|
|
|
{ |
|
147
|
|
|
$width = $width - $x; |
|
148
|
|
|
$height = $height - $y; |
|
149
|
|
|
$newimage = $this->newImage($width, $height); |
|
150
|
|
|
imagecopyresampled($newimage, $this->getImage(), 0, 0, $x, $y, $width, $height, $width, $height); |
|
151
|
|
|
$this->setImage($newimage); |
|
152
|
|
|
$this->setSizes(); |
|
153
|
|
|
|
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param Image $watermark |
|
159
|
|
|
* @param int $x |
|
160
|
|
|
* @param int $y |
|
161
|
|
|
* @return ImageInterface |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function prepareWatermark(Image $watermark, int $x, int $y): ImageInterface |
|
164
|
|
|
{ |
|
165
|
|
|
imagealphablending($this->getImage(), true); |
|
166
|
|
|
imagesavealpha($this->getImage(), true); |
|
167
|
|
|
imagecopyresampled( |
|
168
|
|
|
$this->getImage(), |
|
169
|
|
|
$watermark->getImage(), |
|
|
|
|
|
|
170
|
|
|
$x, |
|
171
|
|
|
$y, |
|
172
|
|
|
0, |
|
173
|
|
|
0, |
|
174
|
|
|
$width = $watermark->getWidth(), |
|
|
|
|
|
|
175
|
|
|
$height = $watermark->getHeight(), |
|
|
|
|
|
|
176
|
|
|
$width, |
|
177
|
|
|
$height |
|
178
|
|
|
); |
|
179
|
|
|
|
|
180
|
|
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function rotate(int $angle = 90): ImageInterface |
|
184
|
|
|
{ |
|
185
|
|
|
$transparent = imagecolorallocatealpha($this->image, 0, 0, 0, 127); |
|
186
|
|
|
$rotate = imagerotate($this->getImage(), $angle, $transparent); |
|
187
|
|
|
imagealphablending($rotate, true); |
|
188
|
|
|
imagesavealpha($rotate, true); |
|
189
|
|
|
$this->setImage($rotate); |
|
190
|
|
|
$this->setSizes(); |
|
191
|
|
|
|
|
192
|
|
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function __toString(): string |
|
196
|
|
|
{ |
|
197
|
|
|
ob_start(); |
|
198
|
|
|
imagepng($this->getImage(), null, 9, PNG_ALL_FILTERS); |
|
199
|
|
|
$temp = ob_get_contents(); |
|
200
|
|
|
ob_clean(); |
|
201
|
|
|
|
|
202
|
|
|
return trim($temp); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function save(string $filename): bool |
|
206
|
|
|
{ |
|
207
|
|
|
return true; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
If you implement
__calland 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
__callis implemented by a parent class and only the child class knows which methods exist: