【问题标题】:Dynamic Images Based on Text PHP基于文本 PHP 的动态图像
【发布时间】:2010-11-08 10:23:29
【问题描述】:

我想使用 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 (切掉部分)

【问题讨论】:

    标签: php gd gd2


    【解决方案1】:

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

    $hImage = imagecreatetruecolor(200, 24);
    

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

    $hImage = imagecreatetruecolor($hImage, 24);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 2015-12-10
      • 2013-07-07
      • 2011-07-19
      • 2016-03-20
      • 1970-01-01
      相关资源
      最近更新 更多