WIP of letter avatar generator

This commit is contained in:
Hadi Nategh 2016-09-02 17:33:43 +02:00
parent 064b51bab2
commit c3583dbb3c
2 changed files with 119 additions and 0 deletions

119
api/src/avatar.php Normal file
View File

@ -0,0 +1,119 @@
<?php
/**
* EGroupware API: Creating letter avatar
*
* @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage avatar
* @author Hadi Nategh <hn@egroupware.de>
* @version $Id$
*/
namespace EGroupware\Api;
/**
* Creating letter avatar
*
* lavatar class designed for creating avatar image from given
* user firstname.lastname and outputs them as in image consist of
* first letter of the firstname plus first letter of the lastname.
*/
class avatar
{
// background colors
private static $BG_COLORS = array(
array(90,135,112), array(178,183,187), array(111,169,171), array(245,175,41),
array(0,136,185), array(241,134,54), array(217,58,55), array(166,177,46),
array(0,136,185), array(241,134,54), array(217,58,55), array(166,177,46),
array(92,155,188), array(245,136,141), array(154,137,181), array(64,120,135),
array(154,137,181),array(90,135,112), array(211,63,51), array(162,176,31),
array(240,177,38),array(0,135,191), array(241,134,54), array(0,135,191),
array(178,183,187),array(114,172,174), array(156,138,180), array(90,135,112),
array(238,180,36),array(64,120,135)
);
// ratio of font size to size
private static $_FONT_SIZE_RATIO = 3.2;
// ratio of text Y position to size
private static $_TEXT_Y_RATIO = 1.6;
private static $_TEXT_COLOR = array (255, 255, 255);
// font path
private static $_FONT_PATH = EGW_SERVER_ROOT.'/api/templates/default/fonts/arial.ttf';
public function __construct() {
}
/**
* Function to generate letter avatar image out of given
* user firstname and lastname.
*
* @param array $_content = null an array of firstname lastname
* example:
* array (
* 'firstname' => [userFirstname],
* 'lastname' => [userLastname],
* )
* @param array $_color = null an array of RGB color, default
* is nul to get a random color from color library.
* example: white color
* array (
* 0, // RED
* 0, // GREEN
* 0 // BLUE
* )
* @param int $_size = 128 image size, default size is 128
*/
public static function lavatar ($_content = null, $_color = null, $_size = 128)
{
// firstname
$firstname = isset($_content['firstname'])? $_content['firstname'] : '';
//lastname
$lastname = isset($_content['lastname'])? $_content['lastname'] : '';
$bg_index = rand(0, count(self::$BG_COLORS));
// Array of RGB color as background color
$bgcolor = $_color ? $_color : self::$BG_COLORS[$bg_index];
// Letters to be shown
$text = $firstname[0].$lastname[0];
//create an image
$image = imagecreatetruecolor($_size, $_size);
// allocate an image color as background color
$bg = imagecolorallocate($image, $bgcolor[0], $bgcolor[1], $bgcolor[2]);
// allocate an image color as textcolor
$textcolor = imagecolorallocate($image, self::$_TEXT_COLOR[0],self::$_TEXT_COLOR[1],self::$_TEXT_COLOR[2]);
// create a rectangle
imagefilledrectangle($image, 0, 0, $_size, $_size, $bg);
$fontsize = $_size / self::$_FONT_SIZE_RATIO;
$box = imagettfbbox($fontsize, 0, self::$_FONT_PATH, $text);
$x = floor(($_size - ($box[2] - $box[0]))/2);
$y = floor ($_size / self::$_TEXT_Y_RATIO);
// write a ttf text
$ttf = imagettftext($image, $fontsize, 0, $x, $y, $textcolor, self::$_FONT_PATH, $text);
if ($ttf)
{
ob_start();
imagejpeg($image, null,100);
$result = ob_get_contents();
ob_clean();
imagedestroy($image);
return $result;
}
else
{
imagedestroy($image);
}
}
}

Binary file not shown.