|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Compolomus\Compomage; |
|
4
|
|
|
|
|
5
|
|
|
use Compolomus\Compomage\Interfaces\ImageInterface; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractImage |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
protected $positions = [ |
|
11
|
|
|
'NORTHWEST' => ['x' => 0, 'y' => 0, 'padX' => 10, 'padY' => 10], |
|
12
|
|
|
'NORTH' => ['x' => 1, 'y' => 0, 'padX' => 0, 'padY' => 10], |
|
13
|
|
|
'NORTHEAST' => ['x' => 2, 'y' => 0, 'padX' => -10, 'padY' => 10], |
|
14
|
|
|
'WEST' => ['x' => 0, 'y' => 1, 'padX' => 10, 'padY' => 0], |
|
15
|
|
|
'CENTER' => ['x' => 1, 'y' => 1, 'padX' => 0, 'padY' => 0], |
|
16
|
|
|
'EAST' => ['x' => 2, 'y' => 1, 'padX' => -10, 'padY' => 0], |
|
17
|
|
|
'SOUTHWEST' => ['x' => 0, 'y' => 2, 'padX' => 10, 'padY' => -10], |
|
18
|
|
|
'SOUTH' => ['x' => 1, 'y' => 2, 'padX' => 0, 'padY' => -10], |
|
19
|
|
|
'SOUTHEAST' => ['x' => 2, 'y' => 2, 'padX' => -10, 'padY' => -10] |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
protected $image; |
|
23
|
|
|
|
|
24
|
|
|
protected $width; |
|
25
|
|
|
|
|
26
|
|
|
protected $height; |
|
27
|
|
|
|
|
28
|
|
|
abstract protected function setSizes(): void; |
|
29
|
|
|
|
|
30
|
|
|
abstract protected function resize(int $width, int $height): ImageInterface; |
|
31
|
|
|
|
|
32
|
|
|
abstract protected function prepareWatermark(Image $watermark, int $x, int $y): ImageInterface; |
|
33
|
|
|
|
|
34
|
|
|
abstract protected function tmp(string $source): ImageInterface; |
|
35
|
|
|
|
|
36
|
|
|
abstract public function __toString(): string; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $image |
|
40
|
|
|
* @throws \Exception |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function init(string $image) |
|
43
|
|
|
{ |
|
44
|
|
|
switch ($image) { |
|
45
|
|
|
// base64 |
|
46
|
|
|
case base64_encode(base64_decode($image, true)) == $image : |
|
|
|
|
|
|
47
|
|
|
$this->getImageByBase64($image); |
|
48
|
|
|
break; |
|
49
|
|
|
// URL |
|
50
|
|
|
case (substr($image, 0, 4) == 'http') : |
|
|
|
|
|
|
51
|
|
|
$this->getImageByURL($image); |
|
52
|
|
|
break; |
|
53
|
|
|
// Local file |
|
54
|
|
|
default: |
|
55
|
|
|
$this->tmp(file_get_contents($image)); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $url |
|
61
|
|
|
* @return \Exception|null |
|
62
|
|
|
* @throws \Exception |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function getImageByURL(string $url): ?\Exception |
|
65
|
|
|
{ |
|
66
|
|
|
[, , $type] = getimagesize($url); |
|
|
|
|
|
|
67
|
|
|
if ($type) { |
|
68
|
|
|
$upload = new \SplFileObject($url, 'rb'); |
|
69
|
|
|
$image = ''; |
|
70
|
|
|
while (!$upload->eof()) { |
|
71
|
|
|
$image .= $upload->fgets(); |
|
72
|
|
|
} |
|
73
|
|
|
} else { |
|
74
|
|
|
throw new \Exception('Unsupported image type'); |
|
75
|
|
|
} |
|
76
|
|
|
$this->tmp($image); |
|
77
|
|
|
|
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $base64 |
|
83
|
|
|
* @throws \Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getImageByBase64(string $base64): void |
|
86
|
|
|
{ |
|
87
|
|
|
$this->tmp(base64_decode($base64)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function setImage($image): void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->image = $image; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getImage() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->image; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function getWidth(): int |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->width; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function setWidth(int $width): void |
|
106
|
|
|
{ |
|
107
|
|
|
$this->width = $width; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getHeight(): int |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->height; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
protected function setHeight(int $height): void |
|
116
|
|
|
{ |
|
117
|
|
|
$this->height = $height; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param int $height |
|
122
|
|
|
* @return ImageInterface |
|
123
|
|
|
*/ |
|
124
|
|
|
public function resizeByHeight(int $height): ImageInterface |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->resize($this->getWidth() * ($height / $this->getHeight()), $height); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param int $width |
|
131
|
|
|
* @return ImageInterface |
|
132
|
|
|
*/ |
|
133
|
|
|
public function resizeByWidth(int $width): ImageInterface |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->resize($width, $this->getHeight() * ($width / $this->getWidth())); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param int $percent |
|
140
|
|
|
* @return ImageInterface |
|
141
|
|
|
*/ |
|
142
|
|
|
public function resizeByPercent(int $percent): ImageInterface |
|
143
|
|
|
{ |
|
144
|
|
|
$width = $this->getWidth() * ($percent / 100); |
|
145
|
|
|
$height = $this->getHeight() * ($percent / 100); |
|
146
|
|
|
return $this->resize($width, $height); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $mode |
|
151
|
|
|
* @param int $param |
|
152
|
|
|
* @return ImageInterface |
|
153
|
|
|
* @throws \Exception |
|
154
|
|
|
*/ |
|
155
|
|
|
public function resizeBy(string $mode, int $param): ImageInterface |
|
156
|
|
|
{ |
|
157
|
|
|
switch ($mode) { |
|
158
|
|
|
case 'width': |
|
159
|
|
|
return $this->resizeByWidth($param); |
|
160
|
|
|
case 'height': |
|
161
|
|
|
return $this->resizeByHeight($param); |
|
162
|
|
|
case 'percent': |
|
163
|
|
|
return $this->resizeByPercent($param); |
|
164
|
|
|
default: |
|
165
|
|
|
throw new \Exception('Unsupported mode type by resize'); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param Image $watermark |
|
171
|
|
|
* @param string $position |
|
172
|
|
|
* @return ImageInterface |
|
173
|
|
|
* @throws \Exception |
|
174
|
|
|
*/ |
|
175
|
|
|
public function watermark(Image $watermark, string $position): ImageInterface |
|
176
|
|
|
{ |
|
177
|
|
|
if (!array_key_exists(strtoupper($position), $this->positions)) { |
|
178
|
|
|
throw new \Exception('Wrong position'); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $this->prepareWatermark( |
|
182
|
|
|
$watermark, |
|
183
|
|
|
intval((($this->getWidth() - $watermark->getWidth()) / 2) * $this->positions[strtoupper($position)]['x']) + $this->positions[strtoupper($position)]['padX'], |
|
|
|
|
|
|
184
|
|
|
intval((($this->getHeight() - $watermark->getHeight()) / 2) * $this->positions[strtoupper($position)]['y']) + $this->positions[strtoupper($position)]['padY'] |
|
|
|
|
|
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function getBase64(): string |
|
189
|
|
|
{ |
|
190
|
|
|
return chunk_split(base64_encode($this->__toString())); |
|
191
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.