< Back to Code Samples
<?php
/* Hacked from a sample script on PHP.net /
 *************************************************
 * getText - a PHP textual image producer
 * PURPOSE: produces PNG text images on the fly by
 *         sourcing from a font.
 * USAGE: <img src="getText.php?text=YourText" alt="the Alt Text" />
 * OPTIONAL PARAMETERS (GET parameters)
 *        size : (default: 20) - the size of the text
 *        text : (default: "just Testing") - the text to show
 *        drop : (default: none) integer value representing the offset for a drop shadow
 *        color: (default: 000000 [black]) the hex value for the foreground color (no prefix)
 *        back : (default: FFFFFF [white]) the hex value for the background color (no prefix)
 **********************************************  */

define('FONT','../lte50543.ttf');
// Set the content-type
header("Content-type: image/png");
// Prevent caching
if (isset($_GET['nocache'])) {
    
header("Cache-Control: no-cache, must-revalidate");
    
header("Expires: Mon, 11 April 2007 3:19:00 GMT");
}

// Get optional parameters
$size = isset($_GET['size']) ? $_GET['size'] : "20";
$text = isset($_GET['text']) ? $_GET['text'] : 'just Testing';
$drop = isset($_GET['drop']) ? $_GET['drop'] : NULL;
$color = isset($_GET['color']) ? 0xFFFFFF hexdec($_GET['color']) : 0x000000;
$back = isset($_GET['back']) ? 0xFFFFFF hexdec($_GET['back']) : 0xFFFFFF;

$bbox imagettfbbox($size0FONT$text);

$imgHeight abs($bbox[5]) + abs($bbox[1])+($size/2) + $drop;
$imgWidth abs($bbox[2]) + abs($bbox[0])+$drop;

// Create the image
$im imagecreatetruecolor($imgWidth$imgHeight);

// Create some colors
$foreground imagecolorallocate($im, ($color 0xFF0000) >> 16, ($color 0x00FF00) >> 8, ($color 0x0000FF));
$dropshadow imagecolorallocate($immax(0,(($back 0xFF0000) >> 16) - 0x30), max(0,(($back 0x00FF00) >> 8) - 0x30), max(0,(($back 0x0000FF) - 0x30)));
$background imagecolorallocate($im, ($back 0xFF0000) >> 16, ($back 0x00FF00) >> 8, ($back 0x0000FF));
imagefilledrectangle($im00$imgWidth-1$imgHeight-1$background);

// Add some shadow to the text
if ($drop) {
    
imagettftext($im$size0$drop$imgHeight-($size/2)+$drop$dropshadowFONT$text);
}

// Add the text
imagettftext($im$size00$imgHeight-($size/2), $foregroundFONT$text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);

?>