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