使用GD在图像上写文本不起作用 [英] Writing text on image using GD is not working
问题描述
我正在使用 PHP 7.4
,当我尝试使用 imagettftext()
函数在图像上写文本时,仅空白图像不会发生!当我回滚到 PHP 5.6
时,它运行完美.
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.
我已确认已启用GD,并且我使用的是Windows OS
I've confirmed that GD is enabled and i'm under windows OS
这是我的代码(我已经从 php.net复制了该代码):
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);
是真的"在PHP 7.4上未定义的function imagettftext "?
推荐答案
我认为字体应该是这里的问题.您应该在路径中包含它.阅读 php:imagettftext
I think the font should be the Problem here. You should include it with a path. Read the documentation at php:imagettftext
取决于PHP使用的GD库版本,如果fontfile不是以前导/开头,则.ttf将附加到文件名,并且库将尝试沿着库定义的字体路径搜索该文件名.
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.
当使用低于2.0.18的GD库版本时,空格字符(而不是分号)被用作不同字体文件的路径分隔符".意外使用此功能将导致出现警告消息:警告:找不到/打开字体.对于这些受影响的版本,唯一的解决方案是将字体移动到不包含空格的路径.
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
这篇关于使用GD在图像上写文本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!