Search code examples
phplaravellaravel-5intervention

get error while file upload in laravel


I am trying to upload image in laravel. But when i upload it gives error.

Call to a member function getClientOriginalExtension() on null

Here is my Blade file form

{{Form::open(array('url' => '/AddNewBlog','id'=>'blogadd' ,
   'method' => 'post','class'=>'form-row','files'=>true,
   "enctype"=>"multipart/form-data"))}}

And here is controller

 $imagename_bg = time() . '.' . $photo->getClientOriginalExtension();
 $destinationPath = public_path('/uploads/blog');
 $thumb_img = Image::make($photo->getRealPath())->resize(750, 450);
 $thumb_img->save($destinationPath . '/' . $imagename_bg, 80);
 $photo->move($destinationPath, $imagename_bg);

Please help me how to resolve this problem.


Solution

  • I am not able to understand your code. if you are looking for uploading image and resize using intervention package try this:-

    if($request->hasFile('blogimage')){
    if (Input::file('blogimage')->isValid()) {
        $file = Input::file('image');
        $img = Image::make($file);
        $img->resize(400,270);
        $name = pathinfo($_FILES['image']['name']);
        $destination = public_path('/uploads/blog');
        $ext = $name['extension'];
        $rand= time().str_random(6).date('h-i-s').'.'.$ext;
        $img->save($destination.$rand);
    }
    }
    

    Or Without intervention pacakge:-

    if($request->hasFile('blogimage')){
    if (Input::file('blogimage')->isValid()) {
        $file = Input::file('blogimage');
        $destination = public_path('/uploads/blog');
        $ext= Input::file('blogimage')->getClientOriginalExtension();
        $mainFilename =time().str_random(5).date('h-i-s');
        $file->move($destination, $mainFilename.".".$ext);
    }
    }
    

    Hope it helps!