laravelavatar

Trying to Generate profile image using Spatie media library


I want to generate a dummy or fake profile picture for my user, I try Spatie image generating tool, but its not working, even I didn't get any error or data in my phpMyAdmin. Would you help me

$user
    ->addMediaConversion('thumb')
    ->width(70)
    ->height(70)
    ->performOnCollections('avatar');

On the other side I try to save the image generated by an API of ui-avatars.com,

https://ui-avatars.com/api/?background=0D8ABC&color=fff&name=Jhon+Roe&rounded=true

but don't know how to save image from URL using Spatie media library.

Thanks in advance for your suggestions


Solution

  • You can add Media to your Model from url with

    $yourModel
        ->addMediaFromUrl($url) 
        ->preservingOriginal() //middle method
        ->toMediaCollection('avatar'); //finishing method
    

    Media Library Doc