PHP code example of naas / php-initial-avatar-generator

1. Go to this page and download the library: Download naas/php-initial-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/ */

    

naas / php-initial-avatar-generator example snippets


$avatar = new LasseRafn\InitialAvatarGenerator\InitialAvatar();

$image = $avatar->name('Lasse Rafn')->generate();

return $image->stream('png', 100);

$avatar = new LasseRafn\InitialAvatarGenerator\InitialAvatar();

echo $avatar->name('Lasse Rafn')->generateSvg()->toXMLString(); // returns SVG XML string

$image = $avatar->name('Albert Magnum')->generate();

$image = $avatar->autoFont()->generate();

// will be 96 pixels wide.
$image = $avatar->width(96)->generate();

// will be 96 pixels tall.
$image = $avatar->height(96)->generate();

// will be 96x96 pixels.
$image = $avatar->size(96)->generate();

// will be red
$image = $avatar->background('#ff0000')->generate();

// will be red
$image = $avatar->color('#ff0000')->generate();

// will be Semibold
$image = $avatar->font('/fonts/OpenSans-Semibold.ttf')->generate();

$image = $avatar->fontName('Arial, Helvetica, sans-serif')->generate();

$image = $avatar->name('John Doe Johnson')->length(3)->generate(); // 3 letters = JDJ

$image = $avatar->gd()->generate(); // Uses GD driver
$image = $avatar->imagick()->generate(); // Uses Imagick driver

$image = $avatar->rounded()->generate();

$image = $avatar->rounded()->smooth()->generate();

$image = $avatar->fontSize(0.25)->generate(); // Font will be 25% of image size.

return $avatar->name('Lasse Rafn')
              ->length(2)
              ->fontSize(0.5)
              ->size(96) // 48 * 2
              ->background('#8BC34A')
              ->color('#fff')
              ->generate()
              ->stream('png', 100);

// note that we
// 1) use glyph() instead of name
// 2) change the font to FontAwesome!
return $avatar->glyph('f007')
              ->font('/fonts/FontAwesome5Free-Regular-400.otf')
              ->color('#fff')
              ->background('#ff0000')
              ->generate()
              ->stream('png', 100);