【问题标题】:imagettftext not workingimagettftext 不工作
【发布时间】:2011-08-11 23:33:58
【问题描述】:

我尝试在图像中写入文本只是为了测试目的,因为我的 Zabbix 安装没有在图表中写入文本。我从 php.net 网站 (http://php.net/manual/en/function.imagettftext.php) 复制了下面的代码

<?php
// 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 = '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf'; 

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

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

有人有想法吗?

【问题讨论】:

  • 描述您的问题。什么不工作?
  • 我已经弄清楚这里出了什么问题。访问 truetype 文件的权限。 PHP 无法访问该文件,因此无法写入。我没有看到问题,因为我没有使用 E_ALL 运行。现在一切顺利

标签: php debian text gd


【解决方案1】:

我已经弄清楚这里出了什么问题。访问 truetype 文件的权限。 PHP 无法访问该文件,因此无法写入。

我没有看到问题,因为我没有使用 E_ALL 运行。现在一切顺利

【讨论】:

    【解决方案2】:

    我的问题是错误的偏移量。图像没有显示任何内容,没有文本,源代码中没有错误,只是一个空白文件。路径是正确的。我以为 ttf 字体有错误,但结果只是定位错误。

    以下是帮助我看到一些文字的原因:

    imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);
    

    这会在右上角显示一些文字。

    完整的工作代码:

        putenv('GDFONTPATH=' . dirname(__FILE__));
        $font = 'arial'; // located next to the script
        imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);
    

    【讨论】:

      【解决方案3】:

      这是一个 GD 函数,您需要安装 php-gd 并支持 gettext 和 ttf。

      -$

      【讨论】:

      • GD 现在与 PHP 捆绑在一起。另一个要求是 FreeType
      • 我知道这一点。还可以。我可以画画,我可以不用truetype写作。问题是访问字体文件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多