imagettftext not working

假如想象 提交于 2020-01-16 03:51:13

问题


I've tried to write text in an image just for testing purposes because my Zabbix install is not writing text in the graphs. I've copied the code bellow from the php.net website (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);
?>

Does anyone have an idea?


回答1:


I've figure out what was the problem here. Permissions to access the truetype file. PHP could not access the file so it couldn't write.

I was not seeing the problem because I was not running with E_ALL. Now everything is running smoothly




回答2:


My issue was a wrong offset. The image was showing nothing, no text, no errors in the source code, just a blank file. The paths were correct. I thought there was an error in the ttf font but turns out it was just wrong positioning.

Here's what helped me to see a bit of the text:

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

This shows a bit of text on the top right.

Full working code:

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



回答3:


It is a GD function and you will need php-gd installed with gettext & ttf support.

-$



来源:https://stackoverflow.com/questions/7034698/imagettftext-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!