Upload Resize image to the digital ocean using laravel livewire

public function update_user_profile(){      
        $this->validate([
            'profileToUpdate'=>'mimes:jpg,bmp,png,jpeg,JPG,BMP,PNG,JPEG',
        ]);
        try {        
            // create folder if not exists                
           if(! Storage::disk("public")->exists("user_profiles") ){
                Storage::disk("public")->makeDirectory("user_profiles");
           }
           // get name of uploaded image
            $uploaded_image_name = $this->profileToUpdate->hashName();
                        
            // Resize and save the uploaded image
            Image::make($this->profileToUpdate)
            ->resize(150, null, function ($constraint) {
                    $constraint->aspectRatio();
                })
            ->save(storage_path()."/app/public/user_profiles/".$uploaded_image_name);        

        // upload the resized image to the cloud                            
        $image_uploaded_at_cloud=Storage::disk('do')
        ->putFile('user_profiles',new File(storage_path("app/public/user_profiles/$uploaded_image_name")), 'public');            
        
        if(! is_null(Auth::user()?->user_detail?->profile_url)){
            // check for the old image that is exist or not in the cloud
            if(Storage::disk("do")->exists(Auth::user()?->user_detail?->profile_url)){
                Storage::disk("do")->delete(Auth::user()?->user_detail?->profile_url);
            }
        }

        // update the image details in the database 
        UserDetails::updateOrCreate(['user_id'=>$this->idToUpdateDetails],['profile_url'=>$image_uploaded_at_cloud]); 

        // Resized image is uploaded to the cloud, now delete the resize image from local
        Storage::disk("public")->delete("user_profiles/$uploaded_image_name");     

        } catch (\Throwable $th) {   
                    //  dd($th);
        }
    }
© 2016 - 2022, All Rights are Reserved.