A complete Image validation rules in Laravel

A complete Image validation rules in Laravel

Laravel provides a lot of validation rules which makes our development process faster and easier. In this post, we are going to explain a set of validation rules for Image. Let’s start with the basic one.

Image

The file under validation must be an image.

MIME

We can limit the MIME types of uploaded image using mimes in validation rules. So, the file under validation must have a MIME type corresponding to one of the listed extensions. A image with only jpeg(jpg) or png extensions are allowed with the example below.

Size

The field under validation must have a size matching the given value. This works for both string and file types. For string data, the value corresponds to the number of characters. For numeric data, the value corresponds to a given integer value. For an array, size corresponds to the count of the array. For files, size corresponds to the file size in kilobytes.

Dimensions

This dimensions validation rule is quite interesting. We can pass a number of parameters with it. The set of parameters that can be passed are as below:

  • min_width: Images should not be narrower than this pixel width
  • max_width: Images should not be wider than this pixel width
  • min_height: Images should not be shorter than this pixel height
  • max_height: Images should not be taller than this pixel height
  • width: Images should be exactly this pixel width
  • height: Images should be exactly this pixel height
  • ratio: Images should be exactly this ratio (width/height, expressed as “width/height”)

Now what?
You can implement these validation rules for uploading multiple images in laravel 5. On the other hand, you can also create an image gallery system following these articles.

This is all for validating image. Hope you found this article interesting. If you have any queries, don’t forget to drop a comment below. Happy Coding.