2016-09-02 17:33:43 +02:00
|
|
|
<?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
|
2016-09-08 10:10:49 +02:00
|
|
|
private static $_FONT_PATH = '/api/templates/default/fonts/arial.ttf';
|
2016-09-02 17:33:43 +02:00
|
|
|
|
|
|
|
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],
|
2016-09-05 12:50:04 +02:00
|
|
|
* 'id' => [user id]
|
2016-09-02 17:33:43 +02:00
|
|
|
* )
|
|
|
|
* @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'] : '';
|
2016-09-05 12:50:04 +02:00
|
|
|
// id
|
|
|
|
$id = isset($_content['id'])? $_content['id']: '';
|
2016-09-02 17:33:43 +02:00
|
|
|
|
|
|
|
// Array of RGB color as background color
|
2016-09-05 12:50:04 +02:00
|
|
|
$bgcolor = $_color ? $_color : self::_getBgColor($firstname.$lastname.$id);
|
2016-09-02 17:33:43 +02:00
|
|
|
|
|
|
|
// Letters to be shown
|
2019-02-15 17:43:34 +01:00
|
|
|
$text = strtoupper(mb_substr($firstname, 0, 1).mb_substr($lastname, 0, 1));
|
2016-09-02 17:33:43 +02:00
|
|
|
|
|
|
|
//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;
|
|
|
|
|
2016-09-08 10:10:49 +02:00
|
|
|
$box = imagettfbbox($fontsize, 0, EGW_SERVER_ROOT.self::$_FONT_PATH, $text);
|
2016-09-02 17:33:43 +02:00
|
|
|
|
|
|
|
$x = floor(($_size - ($box[2] - $box[0]))/2);
|
|
|
|
|
|
|
|
$y = floor ($_size / self::$_TEXT_Y_RATIO);
|
|
|
|
|
|
|
|
// write a ttf text
|
2016-09-08 10:10:49 +02:00
|
|
|
$ttf = imagettftext($image, $fontsize, 0, $x, $y, $textcolor, EGW_SERVER_ROOT.self::$_FONT_PATH, $text);
|
2016-09-02 17:33:43 +02:00
|
|
|
if ($ttf)
|
|
|
|
{
|
|
|
|
ob_start();
|
2019-07-22 15:01:13 +02:00
|
|
|
imagejpeg($image, null,50);
|
2016-09-02 17:33:43 +02:00
|
|
|
$result = ob_get_contents();
|
|
|
|
ob_clean();
|
|
|
|
imagedestroy($image);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
imagedestroy($image);
|
|
|
|
}
|
|
|
|
}
|
2016-09-05 12:50:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to select a color code from background colors array
|
|
|
|
* base on given string phrase. (FirstName LastName Id)
|
|
|
|
*
|
|
|
|
* @param string $_str string to convert to a color code
|
|
|
|
*
|
|
|
|
* @return string color code
|
|
|
|
*/
|
|
|
|
private static function _getBgColor ($_str)
|
|
|
|
{
|
|
|
|
$hash = 0;
|
|
|
|
for ($i=0; $i< strlen($_str); $i++)
|
|
|
|
{
|
|
|
|
$hash = ord($_str[$i]) + $hash;
|
|
|
|
}
|
|
|
|
$index = $hash % count(self::$BG_COLORS);
|
|
|
|
return self::$BG_COLORS[$index];
|
|
|
|
}
|
2016-09-02 17:33:43 +02:00
|
|
|
}
|