imagettftext 无法打开字体文件

发布于 2024-11-09 12:17:48 字数 2217 浏览 5 评论 0原文

使用 来自 php.net 的示例 我收到警告,并且图像不是正确呈现。我提供了 .ttf 文件的完整路径,如下所示: /var/www/public/myfont.ttf

PHP Warning:  imagettftext() [<a href='function.imagettftext'>function.imagettftext</a>]: Could not find/open font in <phpfile>

我正在使用找到的自定义 .ttf 字体 此处。我可以在 Ubuntu 中很好地打开该文件作为有效的字体文件。我也尝试过其他字体,结果相同。

我使用的是 Ubuntu 10.04 LTS 32 位,安装了 apache2、php5、freetype6 和 php5-gd。我还尝试使用 ttf 文件 chmod 777 文件和文件夹,得到相同的结果。

如何使用自定义 ttf 字体文件使示例正常工作?

*编辑:我正在使用的代码:

<?php
// File is: /var/www/public/test.php
// Apart from $font variable, it's copy-pasted 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 = '/var/www/public/UnmaskedBB.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);
?>

phpinfo() 的输出;

[gd]
GD Support  enabled
GD Version  2.0
FreeType Support    enabled
FreeType Linkage    with freetype
FreeType Version    2.3.11
T1Lib Support   enabled
GIF Read Support    enabled
GIF Create Support  enabled
JPEG Support    enabled
libJPEG Version     6b
PNG Support     enabled
libPNG Version  1.2.42
WBMP Support    enabled 

测试 is_fileis_read

$font = realpath('./').'/UnmaskedBB.ttf';
echo "Font: ".$font; // /var/www/public/UnmaskedBB.ttf
echo "Is file? ".is_file($font); // 1
echo "Is readable? ".is_readable($font); // 1

Using the example from php.net I get a warning, and the image is not rendered correctly. I supply a full path to the .ttf file like so: /var/www/public/myfont.ttf

PHP Warning:  imagettftext() [<a href='function.imagettftext'>function.imagettftext</a>]: Could not find/open font in <phpfile>

I am using a custom .ttf font found here. I can open the file fine in Ubuntu as a valid font file. I also attempted other fonts, with the same result.

I am using Ubuntu 10.04 LTS 32 bit, with apache2, php5, freetype6 and php5-gd installed. I also attempted to chmod 777 file and folder with ttf file, with the same result.

How can I get the example working using a custom ttf font file?

*Edit: The code I'm using:

<?php
// File is: /var/www/public/test.php
// Apart from $font variable, it's copy-pasted 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 = '/var/www/public/UnmaskedBB.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);
?>

Output from phpinfo();

[gd]
GD Support  enabled
GD Version  2.0
FreeType Support    enabled
FreeType Linkage    with freetype
FreeType Version    2.3.11
T1Lib Support   enabled
GIF Read Support    enabled
GIF Create Support  enabled
JPEG Support    enabled
libJPEG Version     6b
PNG Support     enabled
libPNG Version  1.2.42
WBMP Support    enabled 

Testing is_file and is_readable:

$font = realpath('./').'/UnmaskedBB.ttf';
echo "Font: ".$font; // /var/www/public/UnmaskedBB.ttf
echo "Is file? ".is_file($font); // 1
echo "Is readable? ".is_readable($font); // 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小红帽 2024-11-16 12:17:48

插入:

putenv('GDFONTPATH=' . realpath('.'));

您可以尝试在第一个imagettftext前面

,只是为了确保它没有路径问题。 更新

如果您使用像fc-cache这样的字体缓存,请不要忘记刷新它,例如:

sudo fc-cache -f -v

You could try to insert:

putenv('GDFONTPATH=' . realpath('.'));

in front of the first imagettftext, just to make sure it is no path issue.

UPDATE

In case you are using font caches like fc-cache, don't forget to refresh it, e.g.:

sudo fc-cache -f -v
揽月 2024-11-16 12:17:48

尝试将 ttf 文件移动到与 php 文件相同的目录中,并将 $font 更改为

$font = 'UnmaskedBB.ttf';

Try moving the ttf file in the same dir as your php file and change $font to

$font = 'UnmaskedBB.ttf';
捶死心动 2024-11-16 12:17:48

升级发行版,包括所有 php 软件包解决了这个问题

Upgrading distro, including all php packages solved the issue

一抹微笑 2024-11-16 12:17:48

https://www.php.net/manual/en/function.imagettftext。 php

注意:只有当 PHP 编译时支持 freetype (--with-freetype-dir=DIR) 时,此功能才可用

sudo apt-get install freetype*

https://www.php.net/manual/en/function.imagettftext.php

Note: This function is only available if PHP is compiled with freetype support (--with-freetype-dir=DIR)

sudo apt-get install freetype*

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文