【发布时间】:2010-12-16 19:43:15
【问题描述】:
我正在尝试通过为标题使用自定义字体来为我的网站增添趣味。对我来说,最合适的方法是使用 PHP 和 GD。我写了一个小脚本,它会根据 $_GET 值输出动态文本,但是有时图像太宽,这会移动其他所有内容。
如何让图像根据文本的宽度调整其宽度?这是我目前写的代码:
<?php
// Settings
$sText = $_GET['t']; // Text of heading
$sFont = "font/AvantGarde-Book.ttf"; // Default font for headings
$sMain = $_GET['c'] ? $_GET['c'] : 0xF2AB27; // Get a font or default it
// Create the image
header("content-type: image/png"); // Set the content-type
$hImage = imagecreatetruecolor(200, 24);
ImageFill($hImage, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($hImage, true);
imagealphablending($hImage, false);
imagettftext($hImage, 20, 0, 0, 24, $sMain, $sFont, $sText); // Draw the text
imagepng($hImage); // Generate the image
imagedestroy($hImage); // Destroy it from the cache ?>
谢谢!
【问题讨论】: