PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 19, 2022

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

 February 19, 2022     file-upload, intervention, laravel, laravel-5.2, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing