1 | <?php |
||
13 | final class ImageFileUpload implements ImageUploadRequestInterface |
||
14 | { |
||
15 | const ALLOWED_FILE_TYPES = [ |
||
16 | IMAGETYPE_GIF => 'GIF', |
||
17 | IMAGETYPE_JPEG => 'JPEG', |
||
18 | IMAGETYPE_PNG => 'PNG', |
||
19 | IMAGETYPE_BMP => 'BMP', |
||
20 | IMAGETYPE_TIFF_II => 'TIFF', |
||
21 | IMAGETYPE_TIFF_MM => 'TIFF' |
||
22 | ]; |
||
23 | const MAX_FILE_SIZE_MB = 5; |
||
24 | const MINIMAL_HEIGHT = 50; |
||
25 | const MINIMAL_WIDTH = 50; |
||
26 | |||
27 | private $imageFormat; |
||
28 | private $imageData; |
||
29 | |||
30 | 6 | public function __construct(string $filename) |
|
36 | |||
37 | 2 | public function getUploadRequest(): array |
|
44 | |||
45 | 6 | private function validateFile(string $file) |
|
46 | { |
||
47 | 6 | self::validateFileSize($file); |
|
48 | 5 | self::validatedImageType($file); |
|
49 | 3 | self::validateImageSize($file); |
|
50 | 2 | } |
|
51 | |||
52 | 6 | private static function validateFileSize($file) |
|
53 | { |
||
54 | 6 | $maxFileSizeBytes = (self::MAX_FILE_SIZE_MB * 1024 * 1024); |
|
55 | 6 | if (filesize($file) > $maxFileSizeBytes) { |
|
56 | 1 | throw new MaximumImageFileSizeExcededException(sprintf('Max file size is \'%d Mb\'', self::MAX_FILE_SIZE_MB)); |
|
57 | } |
||
58 | 5 | } |
|
59 | |||
60 | 5 | private static function validatedImageType(string $file) |
|
61 | { |
||
62 | 5 | $type = false; |
|
63 | |||
64 | try { |
||
65 | 5 | $type = exif_imagetype($file); |
|
66 | 1 | } catch (Exception $e) { |
|
67 | //-- $type is already false |
||
68 | } |
||
69 | |||
70 | 5 | if ($type === false) { |
|
71 | 1 | throw new FileIsNotAnImageException(); |
|
72 | } |
||
73 | |||
74 | 4 | if (!array_key_exists($type, self::ALLOWED_FILE_TYPES)) { |
|
75 | 1 | throw new ImageTypeNotAllowedException( |
|
76 | 1 | sprintf('Allowed: %s', implode(', ', self::ALLOWED_FILE_TYPES)) |
|
77 | ); |
||
78 | } |
||
79 | 3 | } |
|
80 | |||
81 | 3 | private static function validateImageSize(string $file) |
|
97 | } |
||
98 |