getMimeType(); if (! str_starts_with((string) $mime, 'image/')) { $fail("The :attribute must be a valid image file."); } return; } if (is_string($value)) { $payload = trim($value); if (\Illuminate\Support\Str::isUuid($payload)) { return; } if ($payload === '') { $fail("The :attribute must not be empty."); return; } $declaredMimeType = null; if (preg_match('/^data:(?[-\w.+\/]+);base64,(?.+)$/s', $payload, $matches) === 1) { $declaredMimeType = strtolower($matches['mime']); $payload = $matches['data']; } $decoded = base64_decode(preg_replace('/\s+/', '', $payload), true); if ($decoded === false || $decoded === '') { $fail("The :attribute must be a valid base64-encoded image."); return; } $finfo = new \finfo(FILEINFO_MIME_TYPE); $detectedMime = $finfo->buffer($decoded); $mime = $detectedMime ?: $declaredMimeType; if (! $mime || ! str_starts_with((string) $mime, 'image/')) { $fail("The :attribute must be a valid image (file or base64)."); } return; } $fail("The :attribute must be an image file or a base64-encoded image string."); } }