Writing text on image using GD is not working

微笑、不失礼 提交于 2021-02-11 06:55:27

问题


I'm using PHP 7.4 and when i try to write text on image using imagettftext() function nothing happen only blank image! when i roll back to PHP 5.6 it works perfect.

I've confirmed that GD is enabled and i'm under windows OS

Here is my code (i have copied it from php.net):

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

Is it true that “function imagettftext undefined on php 7.4”?


回答1:


I think the font should be the Problem here. You should include it with a path. Read the documentation at php:imagettftext

Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.

So you should make sure where the font is. Or try different combinations with leading "/" or without the file extension



来源:https://stackoverflow.com/questions/65447082/writing-text-on-image-using-gd-is-not-working

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