$minWidth ?? $this->minWidth, 'minHeight' => $minHeight ?? $this->minHeight, 'maxWidth' => $maxWidth ?? $this->maxWidth, 'maxHeight' => $maxHeight ?? $this->maxHeight ]; return [ $name => [ !$nullable ? 'required' : 'nullable', (strlen($requiredWith)) ? 'required_with:' . $requiredWith : '', (strlen($requiredWithout)) ? 'required_without:' . $requiredWithout : '', ($size) ? 'max:' . $size : '', function (string $attribute, $value, callable $fail) use ($dimensions) { self::validatePicture($attribute, $value, $fail, $dimensions); } ] ]; } /** * Validate picture * * @param string $attribute * @param string|null $value * @param callable $fail * @param object $sizes * @return void */ public static function validatePicture(string $attribute, string|null $value, callable $fail, object $sizes): void { if (!($useStorage = Storage::exists($value)) && !File::exists($value)) { $fail(trans(':attribute n\'existe pas dans le système de fichier', [ 'attribute' => $attribute ])); return; } $fileFullPath = self::getUploadedFileFullPath($value, $useStorage); if ( !\in_array(self::getUploadedFileMimeType($value, $useStorage), [ 'image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/svg+xml' ]) ) { $fail(trans(':attribute doit être une image de type \'image/jpeg,image/jpg,image/png,image/gif\',image/svg\'', [ 'attribute' => $attribute ])); return; } if (self::getUploadedFileMimeType($value, $useStorage) !== "image/svg+xml") { if (!($dimensions = \getimagesize($fileFullPath))) { $fail(trans(':attribute impossible de valider les dimensions', [ 'attribute' => $attribute ])); } else { $width = intval($dimensions[0]); $height = intval($dimensions[1]); $xMessage = trans( ":attribute la largeur doit être comprise entre {$sizes->minWidth}px et {$sizes->maxWidth}px" ); if ($sizes->minWidth === $sizes->maxWidth) { $xMessage = trans(":attribute la largeur doit être de {$sizes->minWidth}px"); } $yMessage = trans( ":attribute la hauteur doit être comprise entre {$sizes->minHeight}px et {$sizes->maxHeight}px" ); if ($sizes->minHeight === $sizes->maxHeight) { $yMessage = trans(":attribute la hauteur doit être de {$sizes->minHeight}px"); } if (($width < $sizes->minWidth) || ($width > $sizes->maxWidth)) { $fail($xMessage); } if (($height < $sizes->minHeight) || ($height > $sizes->maxHeight)) { $fail($yMessage); } } //end if } } /** * Get the file full path * * @param string $value * @param boolean $useStorage * @return string */ private static function getUploadedFileFullPath(string $value, bool $useStorage): string { /** @var \Illuminate\Filesystem\FilesystemAdapter */ $disk = Storage::disk(); return !$useStorage ? (realpath($value) ?: $value) : $disk->path($value); } /** * Get the file mimetype * * @param string $value * @param boolean $useStorage * @return string|false */ private static function getUploadedFileMimeType(string $value, bool $useStorage) { return !$useStorage ? File::mimeType($value) : Storage::mimeType($value); } }