3
   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

Michel
  • 1,065
  • 1
  • 10
  • 25

4 Answers4

1

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.

Michel
  • 1,065
  • 1
  • 10
  • 25
0

Try this code it's worked for me

public function newItem(){

    $image = Input::file('image');
    $destinationPath = '/products';
    $img = time().'.'.$image->getClientOriginalExtension();
    $watermark = Image::make('images/watermark.png');
    $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);
}
Nour
  • 1,458
  • 1
  • 18
  • 27
  • I'm using 5.2, Input class is no longer supported. – Michel Sep 18 '16 at 17:12
  • It is still supported I am also having a project written by 5.2 and it's working for me – Nour Sep 18 '16 at 17:15
  • Not sure if `Input` works with 5.2, but the problem is with Intervention image. Using Laravel inbuilt file upload works perfectly. `$request->file('image')->move($destinationPath, $img);` – Michel Sep 18 '16 at 17:26
0

Make sure to create mentioned path folders (passing with image save) in laravel public folder. This will work automatically.

b8x
  • 88
  • 1
  • 10
0

If somebody use File Facade:

    File::exists($destinationPath) or File::makeDirectory($destinationPath);

You have to remember that if your $destinationPath contains more than 1 folder, you have to set 2nd & 3rd parameters like $mode & $recursive to create final destination folder and prepare directory for file upload.

Example:

    File::exists($imagePath) or File::makeDirectory($imagePath, 777, true);
Max Lyu
  • 39
  • 2