基于文本PHP的动态图像

发布于 2024-09-30 18:07:54 字数 1019 浏览 1 评论 0原文

我想使用 GD 生成精美的字体文本图像。

我发现:

<?php
// Settings
$sText = 'This is just a test!'.@$_GET['t']; // Text of heading
$sFont = 'Bryant-Bold.ttf'; // Default font for headings
$sMain = @$_GET['c'] ? $_GET['c'] : 0xF2AB27; // Get a font or default it

// Calcuate the width of the image
$arSize = imagettfbbox(24, 0, $sFont, $sText);
$iWidth = abs($arSize[2] - $arSize[0]);
$iHeight = abs($arSize[7] - $arSize[1]);

// 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
?>

但它看起来像这样: http://img638.imageshack.us/img638 /4575/testphp.png (砍掉部分)

I'd like to generate a fancy font text image using GD.

I have found:

<?php
// Settings
$sText = 'This is just a test!'.@$_GET['t']; // Text of heading
$sFont = 'Bryant-Bold.ttf'; // Default font for headings
$sMain = @$_GET['c'] ? $_GET['c'] : 0xF2AB27; // Get a font or default it

// Calcuate the width of the image
$arSize = imagettfbbox(24, 0, $sFont, $sText);
$iWidth = abs($arSize[2] - $arSize[0]);
$iHeight = abs($arSize[7] - $arSize[1]);

// 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
?>

But it looks like this: http://img638.imageshack.us/img638/4575/testphp.png
(chops off parts)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻之岛 2024-10-07 18:07:54

您的输出图像使用固定宽度 200!

$hImage = imagecreatetruecolor(200, 24);

您应该动态计算宽度 - 可能您需要使用变量 $hImage

$hImage = imagecreatetruecolor($hImage, 24);

You're using a fixed width of 200 for your output image!

$hImage = imagecreatetruecolor(200, 24);

You should be calculating the width dynamically - probably the variable $hImage is what you need to use:

$hImage = imagecreatetruecolor($hImage, 24);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文