使用 GD 在图像上写入文本不起作用
Writing text on image using GD is not working
我正在使用 PHP 7.4
,当我尝试使用 imagettftext()
函数在图像上写文字时,什么也没有发生,只有空白图像!当我回滚到 PHP 5.6
它完美无缺。
我已经确认 GD 已启用并且我在 windows OS
这是我的代码(我从 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);
“function imagettftext undefined on php 7.4”是真的吗?
我认为字体应该是这里的问题。
您应该将其包含在路径中。阅读 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.
所以你应该确定字体在哪里。或者尝试带前导“/”或不带文件扩展名的不同组合
我正在使用 PHP 7.4
,当我尝试使用 imagettftext()
函数在图像上写文字时,什么也没有发生,只有空白图像!当我回滚到 PHP 5.6
它完美无缺。
我已经确认 GD 已启用并且我在 windows OS
这是我的代码(我从 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);
“function imagettftext undefined on php 7.4”是真的吗?
我认为字体应该是这里的问题。 您应该将其包含在路径中。阅读 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.
所以你应该确定字体在哪里。或者尝试带前导“/”或不带文件扩展名的不同组合