Viewing: ImageUrl.php
<?php namespace App\Rules\Admin; use Illuminate\Contracts\Validation\Rule; class ImageUrl implements Rule { protected $allowedExtensions = [".jpeg", ".jpg", ".png", ".gif", ".webp"]; /** * Create a new rule instance. * * @return void */ public function __construct() { // } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { // Get the file content $fileContent = @file_get_contents($value); if ($fileContent === false) { // Unable to read the file content return false; } // Get the image MIME type $mimeType = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $fileContent); // Check if the MIME type is one of the allowed types $allowedMimeTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif','image/webp']; return in_array($mimeType, $allowedMimeTypes); } /** * Get the validation error message. * * @return string */ public function message() { return 'The image URL must be a file of type: ' . implode(', ', $this->allowedExtensions); } }
Return