Saturday, February 19, 2022

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

Issue

   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.



Answered By - Michel

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.