Michel
Michel

Reputation: 1165

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

Upvotes: 3

Views: 6437

Answers (4)

Max Lyu
Max Lyu

Reputation: 39

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);

Upvotes: 0

b8x
b8x

Reputation: 88

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

Upvotes: 0

Michel
Michel

Reputation: 1165

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.

Upvotes: 1

Nour
Nour

Reputation: 1497

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);
}

Upvotes: 0

Related Questions