Search code examples
phplaravelfile-uploadlaravel-5intervention

Can't write image data to path. Intervention Image Laravel 5.2


   public function newItem(Request $request){

        $image = $request->file('image');
        $img = time().'.'.$image->getClientOriginalExtension();
        $watermark = Image::make('images/watermark.png');
        $destinationPath = public_path('/products');
        $img = Image::make($image->getRealPath());
        $img->resize(300, 365, function ($constraint) {
            $constraint->aspectRatio();
        })->insert($watermark, 'center');
        File::exists($destinationPath) or File::makeDirectory($destinationPath);
        $img->save($destinationPath.'/'.$img);

}

I keep getting Can't write image data to path Can anyone figure out what I'm doing wrong? The question might seem duplicate, but other suggestions in similar questions did not work for me.

Thanks in advance


Solution

  • For the sake of others that might have the same issue. This is how I solved it:

     $image = $request->file('image');
        $img = time().'.'.$image->getClientOriginalExtension();
    
    $watermark = Image::make('images/watermark.png');
    $destinationPath = public_path('/products');
    Image::make($image->getRealPath())->resize(300, 365, function ($constraint) {
        $constraint->aspectRatio();
    })->insert($watermark, 'center')->save($destinationPath.'/'.$img);
    

    The mistake I was making was assigning Image::make() to a variable. You can look at my code here and the one above in my question.