Gabotronix's avatar

Error when uploading image with laravel and intervention package

Hi everybody, before storing a file on my laravel app I want to proccess the image with Intervention package, and then upload it to my public disc using method storeAs as always, however I'm getting the following error:

Command (StoreAs) is not available for driver (Gd).

My upload method:

public function uploadAndProccess( $request, $name, $previousImage = false , $folder = 'uploads')
    {
        if ($request->hasFile($name))//&& $request->file($name)->isValid())
        {
            $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->storeAs('uploads', $filename);

            if($previous)
            {
                File::delete($filename);
            }

            return $folder.$filename;

        }
        else
        {
            return $previousImage;
        }
    }
0 likes
3 replies
Gabotronix's avatar

@tykus I changed it to :

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

But now I'm getting another error, I want to store in uploads folder inside my public folder!

Can't write image data to path (uploads)
tykus's avatar
tykus
Best Answer
Level 104

@Gabotronix what do you think the error message means? You are trying to write to an uploads directory in the root in the filesystem rather than the public directory! And you're not using the $filename you created!

Let's simplify this:

$extension = $image->guessExtension();

$resized = Image::make($request->file($name))->resize(640, null, function ($constraint) {
    $constraint->aspectRatio(); 
    $constraint->upsize();
})->encode('jpg',75);

$filename = 'file-'.Carbon::now()->format('d-m-Y-H-i').'-'.Str::random(3).'-'.mt_rand('000','999').'.'.jpg;
$resized->save(public_path("uploads/{$filename}"));

Please or to participate in this conversation.