|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* KNUT7 K7F (https://marciozebedeu.com/) |
|
4
|
|
|
* KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under The MIT License |
|
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
9
|
|
|
* |
|
10
|
|
|
* @link https://github.com/knut7/framework/ for the canonical source repository |
|
11
|
|
|
* @copyright (c) 2015. KNUT7 Software Technologies AO Inc. (https://marciozebedeu.com/) |
|
12
|
|
|
* @license https://marciozebedeu.com/license/new-bsd New BSD License |
|
13
|
|
|
* @author Marcio Zebedeu - [email protected] |
|
14
|
|
|
* @version 1.0.2 |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace Ballybran\Helpers\Images; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
use Ballybran\Helpers\Images\ImageInterface\ResizeInterface; |
|
21
|
|
|
|
|
22
|
|
|
class ImageResize implements ResizeInterface |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
private $file; |
|
27
|
|
|
private $image; |
|
28
|
|
|
private $width; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return mixed |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getFile() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->file; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return resource |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getImage() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->image; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getWidth() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->width; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getHeight() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->height; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return mixed |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getBits() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->bits; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return mixed |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getMime() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->mime; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param mixed $file |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setFile($file) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->file = $file; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param resource $image |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setImage($image) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->image = $image; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param mixed $width |
|
96
|
|
|
*/ |
|
97
|
|
|
public function setWidth($width) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->width = $width; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param mixed $height |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setHeight($height) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->height = $height; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $bits |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setBits(string $bits) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->bits = $bits; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $mime |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setMime(string $mime) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->mime = $mime; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private $height; |
|
127
|
|
|
private $bits; |
|
128
|
|
|
private $mime; |
|
129
|
|
|
|
|
130
|
|
|
public function upload($file) |
|
131
|
|
|
{ |
|
132
|
|
|
if (file_exists($file)) { |
|
133
|
|
|
$this->file = $file; |
|
134
|
|
|
|
|
135
|
|
|
$info = getimagesize($file); |
|
136
|
|
|
|
|
137
|
|
|
$this->setWidth($info[0]); |
|
138
|
|
|
$this->setHeight($info[1]); |
|
139
|
|
|
$this->setBits(isset($info['bits']) ? $info['bits'] : ''); |
|
140
|
|
|
$this->setMime(isset($info['mime']) ? $info['mime'] : ''); |
|
141
|
|
|
|
|
142
|
|
|
if ($this->getMime() == 'image/gif') { |
|
143
|
|
|
$this->image = imagecreatefromgif($file); |
|
144
|
|
|
$this->setImage($this->image); |
|
|
|
|
|
|
145
|
|
|
} elseif ($this->mime == 'image/png') { |
|
146
|
|
|
$this->image = imagecreatefrompng($file); |
|
147
|
|
|
$this->setImage($this->image); |
|
148
|
|
|
|
|
149
|
|
|
} elseif ($this->mime == 'image/jpeg') { |
|
150
|
|
|
$this->image = imagecreatefromjpeg($file); |
|
151
|
|
|
$this->setImage($this->image); |
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
} else { |
|
155
|
|
|
exit('Error: Could not load image ' . $file . '!'); |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* |
|
162
|
|
|
* @param type $file Inser you file |
|
163
|
|
|
* @param type $quality Isert optional quality for image |
|
|
|
|
|
|
164
|
|
|
* Exemple save('exemple.jpg, 100); |
|
165
|
|
|
*/ |
|
166
|
|
|
public function save(string $savePath, int $imageQuality = 100) |
|
167
|
|
|
{ |
|
168
|
|
|
$info = pathinfo($savePath); |
|
169
|
|
|
|
|
170
|
|
|
$extension = strtolower($info['extension']); |
|
171
|
|
|
|
|
172
|
|
|
if (is_resource($this->image)) { |
|
173
|
|
|
if ($extension == 'jpeg' || $extension == 'jpg') { |
|
174
|
|
|
imagejpeg($this->image, $savePath, $imageQuality); |
|
175
|
|
|
} elseif ($extension == 'png') { |
|
176
|
|
|
imagepng($this->image, $savePath); |
|
177
|
|
|
} elseif ($extension == 'gif') { |
|
178
|
|
|
imagegif($this->image, $savePath); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
imagedestroy($this->image); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* |
|
187
|
|
|
* @param type $width Inser Width for you image |
|
188
|
|
|
* @param type $height Inser height for you image |
|
189
|
|
|
* @param type $default nser you scale (where w = width end h = height) |
|
190
|
|
|
* @return type |
|
191
|
|
|
* Exemplae: resize(800, 600, 'w'); |
|
192
|
|
|
*/ |
|
193
|
|
|
public function resizeImage($width = 0, $height = 0, $option = 'auto') |
|
194
|
|
|
{ |
|
195
|
|
|
if (!$this->width || !$this->height) { |
|
196
|
|
|
return; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$xpos = 0; |
|
|
|
|
|
|
200
|
|
|
$ypos = 0; |
|
|
|
|
|
|
201
|
|
|
$scale = 1; |
|
202
|
|
|
|
|
203
|
|
|
$scale_w = $width / $this->width; |
|
204
|
|
|
$scale_h = $height / $this->height; |
|
205
|
|
|
|
|
206
|
|
|
if ($option == 'w') { |
|
207
|
|
|
$scale = $scale_w; |
|
208
|
|
|
} elseif ($option == 'h') { |
|
209
|
|
|
$scale = $scale_h; |
|
210
|
|
|
} else { |
|
211
|
|
|
$scale = min($scale_w, $scale_h); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
if ($scale == 1 && $scale_h == $scale_w && $this->mime != 'image/png') { |
|
215
|
|
|
return; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$new_width = (int)($this->width * $scale); |
|
219
|
|
|
$new_height = (int)($this->height * $scale); |
|
220
|
|
|
$xpos = (int)(($width - $new_width) / 2); |
|
221
|
|
|
$ypos = (int)(($height - $new_height) / 2); |
|
222
|
|
|
|
|
223
|
|
|
$image_old = $this->image; |
|
224
|
|
|
$this->image = imagecreatetruecolor($width, $height); |
|
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
if ($this->mime == 'image/png') { |
|
227
|
|
|
imagealphablending($this->image, false); |
|
228
|
|
|
imagesavealpha($this->image, true); |
|
229
|
|
|
$background = imagecolorallocatealpha($this->image, 255, 255, 255, 127); |
|
230
|
|
|
imagecolortransparent($this->image, $background); |
|
231
|
|
|
} else { |
|
232
|
|
|
$background = imagecolorallocate($this->image, 255, 255, 255); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
imagefilledrectangle($this->image, 0, 0, $width, $height, $background); |
|
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->width, $this->height); |
|
238
|
|
|
imagedestroy($image_old); |
|
239
|
|
|
|
|
240
|
|
|
$this->width = $width; |
|
241
|
|
|
$this->height = $height; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
public function crop($top_x, $top_y, $bottom_x, $bottom_y) |
|
246
|
|
|
{ |
|
247
|
|
|
$image_old = $this->image; |
|
248
|
|
|
$this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y); |
|
249
|
|
|
|
|
250
|
|
|
imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->width, $this->height); |
|
251
|
|
|
imagedestroy($image_old); |
|
252
|
|
|
|
|
253
|
|
|
$this->width = $bottom_x - $top_x; |
|
254
|
|
|
$this->height = $bottom_y - $top_y; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function imageRotate(int $degree, $color = '000000') |
|
258
|
|
|
{ |
|
259
|
|
|
|
|
260
|
|
|
$rgb = $this->html2rgb($color); |
|
261
|
|
|
|
|
262
|
|
|
$this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2])); |
|
263
|
|
|
|
|
264
|
|
|
$this->width = imagesx($this->image); |
|
265
|
|
|
$this->height = imagesy($this->image); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* |
|
271
|
|
|
* @param type $watermark |
|
272
|
|
|
* @param type $position |
|
273
|
|
|
*/ |
|
274
|
|
|
public function watermark($watermark, $position = 'bottomright') |
|
|
|
|
|
|
275
|
|
|
{ |
|
276
|
|
|
$watermark = imagecreatefromjpeg('DDDDD'); |
|
277
|
|
|
|
|
278
|
|
|
switch ($position) { |
|
279
|
|
|
case 'topleft': |
|
280
|
|
|
$watermark_pos_x = 0; |
|
281
|
|
|
$watermark_pos_y = 0; |
|
282
|
|
|
break; |
|
283
|
|
|
case 'topright': |
|
284
|
|
|
$watermark_pos_x = $this->width - 10; |
|
285
|
|
|
$watermark_pos_y = 0; |
|
286
|
|
|
break; |
|
287
|
|
|
case 'bottomleft': |
|
288
|
|
|
$watermark_pos_x = 0; |
|
289
|
|
|
$watermark_pos_y = $this->height - 10; |
|
290
|
|
|
break; |
|
291
|
|
|
case 'bottomright': |
|
292
|
|
|
$watermark_pos_x = $this->width - 5; |
|
293
|
|
|
$watermark_pos_y = $this->height - 5; |
|
294
|
|
|
break; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, $this->getWidth(), $this->getHeight()); |
|
|
|
|
|
|
298
|
|
|
|
|
299
|
|
|
imagedestroy($watermark); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
private function filter() |
|
|
|
|
|
|
303
|
|
|
{ |
|
304
|
|
|
$args = func_get_args(); |
|
305
|
|
|
|
|
306
|
|
|
call_user_func_array('imagefilter', $args); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') |
|
310
|
|
|
{ |
|
311
|
|
|
$rgb = $this->html2rgb($color); |
|
312
|
|
|
|
|
313
|
|
|
imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2])); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
private function merge($merge, $x = 0, $y = 0, $opacity = 100) |
|
|
|
|
|
|
317
|
|
|
{ |
|
318
|
|
|
imagecopymerge($this->image, $this->getImage(), $x, $y, 0, 0, $this->getWidth(), $this->getHeight(), $opacity); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
private function html2rgb($color) |
|
322
|
|
|
{ |
|
323
|
|
|
if ($color[0] == '#') { |
|
324
|
|
|
$color = substr($color, 1); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
if (strlen($color) == 6) { |
|
328
|
|
|
list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]); |
|
329
|
|
|
} elseif (strlen($color) == 3) { |
|
330
|
|
|
list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]); |
|
331
|
|
|
} else { |
|
332
|
|
|
return false; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
$r = hexdec($r); |
|
336
|
|
|
$g = hexdec($g); |
|
337
|
|
|
$b = hexdec($b); |
|
338
|
|
|
|
|
339
|
|
|
return array($r, $g, $b); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
} |
|
343
|
|
|
|