Package Data | |
---|---|
Maintainer Username: | artdarek |
Maintainer Contact: | artdarek@gmail.com (Dariusz Prząda) |
Package Create Date: | 2015-09-22 |
Package Last Update: | 2017-02-21 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-11 15:12:51 |
Package Statistics | |
---|---|
Total Downloads: | 13,980 |
Monthly Downloads: | 562 |
Daily Downloads: | 28 |
Total Stars: | 10 |
Total Watchers: | 1 |
Total Forks: | 1 |
Total Open Issues: | 0 |
Easy get avatars from social services like Facebook.com and Gravatar.com
A Provider can be a social network service that allows to get it's users avatars. As for right now we support:
Use composer require command to install this package.
$ composer require artdarek/avatarer
Add package to your composer.json file:
"require": {
"artdarek/avatarer": "2.0.*"
}
Use composer update command to install this package.
$ composer update
Add the Avatarer alias into your config file config/app.php
:
'aliases' => [
'Avatarer' => Artdarek\Avatarer\Support\Laravel\Facades\Avatarer::class,
],
To ininitialize Avatarer call make() method and pass Provider name (service name like Gravatar/Facebook/Twitter):
<?php
use \Artdarek\Avatarer\Avatar\Provider\Gravatar;
...
// create avatarer object using Gravatar provider
$avatar = Avatarer::make(Gravatar::class);
?>
<?php
use \Artdarek\Avatarer\Avatar\Provider\Facebook;
...
// create avatarer object using Facebook provider
$avatar = Avatarer::make(Facebook::class);
?>
<?php
use \Artdarek\Avatarer\Avatar\Provider\Twitter;
...
// create avatarer object using Gravatar provider
$avatar = Avatarer::make(Twitter::class);
?>
If you want to use Avatarer library outside of Laravel framework or you
don't want to use Avatarer Facade
in your Laravel application, you can do this:
<?php
use \Artdarek\Avatarer\Avatar\Provider\Gravatar;
...
// create avatarer object
$avatar = new \Artdarek\Avatarer\Avatarer;
$avatar->make(Gravatar::class);
?>
or
<?php
use \Artdarek\Avatarer\Avatar\Provider\Gravatar;
...
// create avatarer object
$avatar = (new \Artdarek\Avatarer\Avatarer)->make(Gravatar::class);
?>
or
<?php
use \Artdarek\Avatarer\Avatarer;
use \Artdarek\Avatarer\Avatar\Provider\Gravatar;
...
// create avatarer object
$avatar = (new Avatarer)->make(Gravatar::class);
?>
Generating avatar with default settings is very simple and all you have to do is to call
user()
method with user id as a parameter (each Provider can identify user differently
via email/id/screenname etc.):
For Gravatar we use Email:
<?php
use \Artdarek\Avatarer\Avatar\Provider\Gravatar;
...
// user email
$email = "example@user.email";
// create avatarer object
$avatar = Avatarer::make(Gravatar::class);
$avatar->user( $email );
// get url
$url = $avatar->get();
?>
For Facebook we use UserID:
<?php
use \Artdarek\Avatarer\Avatar\Provider\Facebook;
...
// user id
$userID = "838979896180389";
// create avatarer object
$avatar = Avatarer::make(Facebook::class);
$avatar->user( $userID );
// get url
$url = $avatar->get();
?>
For Twitter we use user ScreenName:
<?php
use \Artdarek\Avatarer\Avatar\Provider\Twitter;
...
// user id
$userScreenName = "artdarek";
// create avatarer object
$avatar = Avatarer::make(Twitter::class);
$avatar->user( $userScreenName );
// get url
$url = $avatar->get();
?>
If you want to set a size of avatar that should be returned you can use size()
method.
This method can take two parameters width
and height
and both are optional
(notice that not all providers will require or use both width and height, for example
Gravatar expects only width).
For Gravatar:
<?php
// create avatarer object
$avatar = Avatarer::make(Gravatar::class);
$avatar->user( $email );
$avatar->size( 200 );
// get url
$url = $avatar->get();
?>
For Facebook:
<?php
// create avatarer object
$avatar = Avatarer::make(Facebook::class);
$avatar->user( $userID );
$avatar->size( 200, 200 );
// get url
$url = $avatar->get();
?>
If you want to customize avatar a little bit more you can set some additional parameters
using options()
method.
For Gravatar:
<?php
// create avatarer object
$avatar = Avatarer::make(Gravatar::class);
$avatar->user( $email );
$avatar->size( 200 );
$avatar->options([
'default' => 'mm', // Url to your default avatar image or [ 404 | mm | identicon | monsterid | wavatar | blank | retro ]
'forceDefault' => null, // If for some reason you wanted to force the default image to always load [ y ]
'ratings' => 'g', // Maximum rating (inclusive) [ g | pg | r | x ]
]);
// get url
$url = $avatar->get();
?>
For Facebook:
<?php
$avatar = Avatarer::make(Facebook::class);
$avatar->user( $userID );
$avatar->size( 200, 200 );
$avatar->options([
'type' => 'square', // Type of avatar [ small, normal, album, large, square ]
]);
$url = $avatar->get();
?>
If thats more convinient for you you can chain all methods like below:
<?php
$url = Avatarer::make(Gravatar::class)
->user( $email )
->size(220)
->options([
'default' => 'mm'
'ratings' => 'g'
])->get();
?>
With Avatarer by using get()
method you can get url string of user avatar:
<?php
$avatar = Avatarer::make(Gravatar::class)->user( $email )->size('200');
$url = $avatar->get();
?>
If you wish to get output in different format like Array, Json or even HTML code
you can do that by calling get()
method with Output object that implements
OutputInterface as a parameter:
To get Array (add use \Artdarek\Avatarer\Output\ToArray;
at the top of your class):
<?php
use \Artdarek\Avatarer\Avatar\Provider\Gravatar;
use \Artdarek\Avatarer\Output\ToArray;
...
$avatar = Avatarer::make(Gravatar::cass)->user( $email )->size('200');
$url = $avatar->get(new ToArray);
?>
To get JSON (add use \Artdarek\Avatarer\Output\ToJson;
at the top of your class):
<?php
use \Artdarek\Avatarer\Avatar\Provider\Gravatar;
use \Artdarek\Avatarer\Output\ToJson;
...
$avatar = Avatarer::make(Gravatar::cass)->user( $email )->size('200');
$url = $avatar->get(new ToJson);
?>
To get Object (add use \Artdarek\Avatarer\Output\ToObject;
at the top of your class):
<?php
use \Artdarek\Avatarer\Avatar\Provider\Gravatar;
use \Artdarek\Avatarer\Output\ToObject;
...
$avatar = Avatarer::make(Gravatar::cass)->user( $email )->size('200');
$url = $avatar->get(new ToObject);
?>
To get HTML (add use \Artdarek\Avatarer\Output\ToHtml;
at the top of your class):
<?php
use \Artdarek\Avatarer\Avatar\Provider\Gravatar;
use \Artdarek\Avatarer\Output\ToHtml;
...
$avatar = Avatarer::make(Gravatar::cass)->user( $email )->size('200');
$url = $avatar->get(new ToHtml);
?>
If you want to have more controll over returned HTML code you can pass array with
additional attributes via ToHtml()
constructor, for examle:
<?php
use \Artdarek\Avatarer\Avatar\Provider\Gravatar;
use \Artdarek\Avatarer\Output\ToHtml;
...
$avatar = Avatarer::make(Gravatar::cass)->user( $email )->size('200');
$url = $avatar->get(
new ToHtml([
'class' => 'avatar',
'id' => 'user123'
])
);
?>