Code Dump: Icongen

Here’s a quick and dirty php script (using GD) that creates icons for my android phone. I got fed up at SimpleText, which no longer creates transparent images, and appears to have been abandoned by its developer. Images are created from the URL

icongen.php?name=ICON

into a folder called images.

$img_width=128;
$img_height=128;
$handle = ImageCreatetruecolor ($img_width,$img_height) or die ("Cannot Create image");
$bg_color = ImageColorAllocate ($handle, 0,0,0);
$black = ImageColorAllocate ($handle, 0,0,0);
$white = ImageColorAllocate ($handle, 255,255,255);
$red = ImageColorAllocate ($handle, 255,0,0);
$blue = ImageColorAllocate ($handle, 50,50,150);
$txt_color = $black;
imagecolortransparent($handle,$bg_color);
$font_size = 26;
$font_path='./OpenSans-Regular.ttf';
$txt_max_width = intval(0.8 * $img_width);
if($_GET['name']) { $text=$_GET['name'];
} else { $text='snap';
} #do { #$font_size++;
$p = imagettfbbox($font_size,0,$font_path,$text);
$txt_width=$p[2]-$p[0];
#} while ($txt_width <= $txt_max_width); $y = $img_height * 0.57; // baseline of text at 90% of $img_height $x = ($img_width - $txt_width) / 2; imagettftext($handle, $font_size, 0,$x, $y, $white, $font_path, $text); ImagePng ($handle,'images/'.$text.'.png'); imagedestroy($handle); ?>.png>