Bytes
Bytes

Reputation: 21

how to check width and height of image in laravel

Iam new in laravel. I want to check width and height of image before insert into database.

my Model folder contain ForumGallery file code is

public function correct_size($photo)
{
    $maxHeight=822;
    $maxWidth=1237;
    list($width,$height)=getimagesize($photo);
    return (($width<=$maxWidth) && ($height<=$maxHeight)); 
}

my controller.php code is here

     $validator=Validator::make(Input::all(),array(
            'galname'=>'required|max:20',
            'galimg'=>'required|max:300kb|Mimes:jpeg,jpg,gif,png
              ,pneg' 
      ));

  if($validator->passes() && correct_size(Input::file('galimg')))
    { }

  if($validator->fails())
     {  
      return Redirect::route('getgallery')
      ->withErrors($validator)->withInput(); 
     }
  else
  { 
    $max_image = 3;
    if(ForumGallery::all()->count() < $max_image)
     {  
      $file=Input::file('galimg');
      $filename=$file->getClientOriginalName();
      $file->move('uploads',$filename);
      ForumGallery::create([
      'galname'=>Input::get('galname'),
       'galimg'=>$filename
       ]);
     return Redirect::route('addgallery'); 
      }

igot an error Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR) Call to undefined function correct_size().how to solve?

Iam reffer this "how to check image dimensions before upload in laravel 4"stack overflow question(how to check image dimensions before upload in laravel 4)

Upvotes: 0

Views: 8086

Answers (3)

Sanjaya
Sanjaya

Reputation: 195

You can do like this:

'galimg'=>'required|max:300kb|Mimes:jpeg,jpg,gif,png,svg| dimensions:width=200,height=50'

Upvotes: 1

TalESid
TalESid

Reputation: 2514

As you've mentioned that you got the following error

Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR) Call to undefined function correct_size().

Its transparent that the function correct_size($photo) isn't accessible here:

if ($validator->passes() && correct_size(Input::file('galimg')))
{ }

You should use $this keyword to access this class' method like:

$this->correct_size(Input::file('galimg'))

Upvotes: 1

Sharifur Robin
Sharifur Robin

Reputation: 325

 'galimg'=>'required|max:300kb|Mimes:jpeg,jpg,gif,png| dimensions:width=200,height=50'

Do Something like this

Instead of This

'galimg'=>'required|max:300kb|Mimes:jpeg,jpg,gif,png

Upvotes: 1

Related Questions