2010-11-08 58 views
2

我想用GD生成一個奇特的字體文本圖像。基於文本的動態圖像PHP

我發現:

<?php 
// Settings 
$sText = 'This is just a test!'[email protected]$_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

回答

3

您正在使用的200固定寬度爲輸出圖像(砍掉部分)!

$hImage = imagecreatetruecolor(200, 24); 

你應該動態地計算寬度 - 可能是變量$hImage是你需要的內容:

$hImage = imagecreatetruecolor($hImage, 24);