Text to image in PHP
Monday, October 15th, 2007want to show some cool font rendered as image to the client
here is sample code of how you can using GD in PHP
save the below code as text2image.php
==========text2image.php============
<?
$text=stripslashes($to_image);
$text=ucwords($text);
$temp_color=$color;
$r= hexdec( substr($temp_color,0,2));
$g=hexdec( substr($temp_color,2,2));
$b=hexdec( substr($temp_color,4,2));
header(”Content-type: image/png”);
$fonte=”path_to_font_dir/”.$font.”.ttf”;
$bbox = imagettfbbox($size,0,$fonte,$text);
$x_size = abs($bbox[4] - $bbox[0]);
$y_size = abs($bbox[5] - $bbox[1]);
$width=$x_size+5 ;
$im = imagecreatetruecolor($width, 30);
$white = imagecolorallocate($im, 255, 255, 255);
$red=imagecolorallocate($im,$r,$g,$b);
//$string = “This is some text..”;
imagealphablending($im,true);
imagefill($im,0,0,$white);
imagettftext($im,$size/75*72, 0, 5, 21, $red, $fonte, $text);
imagepng($im);
imagedestroy($im);
?>
not from any HTML page or PHP page simple
call
<img src=”text2image.php?font=SomeFontAvailableInServer&to_image=text to display&color=ff0000″ alt=”image rendered at server”>