Skip to content

Instantly share code, notes, and snippets.

@kaixiong
Created September 12, 2013 19:02
Show Gist options
  • Save kaixiong/6542314 to your computer and use it in GitHub Desktop.
Save kaixiong/6542314 to your computer and use it in GitHub Desktop.
<?php
// Usage: icon_letters.php?FN=symbol&L=m&L_B=250&L_R=00&L_G=150&BG_B=100&BG_R=150&BG_G=250
// Usage: icon_letters.php?FN=kaiu&dx=-3&dy=0&BG_B=100&BG_R=100&L=陈
$width_height = 320;
//Send a generated image to the browser
$LETTERS_MAX_LEN = 3;
$letters = "SDP";
if (isset($_GET['L'])) {
$letters = $_GET['L'];
if (strlen($letters) == 0) {
$letters = "SDP";
}
}
if (strlen($letters) > $LETTERS_MAX_LEN) {
$letters = substr($letters, 0, $LETTERS_MAX_LEN);
}
# detect if the string was passed in as unicode
# make sure it's in unicode
if (mb_detect_encoding($letters, 'UTF-8, ISO-8859-1') != 'UTF-8') {
$letters = mb_convert_encoding($letters, 'UTF-8', $text_encoding);
}
# html numerically-escape everything (&#[dec];)
$letters = mb_encode_numericentity($letters, array (0x0, 0xffff, 0, 0xffff), 'UTF-8');
$avail_fonts = array("arial", "arialb", "ariali", "cour", "courb", "georgia", "georgiab", "georgiai", "tahoma", "tahomab", "verdana", "verdanai", "verdanab", "symbol", "kaiu");
$font = "arial";
if (isset($_GET['FN'])) {
$font = $_GET['FN'];
if (!in_array($font, $avail_fonts)) {
$font = "arial";
}
}
$letter_R = 1;
if (isset($_GET['L_R'])) {
$letter_R = intval($_GET['L_R']);
if ($letter_R > 255) {
$letter_R = 255;
}
if ($letter_R < 1) {
$letter_R = 1;
}
}
$letter_G = 1;
if (isset($_GET['L_G'])) {
$letter_G = intval($_GET['L_G']);
if ($letter_G > 255) {
$letter_G = 255;
}
if ($letter_G < 1) {
$letter_G = 1;
}
}
$letter_B = 1;
if (isset($_GET['L_B'])) {
$letter_B = intval($_GET['L_B']);
if ($letter_B > 255) {
$letter_B = 255;
}
if ($letter_B < 1) {
$letter_B = 1;
}
}
$BG_R = 255;
if (isset($_GET['BG_R'])) {
$BG_R = intval($_GET['BG_R']);
if ($BG_R > 255) {
$BG_R = 255;
}
if ($BG_R < 0) {
$BG_R = 0;
}
}
$BG_G = 10;
if (isset($_GET['BG_G'])) {
$BG_G = intval($_GET['BG_G']);
if ($BG_G > 255) {
$BG_G = 255;
}
if ($BG_G < 0) {
$BG_G = 0;
}
}
$BG_B = 10;
if (isset($_GET['BG_B'])) {
$BG_B = intval($_GET['BG_B']);
if ($BG_B > 255) {
$BG_B = 255;
}
if ($BG_B < 0) {
$BG_B = 0;
}
}
$dx = 0;
if (isset($_GET['dx'])) {
$dx = (int)($_GET['dx']);
}
$dy = 0;
if (isset($_GET['dy'])) {
$dy = (int)($_GET['dy']);
}
create_image($width_height, $letters, $font, $dx, $dy, $letter_R, $letter_G, $letter_B, $BG_R, $BG_G, $BG_B);
exit();
function create_image($width_height, $letters, $font, $dx, $dy, $letter_R, $letter_G, $letter_B, $BG_R, $BG_G, $BG_B)
{
//Set the image width and height
$border_radius = floor($width_height/9);
$inner_border_radius = ceil($width_height/8);
$fontfile = "./fonts/".$font.".ttf";
//Create the image resource
$image = ImageCreateTrueColor($width_height, $width_height);
//We are making three colors, white, black and gray
$transparent = ImageColorAllocateAlpha($image, 0, 0, 0, 127);
$letter_color = ImageColorAllocateAlpha($image, $letter_R, $letter_G, $letter_B, 15);
$BG_color = ImageColorAllocateAlpha($image, $BG_R, $BG_G, $BG_B, 30);
// draw corners
filled_rounded_rect($image, 0, 0, $width_height, $width_height, $border_radius, $BG_color);
$ok = false;
$fontsize = $width_height - 2*$inner_border_radius;
$text_position_x = 0;
$text_position_y = 0;
while (!$ok) {
$ok = true;
$curr_ttf_box = imagettfbbox( $fontsize, 0, $fontfile, $letters );
// key contents
// 0 lower left corner, X position
// 1 lower left corner, Y position
// 2 lower right corner, X position
// 3 lower right corner, Y position
// 4 upper right corner, X position
// 5 upper right corner, Y position
// 6 upper left corner, X position
// 7 upper left corner, Y position
if ( (($curr_ttf_box[2] - $curr_ttf_box[0]) > $width_height - 2*$inner_border_radius) || (($curr_ttf_box[1] - $curr_ttf_box[7]) > $width_height - 2*$inner_border_radius) )
{
$ok = false;
$fontsize = $fontsize - 1;
}
else
{
$text_position_x = floor($inner_border_radius + (($width_height - 2*$inner_border_radius)-($curr_ttf_box[2] - $curr_ttf_box[0]))/2);
$text_position_y = floor($inner_border_radius - $curr_ttf_box[7] + (($width_height - 2*$inner_border_radius)-($curr_ttf_box[1] - $curr_ttf_box[7]))/2);
// imageline($image, $text_position_x, $text_position_y, $text_position_x+($curr_ttf_box[2] - $curr_ttf_box[0]), $text_position_y - ($curr_ttf_box[1] - $curr_ttf_box[7]), $letter_color );
// imageline($image, $text_position_x, $text_position_y - ($curr_ttf_box[1] - $curr_ttf_box[7]), $text_position_x+($curr_ttf_box[2] - $curr_ttf_box[0]), $text_position_y, $letter_color );
// imageline($image, $text_position_x, $text_position_y, $text_position_x, $text_position_y - ($curr_ttf_box[1] - $curr_ttf_box[7]), $letter_color );
// imageline($image, $text_position_x, $text_position_y, $text_position_x+($curr_ttf_box[2] - $curr_ttf_box[0]), $text_position_y, $letter_color );
// imageline($image, $text_position_x+($curr_ttf_box[2] - $curr_ttf_box[0]), $text_position_y, $text_position_x+($curr_ttf_box[2] - $curr_ttf_box[0]), $text_position_y - ($curr_ttf_box[1] - $curr_ttf_box[7]), $letter_color );
// imageline($image, $text_position_x, $text_position_y - ($curr_ttf_box[1] - $curr_ttf_box[7]), $text_position_x+($curr_ttf_box[2] - $curr_ttf_box[0]), $text_position_y - ($curr_ttf_box[1] - $curr_ttf_box[7]), $letter_color );
}
}
//Add string to the image
ImageTTFText($image, $fontsize, 0, $text_position_x + $dx, $text_position_y - $dy, $letter_color, $fontfile, $letters);
ImageAlphaBlending($image, false);
ImageSaveAlpha($image, true);
//Tell the browser what kind of file is come in
header("Content-Type: image/png");
//Output the newly created image in png format
ImagePng($image);
//Free up resources
ImageDestroy($image);
}
function filled_rounded_rect($image, $x0, $y0, $width, $height, $corner, $color)
{
$x1 = $x0 + $width - 1;
$y1 = $y0 + $height - 1;
// Approach #1: Draws border then flood fill
/* imagearc($image, $x0+$corner-1, $y0+$corner-1, $corner*2, $corner*2, 180, 270, $color); */
/* imagearc($image, $x1-$corner+1, $y0+$corner-1, $corner*2, $corner*2, 270, 0, $color); */
/* imagearc($image, $x0+$corner-1, $y1-$corner+1, $corner*2, $corner*2, 90, 180, $color); */
/* imagearc($image, $x1-$corner+1, $y1-$corner+1, $corner*2, $corner*2, 0, 90, $color); */
/* imageline($image, $x0+$corner, $y0, $x1-$corner, $y0, $color); */
/* imageline($image, $x0+$corner, $y1, $x1-$corner, $y1, $color); */
/* imageline($image, $x0, $y0+$corner, $x0, $y1-$corner, $color); */
/* imageline($image, $x1, $y0+$corner, $x1, $y1-$corner, $color); */
/* imagefilltoborder($image, ($x0+$x1)/2, ($y0+$y1)/2, $color, $color); */
// Approach #2: Draws 4 filled arcs, followed by 3 filled rectangles
imagefilledarc($image, $x0+$corner-1, $y0+$corner-1, $corner*2, $corner*2, 180, 270, $color, IMG_ARC_PIE);
imagefilledarc($image, $x1-$corner+1, $y0+$corner-1, $corner*2, $corner*2, 270, 0, $color, IMG_ARC_PIE);
imagefilledarc($image, $x0+$corner-1, $y1-$corner+1, $corner*2, $corner*2, 90, 180, $color, IMG_ARC_PIE);
imagefilledarc($image, $x1-$corner+1, $y1-$corner+1, $corner*2, $corner*2, 0, 90, $color, IMG_ARC_PIE);
imagefilledrectangle($image, $x0, $y0+$corner, $x1, $y1-$corner, $color);
imagefilledrectangle($image, $x0+$corner-1, $y0, $x1-$corner+1, $y0+$corner-1, $color);
imagefilledrectangle($image, $x0+$corner-1, $y1-$corner+1, $x1-$corner+1, $y1, $color);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment