【问题标题】:imagettftext do not workimagettftext 不起作用
【发布时间】:2014-11-04 10:03:52
【问题描述】:

我用imagettftext()试试这个代码:

// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);

但它不能正常工作,我安装了 GD 和 FreeType,如您所见:

谁能指出我正确的方向?

【问题讨论】:

  • 您好,显示的错误是什么? (要查看它,请在删除内容类型标头后直接访问您的图像)。

标签: php gd freetype imagettftext


【解决方案1】:

将变量 $font 分配给字体文件的绝对路径。

【讨论】:

  • 改成绝对路径,还是一样的问题:(
  • 不,我粘贴在主帖中的代码是我尝试运行的唯一代码。
  • 您收到什么样的错误信息?例如:空白页或无效的图像符号
  • 我得到一个无效的图像符号
  • 尝试清理输出缓冲区。 ob_clean();把它放在 imagepng 函数之前
【解决方案2】:
// Replace path by your own font path
$font = 'arial.ttf';

您的代码正在寻找不存在的“arial.ttf”。请将字体文件(arial.ttf)复制到代码的同一目录中。

【讨论】:

    【解决方案3】:

    尝试替换你的字体路径

    $font = 'arial.ttf';
    

    $font = __DIR__ .'\Arial.ttf';
    

    或使用绝对路径(不正确但有效),如果您使用 Windows 系统

    $font = 'c:\WINDOWS\Fonts\Arial.ttf';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      相关资源
      最近更新 更多