如何检查laravel中图像的宽度和高度

时间:2015-02-17 10:20:08

标签: validation symfony laravel-4

我是laravel的新人。我想在插入数据库之前检查图像的宽度和高度。

我的Model文件夹包含ForumGallery文件代码

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

我的controller.php代码在这里

     $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错误Symfony \ Component \ Debug \ Exception \ FatalErrorException(E_ERROR) 调用未定义的函数correct_size()。如何解决?

Iam提到这个&#34;如何在上传之前检查图像尺寸4和#34;堆栈溢出问题(how to check image dimensions before upload in laravel 4

3 个答案:

答案 0 :(得分:0)

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

做这样的事

而不是这个

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

答案 1 :(得分:0)

正如您所提到的,您遇到以下错误

  

Symfony \组件\调试\异常\ FatalErrorException(E_ERROR)调用未定义的函数correct_size()。

透明的是,无法在此处访问函数correct_size($photo)

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

您应该使用$this关键字来访问此类的方法,例如:

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

答案 2 :(得分:0)

您可以这样做:

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