blob: c26377cbddf5f87f10efd172d38efdb4b8860712 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<?php
namespace Studip\Services;
final class ImageValidator
{
public const VALID_EXTENSIONS = [
'gif',
'jpeg', 'jpg',
'png',
'webp',
];
public const VALID_MIMETYPES = [
'image/gif',
'image/jpeg',
'image/png',
'image/webp',
];
public function validate(string $filename): bool
{
return $this->validateName($filename)
&& $this->validateMimeType(get_mime_type($filename))
&& $this->validateContents($filename);
}
public function validateMimeType(string $mime_type): bool
{
return str_starts_with($mime_type, 'image/')
&& in_array($mime_type, self::VALID_MIMETYPES);
}
public function validateName(string $filename): bool
{
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$extension = strtolower($extension);
return in_array($extension, self::VALID_EXTENSIONS);
}
public function validateContents(string $filename): bool
{
$check = imagecreatefromstring(file_get_contents($filename));
if ($check === false) {
return false;
}
imagedestroy($check);
return true;
}
}
|