DwrAvatarBundle
This bundle provides easy image avatar generator support for Symfony2 and Symfony3.
Example of avatars:
plainAvatars:
profileAvatars:
In order to generate Avatar you can:
In Controller
public function indexAction()
{
$avatar = new AvatarFactory();
//for plainAvatar
$plainAvatar = $avatar->generate(new PlainAvatar(140, 140));
//for profileAvatar
$profileAvatar = $avatar->generate(new ProfileAvatar(140, 140));
return array(
'plainAvatar' => $plainAvatar->render()
'profileAvatar' => $profileAvatar->render()
);
}And in a view file (twig):
<img src="data:image/jpg;base64,{{ plainAvatar }}" >
<img src="data:image/jpg;base64,{{ profileAvatar }}" >Installation
Installation is a quick 3 steps process:
- Download DwrAvatarBundle using composer
- Enable the Bundle
- Add routing to routing.yml in order to can open example in your browser
Step 1: Download DwrAvatarBundle using composer
Add DwrAvatarBundle in version 2.0 (for Symfony 3) in your composer.json:
{
"require": {
"dwr/avatar-bundle": "2.0"
}
}Add DwrAvatarBundle in version 1.0 (for Symfony 2) your composer.json:
{
"require": {
"dwr/avatar-bundle": "1.0"
}
}Download the bundle by running the command:
$ php composer.phar require dwr/avatar-bundleComposer will install the bundle into your project's vendor/dwr/avatar-bundle directory.
Step 2: Enable the bundle
Enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Dwr\AvatarBundle\DwrAvatarBundle(),
);
}Step 3: Add routing to routing.yml in order to can open example in your browser
dwr_avatar:
resource: "@DwrAvatarBundle/Controller/"
type: annotation
prefix: /dwr_avatarbundleCongratulations! You're ready to generate avatars in your symfony application. Example how PlainAvatar looks like you can find on: yours-application-url/dwr_avatarbundle/avatar .
Usage
Generate avatar in base64 stream
Controller
public function indexAction()
{
$avatar = new AvatarFactory();
//for plainAvatar
$plainAvatar = $avatar->generate(new PlainAvatar(140, 140));
//for profileAvatar
$profileAvatar = $avatar->generate(new ProfileAvatar(140, 140));
return array(
'plainAvatar' => $plainAvatar->render()
'profileAvatar' => $profileAvatar->render()
);
}View (twig)
<img src="data:image/jpg;base64,{{ plainAvatar }}" >
<img src="data:image/jpg;base64,{{ profileAvatar }}" >Outputs
Example how to generate avatars in base64 stream is also stored in AvatarBundle/Controller/DefaultController.php.
Generate avatar and save it to file
public function indexAction()
{
$avatar = new AvatarFactory();
$plainAvatar = $avatar->generate(new PlainAvatar(140, 140));
return array(
'plainAvatar' => $plainAvatar->save('web/images/avatar/directory')
);
}Above example generates avatar file into directory. Directory name is passed in function save as parameter. File name will be generated automatically based on this:
$filename = date('YmdHis') . uniqid() . '.jpg';In order to generate avatar with your own filename you can pass filename (e.g image.jpg) as second argument of save method.
public function indexAction()
{
$avatar = new AvatarFactory();
$plainAvatar = $avatar->generate(new PlainAvatar(140, 140));
return array(
'plainAvatar' => $plainAvatar->save('web/images/avatar/directory', 'image.jpg')
);
}Change log
Please see CHANGELOG for more information on what has changed recently.
Troubleshooting
- Make sure you have GD installed on your web server. http://php.net/manual/en/function.gd-info.php
