|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jonasdamher\Simplifyimage\Core; |
|
6
|
|
|
|
|
7
|
|
|
use Jonasdamher\Simplifyimage\Core\Configuration; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Validate if required file, |
|
11
|
|
|
* size and format image, path save image. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Validate extends Configuration |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
private bool $requiredImage = false; |
|
17
|
|
|
private int $maxSize = 2097152; |
|
18
|
|
|
private array $allowedFormats = [ |
|
19
|
|
|
'png', |
|
20
|
|
|
'jpg', |
|
21
|
|
|
'jpeg', |
|
22
|
|
|
'gif', |
|
23
|
|
|
'webp' |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
// GETS & SETS |
|
27
|
|
|
protected function getRequiredImage(): bool |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->requiredImage; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** Set required file */ |
|
33
|
|
|
public function required() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->requiredImage = true; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function getMaxSize(): int |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->maxSize; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Max size allow for upload images. |
|
45
|
|
|
* |
|
46
|
|
|
* By default is 2097152 bytes (2 MB) |
|
47
|
|
|
* @param int $maxSize |
|
48
|
|
|
*/ |
|
49
|
|
|
public function maxSize(int $maxSize) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->maxSize = $maxSize; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function getAllowedFormats(): array |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->allowedFormats; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// FINAL GETS & SETS |
|
60
|
|
|
|
|
61
|
|
|
private function fileExist(): bool |
|
62
|
|
|
{ |
|
63
|
|
|
return (isset($_FILES[$this->getNameInputFile()]) && mb_strlen($_FILES[$this->getNameInputFile()]['tmp_name']) > 0); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function existFileAndPath(): bool |
|
67
|
|
|
{ |
|
68
|
|
|
try { |
|
69
|
|
|
$ok = true; |
|
70
|
|
|
if (!$this->path->exist()) { |
|
71
|
|
|
throw new \Exception('Dont exist path, your path is (' . $this->path->get() . ').'); |
|
72
|
|
|
} |
|
73
|
|
|
if (!$this->fileExist()) { |
|
74
|
|
|
|
|
75
|
|
|
if ($this->getRequiredImage()) { |
|
76
|
|
|
throw new \Exception("Don't exist image request."); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} catch (\Exception $e) { |
|
80
|
|
|
$ok = false; |
|
81
|
|
|
parent::fail($e->getMessage()); |
|
82
|
|
|
} finally { |
|
83
|
|
|
return $ok; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function sizeValidate(int $size): bool |
|
88
|
|
|
{ |
|
89
|
|
|
return $size <= $this->getMaxSize(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function formatValidate(string $format): bool |
|
93
|
|
|
{ |
|
94
|
|
|
if (!in_array($format, $this->getAllowedFormats(), true)) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
$myFormat = ($format == 'jpg') ? 'jpeg' : $format; |
|
98
|
|
|
|
|
99
|
|
|
$this->imagecreatefrom .= $myFormat; |
|
100
|
|
|
$this->transformImage .= $myFormat; |
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function validateImage(string $format, int $size): bool |
|
105
|
|
|
{ |
|
106
|
|
|
try { |
|
107
|
|
|
$ok = true; |
|
108
|
|
|
if (!$this->sizeValidate($size)) { |
|
109
|
|
|
throw new \Exception('It has to be an image smaller than ' . $this->getMaxSize() . ' Bytes.'); |
|
110
|
|
|
} |
|
111
|
|
|
if (!$this->formatValidate($format)) { |
|
112
|
|
|
throw new \Exception('Invalid image format.'); |
|
113
|
|
|
} |
|
114
|
|
|
} catch (\Exception $e) { |
|
115
|
|
|
$ok = false; |
|
116
|
|
|
Parent::fail($e->getMessage()); |
|
117
|
|
|
} finally { |
|
118
|
|
|
return $ok; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
protected function verifyImagePath(string $imageName): bool |
|
123
|
|
|
{ |
|
124
|
|
|
$imagePath = $this->path->get() . $imageName; |
|
125
|
|
|
return (file_exists($imagePath)); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|