Another function for centered text string.
What it does: Generate a truecolor .png image of a text string. The image will be just large enough encompass the text and a 2 px border and the text will be centered in it.
It is called from any other page like so...
<img src="linkImg.php?text=php.net is great&border=out" /> // Text with #&+"'\<> will need to be escaped, but I've found spaces don't cause errors. I haven't tested this with any other languages.
The following code is the file named linkImg.php
The file was not designed to have anything else here with it, and the open and close php tags should the the very first and very last characters of the page respectively, as outside whitespace can be a little evil sometimes.
<?php
$text = $_GET['text'];
$border = $_GET['border'];
$font = "fontpath"; $fontsize = font size; $box = imagettfbbox ($fontsize, 0, $font, $text);
$textW = $box[2] - $box[0];
$textH= $box[3]-$box[5];
$paddingx = 10;
$paddingy = 10;
$width = $textW+$paddingx;
$height= $textH+$paddingy;
$textx = $paddingx/2;
$texty = $height - $paddingy/2;
$shadoffx = 1;
$shadoffy = 1;
$img = imagecreatetruecolor($width,$height);
$white = imagecolorallocate($img,255,255,255);
$black = imagecolorallocate($img,0,0,0);
$lightgrey = imagecolorallocate($img,200,200,200);
$grey = imagecolorallocate($img,100,100,100);
$bgcol = imagecolorallocate($img,192,213,196); $fgcol = imagecolorallocate($img,243,104,88); imagefill($img,0,0,$bgcol);
imagettftext($img, $fontsize, 0, $textx+$shadoffx, $texty+$shadoffy, $grey, $font, $text);
imagettftext($img, $fontsize, 0, $textx, $texty, $fgcol, $font, $text);
if ($border == "out")
{
imageline ($img,0,0,$width,0,$white);imageline ($img,0,0,0,$height,$white);
imageline ($img,1,1,$width,1,$lightgrey);imageline ($img,1,1,1,$height-1,$lightgrey);
imageline ($img,0,$height-1,$width-1,$height-1,$black);imageline ($img,$width-1,$height-1,$width-1,0,$black);
imageline ($img,2,$height-2,$width-2,$height-2,$grey);imageline ($img,$width-2,$height-2,$width-2,2,$grey);
}
if ($border == "flat")
{
imageline ($img,0,0,$width,0,$white);imageline ($img,0,0,0,$height,$white);
imageline ($img,1,1,$width,1,$grey);imageline ($img,1,1,1,$height-1,$grey);
imageline ($img,0,$height-1,$width-1,$height-1,$white);imageline ($img,$width-1,$height-1,$width-1,0,$white);
imageline ($img,2,$height-2,$width-2,$height-2,$grey);imageline ($img,$width-2,$height-2,$width-2,2,$grey);
}
if ($border == "in")
{
imageline ($img,0,0,$width,0,$black);imageline ($img,0,0,0,$height,$black);
imageline ($img,1,1,$width,1,$grey);imageline ($img,1,1,1,$height-1,$grey);
imageline ($img,0,$height-1,$width-1,$height-1,$white);imageline ($img,$width-1,$height-1,$width-1,0,$white);
imageline ($img,2,$height-2,$width-2,$height-2,$lightgrey);imageline ($img,$width-2,$height-2,$width-2,2,$lightgrey);
}
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>
Hope this helps someone!
-Patrick-