forked from iamkannan/letterAvatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
letterAvatarClass.php
119 lines (105 loc) · 3.36 KB
/
letterAvatarClass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* letterAvatar class
* @category Class
* @package letterAvatar
* @author Kannan M
* @version 1.0 (Oct 03 - 2016)
*/
class letterAvatarClass
{
/**
* Function to generate letter avatar
* @access public
* @param Text, Font Size, Image width and height
* @return Image Url
* @author Kannan
*/
public function letterAvatar($text,$fontSize,$imgWidth,$imgHeight)
{
/* settings */
$font = './calibri.ttf'; /*define font*/
// Split words and get first letter of each word
// Example - Kannan m -> KM
$words = explode(" ", $text);
$text = "";
foreach ($words as $i => $w) {
$text .= mb_strtoupper(mb_substr($w, 0, 1, 'UTF-8'), 'UTF-8');
if (mb_strlen($text, 'UTF-8') == 2) {
break;
}
}
// Upload directory
$folder = 'avatars/';
// File name and extension
$fileName = md5($text).'.png';
// Text color
// Default - White
$textColor = '#FFF';
// Convert hex code to RGB
$textColor=$this->hexToRGB($textColor);
// check letter avatar already exist
// if exist return the image
if(file_exists($folder.$fileName))
{
return array('status' => TRUE,'image' => $folder.$fileName);
}
$im = imagecreatetruecolor($imgWidth, $imgHeight);
$textColor = imagecolorallocate($im, $textColor['r'],$textColor['g'],$textColor['b']);
// Random background Colors
$colorCode=array("#56aad8", "#61c4a8", "#d3ab92","#1abc9c", "#2ecc71", "#3498db", "#9b59b6", "#34495e", "#16a085", "#27ae60", "#2980b9", "#8e44ad", "#2c3e50", "#f1c40f", "#e67e22", "#e74c3c", "#f39c12", "#d35400", "#c0392b", "#7f8c8d");
$backgroundColor = $this->hexToRGB($colorCode[rand(0, count($colorCode)-1)]);
$backgroundColor = imagecolorallocate($im, $backgroundColor['r'],$backgroundColor['g'],$backgroundColor['b']);
imagefill($im,0,0,$backgroundColor);
list($x, $y) = $this->ImageTTFCenter($im, $text, $font, $fontSize);
imagettftext($im, $fontSize, 0, $x, $y, $textColor, $font, $text);
if(imagepng($im,$folder.$fileName)){
/*save image as PNG*/
return array('status' => TRUE,'image' => $folder.$fileName);
imagedestroy($im);
}
}
/**
* function to convert hex value to rgb array
* @access public
* @param Color
* @return hex value
* @author Kannan
*/
protected function hexToRGB($colour)
{
if ( $colour[0] == '#' ) {
$colour = substr( $colour, 1 );
}
if ( strlen( $colour ) == 6 ) {
list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] );
} elseif ( strlen( $colour ) == 3 ) {
list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] );
} else {
return false;
}
$r = hexdec( $r );
$g = hexdec( $g );
$b = hexdec( $b );
return array( 'r' => $r, 'g' => $g, 'b' => $b );
}
/**
* function to get center position on image
* @access public
* @param image,text,font,size,angle
* @return position
* @author Kannan
*/
protected function ImageTTFCenter($image, $text, $font, $size, $angle = 8)
{
$xi = imagesx($image);
$yi = imagesy($image);
$box = imagettfbbox($size, $angle, $font, $text);
$xr = abs(max($box[2], $box[4]))+5;
$yr = abs(max($box[5], $box[7]));
$x = intval(($xi - $xr) / 2);
$y = intval(($yi + $yr) / 2);
return array($x, $y);
}
}
?>