|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CoffeeCode\Uploader; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class CoffeeCode Image |
|
7
|
|
|
* |
|
8
|
|
|
* @author Robson V. Leite <https://github.com/robsonvleite> |
|
9
|
|
|
* @package CoffeeCode\Uploader |
|
10
|
|
|
*/ |
|
11
|
|
|
class Image extends Uploader |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Allow jpg, png and gif images, use from check. For new extensions check the imageCrete method |
|
15
|
|
|
* |
|
16
|
|
|
* @var array allowed media types |
|
17
|
|
|
*/ |
|
18
|
|
|
protected static $allowTypes = [ |
|
19
|
|
|
"image/jpeg", |
|
20
|
|
|
"image/png", |
|
21
|
|
|
"image/gif", |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
public function upload(array $image, string $name, int $width = 2000, ?array $quality = null): string |
|
26
|
|
|
{ |
|
27
|
|
|
if (empty($image['type'])) { |
|
28
|
|
|
throw new \Exception("Not a valid data from image"); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (!$this->imageCreate($image)) { |
|
32
|
|
|
throw new \Exception("{$image['type']} - Not a valid image type"); |
|
33
|
|
|
} else { |
|
34
|
|
|
$this->name($name); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if ($this->ext == "gif") { |
|
38
|
|
|
move_uploaded_file("{$image['tmp_name']}", "{$this->path}/{$this->name}"); |
|
39
|
|
|
return "{$this->path}/{$this->name}"; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->imageGenerate($width, ($quality ?? ["jpg" => 75, "png" => 5])); |
|
43
|
|
|
return "{$this->path}/{$this->name}"; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param int $width |
|
48
|
|
|
* @param array $quality |
|
49
|
|
|
*/ |
|
50
|
|
|
private function imageGenerate(int $width, array $quality) |
|
51
|
|
|
{ |
|
52
|
|
|
$fileX = imagesx($this->file); |
|
53
|
|
|
$fileY = imagesy($this->file); |
|
54
|
|
|
$imageW = ($width < $fileX ? $width : $fileX); |
|
55
|
|
|
$imageH = ($imageW * $fileY) / $fileX; |
|
56
|
|
|
$imageCreate = imagecreatetruecolor($imageW, $imageH); |
|
57
|
|
|
|
|
58
|
|
|
if ($this->ext == "jpg") { |
|
59
|
|
|
imagecopyresampled($imageCreate, $this->file, 0, 0, 0, 0, $imageW, $imageH, $fileX, $fileY); |
|
60
|
|
|
imagejpeg($imageCreate, "{$this->path}/{$this->name}", $quality['jpg']); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ($this->ext == "png") { |
|
64
|
|
|
imagealphablending($imageCreate, false); |
|
65
|
|
|
imagesavealpha($imageCreate, true); |
|
66
|
|
|
imagecopyresampled($imageCreate, $this->file, 0, 0, 0, 0, $imageW, $imageH, $fileX, $fileY); |
|
67
|
|
|
imagepng($imageCreate, "{$this->path}/{$this->name}", $quality['png']); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
imagedestroy($this->file); |
|
71
|
|
|
imagedestroy($imageCreate); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Image create from mime-type |
|
76
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#Image_types |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $image |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function imageCreate(array $image): bool |
|
82
|
|
|
{ |
|
83
|
|
|
if ($image['type'] == "image/jpeg") { |
|
84
|
|
|
$this->file = imagecreatefromjpeg($image['tmp_name']); |
|
85
|
|
|
$this->ext = "jpg"; |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($image['type'] == "image/png") { |
|
90
|
|
|
$this->file = imagecreatefrompng($image['tmp_name']); |
|
91
|
|
|
$this->ext = "png"; |
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if ($image['type'] == "image/gif") { |
|
96
|
|
|
$this->ext = "gif"; |
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
} |