开发者

Dynamic Images Based on Text PHP

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
开发者_JAVA技巧$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)


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜