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