2020-04-26 21:09:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Rules;
|
|
|
|
|
2024-05-19 13:49:42 +08:00
|
|
|
use Closure;
|
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
2024-01-10 01:47:09 +01:00
|
|
|
use Illuminate\Support\Str;
|
2020-04-26 21:09:43 +02:00
|
|
|
|
2024-05-19 13:49:42 +08:00
|
|
|
class ImageData implements ValidationRule
|
2020-04-26 21:09:43 +02:00
|
|
|
{
|
2024-05-19 13:49:42 +08:00
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
2020-04-26 21:09:43 +02:00
|
|
|
{
|
2024-10-01 00:15:38 +02:00
|
|
|
$passes = rescue(static function () use ($value) {
|
2024-01-10 01:47:09 +01:00
|
|
|
return (bool) preg_match('/data:image\/(jpe?g|png|webp|gif)/i', Str::before($value, ';'));
|
2024-10-01 00:15:38 +02:00
|
|
|
}) ?? false;
|
2020-04-26 21:09:43 +02:00
|
|
|
|
2024-05-19 13:49:42 +08:00
|
|
|
if (!$passes) {
|
|
|
|
$fail('Invalid DataURL string');
|
|
|
|
}
|
2020-04-26 21:09:43 +02:00
|
|
|
}
|
|
|
|
}
|