PHP code example of sowrensen / svg-avatar-generator
1. Go to this page and download the library: Download sowrensen/svg-avatar-generator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sowrensen / svg-avatar-generator example snippets
use Illuminate\Database\Eloquent\Model;
use \Illuminate\Database\Eloquent\Casts\Attribute;
class User extends Model
{
//...
public function profilePhoto(): Attribute
{
return Attribute::get(function ($value, $attributes) {
// If profile photo is set, return the original
if (! is_null($attributes['profile_photo'])) {
return $attributes['profile_photo'];
}
// Else, generate one
return \Svg::for($attributes['name'])->toUrl();
});
}
}
use Sowren\SvgAvatarGenerator\Facades\Svg;
use Sowren\SvgAvatarGenerator\Enums\FontWeight;
Svg::for('John Doe')
->asCircle() // or, asRectangle() along with optional setCornerRadius($radius) method
->setSize(64)
->setCustomFontUrl('https://api.fontshare.com/v2/css?f[]=kola@400&display=swap')
->setFontFamily('Kola')
->setFontSize(40)
->setFontWeight(FontWeight::SEMIBOLD)
->setForeground('#FFFFFF')
->setGradientColors( // set of 3 different gradients
['#4158D0', '#C850C0', '#FFCC70'],
['#00DBDE', '#FC00FF'],
['#FF9A8B', '#FF6A88', '#FF99AC']
)
->setGradientStops(0, .5, 1)
->setGradientRotation(120)
->render();
namespace App\Extractors;
use Sowren\SvgAvatarGenerator\Extractors\Extractor;
class CustomExtractor implements Extractor
{
public function extract(string $text): string
{
// Write your custom logic and return initials
}
}