| Total Complexity | 5 |
| Total Lines | 57 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 16 | trait ImageHelper |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Since Twitter & Petitesannonces share the same supported mime types, we can leave it here |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $supportedMimeTypes = [ |
||
| 24 | 'image/jpeg', |
||
| 25 | 'image/png', |
||
| 26 | 'image/gif', |
||
| 27 | ]; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param $imageName |
||
| 31 | * @throws \Exception |
||
| 32 | */ |
||
| 33 | protected function imageSizeValidation($imageName) |
||
| 34 | { |
||
| 35 | if (Storage::size($imageName) > self::MAX_IMAGE_UPLOAD_SIZE) { |
||
|
|
|||
| 36 | Storage::delete($imageName); |
||
| 37 | throw new \Exception( |
||
| 38 | sprintf( |
||
| 39 | "La taille de l'image ne doit pas excéder %d %s", |
||
| 40 | self::MAX_IMAGE_UPLOAD_SIZE / pow(1024, 2), |
||
| 41 | 'Mo' |
||
| 42 | ) |
||
| 43 | ); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $imageUrl |
||
| 49 | * @throws \Exception |
||
| 50 | */ |
||
| 51 | protected function imageUrlValidation($imageUrl) |
||
| 52 | { |
||
| 53 | $fileInfo = new finfo(FILEINFO_MIME_TYPE); |
||
| 54 | $mimeType = $fileInfo->buffer(file_get_contents($imageUrl)); |
||
| 55 | |||
| 56 | if (!in_array($mimeType, $this->supportedMimeTypes)) { |
||
| 57 | throw new \Exception( |
||
| 58 | sprintf( |
||
| 59 | 'Type de fichier non supporté. Types supportés : [%s]', |
||
| 60 | str_replace('image/', '', implode(', ', $this->supportedMimeTypes)) |
||
| 61 | ) |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param $imageUrl |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | protected function getUniqueNameFromUrl($imageUrl) |
||
| 73 | } |
||
| 74 | } |
||
| 75 |