很多人不了解 php 可以产生非html的资料.这是对产生影像图非常有用的.可以从 database 产生一个简单的广告横图或更简单只产生一个图形按钮 .
我用 ttf 字型在以下的范例中
我通常取名作 'button.php3':
#######################################################
-----button.php3------
<?
header("content-type: image/gif");
if(!isset($s)) $s=11;
$size = imagettfbbox($s,0,"fonts/times.ttf",$text);
$dx = abs($size[2]-$size[0]);
$dy = abs($size[5]-$size[3]);
$xpad=9;
$ypad=9;
$im = imagecreate($dx+$xpad,$dy+$ypad);
$blue = imagecolorallocate($im, 0x2c,0x6d,0xaf);
$black = imagecolorallocate($im, 0,0,0);
$white = imagecolorallocate($im, 255,255,255);
imagerectangle($im,0,0,$dx+$xpad-1,$dy+$ypad-1,$black);
imagerectangle($im,0,0,$dx+$xpad,$dy+$ypad,$white);
imagettftext($im, $s, 0, (int)($xpad/2)+1, $dy+(int)($ypad/2), $black, "fonts/times.ttf", $text);
imagettftext($im, $s, 0, (int)($xpad/2), $dy+(int)($ypad/2)-1, $white, "fonts/times.ttf", $text);
imagegif($im);
imagedestroy($im);
?>
#######################################################
很重要一点是你不能在这档案中放任何html tags.也不能有空白行在 <?和 ?> tag 之前或之後. 如果你用这段script後看到一个不完整的影像, 表示你可能在php标签以外误打了字元.
以上的 script 可以由此语法在网页中叫出来: <img src=http://www.dvbbs.net/tech/php/"button.php3?s=36&text=php+is+cool">
| 对此文章发表了评论 |
