before storing a file on my laravel app I want to proccess the image with Intervention package, and then upload it to /uploads folder inside my public disk however I'm getting the following error:
ERROR:Can't write image data to path (uploads)
My store method
$image = $request->file($name);
$extension = $image->guessExtension();
$resized = Image::make($image)->resize(640, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize(); })
->encode('jpg',75);
//->stream();
$file = $resized;
$filename = 'file-'.Carbon::now()->format('d-m-Y-H-i').'-'.Str::random(3).'-'.mt_rand('000','999').'.'.$extension;
$file->save('/uploads');
My Answer:
file('image');
$imageName = time() . '.' . $image->getClientOriginalExtension();
$destinationPath = public_path('/uploads');
$image->move($destinationPath, $imageName);
// Process the image with Intervention package
$img = Image::make($destinationPath . '/' . $imageName);
$img->resize(300, 200);
$img->save();
// Save the image path to the database
$imagePath = '/uploads/' . $imageName;
// Save $imagePath to the database
return redirect()->back()->with('success', 'Image uploaded successfully');
__The issue in your code is that you are trying to save the processed image using Intervention package before moving the original image to the destination path. You should first move the original image to the destination path and then process it with Intervention package.__
__Here is the corrected code:__
php
$image = $request->file('image');
$imageName = time() . '.' . $image->getClientOriginalExtension();
$destinationPath = public_path('/uploads');
$image->move($destinationPath, $imageName);
// Process the image with Intervention package
$img = Image::make($destinationPath . '/' . $imageName);
$img->resize(300, 200);
$img->save();
// Save the image path to the database
$imagePath = '/uploads/' . $imageName;
// Save $imagePath to the database
return redirect()->back()->with('success', 'Image uploaded successfully');
__Make sure that the `/uploads` folder exists in your `public` directory before running the code.__
Rate this post
4 of 5 based on 9429 votesComments