niiwill's avatar

Upload images using Intervention error code

I have upload file controller method and it is slow i dont know why? Can you review if my code is structured okay? Is it in right order? Can I do it better? How to optimize my code pls?

        $id = Auth::user()->id;
        $image=$request->file('file');
        $fileName=time().uniqid(rand()).'.'.$image->getClientOriginalExtension();

        //Resize image here    
        $background = Image::canvas(500, 500);  
        $background->fill('#fff');  

        $lphoto = Image::make($image->getRealpath());
        $lphoto->orientate();
        $lphoto->resize(500, 500, function ($constraint) {
            $constraint->upsize();
            $constraint->aspectRatio();
        });
        $lphoto->encode();

        $sphoto = Image::make($image->getRealpath());
        $sphoto->orientate();
        $sphoto->fit(186,180);
        $sphoto->encode(null, 90);

        $background->insert($lphoto, 'center');
        $background->encode();

        Storage::disk('public')->put( 'images/'.$year.'/'.$month."/".$day."/".$id."/".$fileName, $background);
        Storage::disk('public')->put( 'images/'.$year.'/'.$month."/".$day."/".$id.'/small-'.$fileName, $sphoto);
0 likes
0 replies

Please or to participate in this conversation.