In my Laravel 5.6 app, I have a controller with a store
method:
use Intervention\Image\Facades\Image as ImageHandler;
public function store(StoreFoo $request)
{
if ($request->hasFile('image')) {
$toResize = ImageHandler::make($request->validated()->file('image')->getRealPath());
}
}
My StoreFoo
class validates that the image
field is an image:
public function rules()
{
return [
'image' => 'image'
];
}
I would expect that when I try to upload a file which is not an image, the validator would catch it and return an error. Instead the code inside the store
method is run, which produces an "Unsupported image type" exception from Intervention.
Why does the validator not catch this beforehand, and how can I make it work this way?
Try this in your request file:
'image' => 'nullable|file|mimes:jpeg,png,jpg',
Of course, feel free to add whatever other mimes you will accept.