Home > front end >  How to check if the submitted picture is horizontal? Laravel
How to check if the submitted picture is horizontal? Laravel

Time:07-29

I created a custom validation rule by using the command: php artisan make:rule Horizontal

Now, how to check if the submitted picture from my post form is horizontal? I tried this:

 public function passes($attribute, $value)
{

    if ($this->request->has('picture')) {
        $image = $this->request->get('picture');
        if ($image->width < $image->height) {
            session()->flash('error', 'Image must be horizontal!');
            return false;
        }
    }
}

public function message()
{
    return 'The validation error message.';
}

My Post Request:

public function rules()
{
    return [
        'title' => 'required|min:2|max:255',
        'body' => 'required|min:10',
        'picture' => [
            'required',
            new Horizontal()
        ]
    ];
}

CodePudding user response:

You can do something like this

$image = $this->request->file('post_image');
list($width, $height) = getimagesize($image);
if ($width < $height) {
     session()->flash('error', 'Image must be horizontal!');
     return false;
}

CodePudding user response:

After Image upload Laravel Intervention Image Can help like this:

$img = Image::make('public/foo.jpg');

$img->flip('h');

h for horizontal and v for vertical.

There is one more method orientate to set auto Orientation as well

You can compare height and width as well

This image library required GD library and Imagick plugin

Good luck!

  • Related