PHP imagettftext():无法使用“带文本的图像”PHP库

时间:2015-08-09 15:04:48

标签: php

我正在尝试使用this库生成图像,我已经按照示例here,但是当我运行脚本时,我在error.log文件中出现了这些错误:

[09-Aug-2015 14:40:54 UTC] PHP Warning:  imagettfbbox(): Could not find/open font in /home/user/public_html/my_website/vendor/nmcteam/image-with-text/src/NMC/ImageWithText/Text.php on line 166
[09-Aug-2015 14:40:54 UTC] PHP Warning:  imagettfbbox(): Could not find/open font in /home/user/public_html/my_website/vendor/nmcteam/image-with-text/src/NMC/ImageWithText/Text.php on line 182
[09-Aug-2015 14:40:54 UTC] PHP Warning:  imagettfbbox(): Could not find/open font in /home/user/public_html/my_website/vendor/nmcteam/image-with-text/src/NMC/ImageWithText/Text.php on line 194
[09-Aug-2015 14:40:54 UTC] PHP Warning:  imagettftext(): Could not find/open font in /home/user/public_html/my_website/vendor/intervention/image/src/Intervention/Image/Image.php on line 1300

这是我的网站结构:

-public_html
    -my_website
        -vendor
            -autoload.php
            -nmcteam
                -image-with-text
                    -NMC
                        -ImageWithText
                            -Text.php
            -intervention
                -src
                    -Intervention
                        -Image
                            -Image.php
            -
        -php
            -img_creator.php
            -font.ttf
            -source.png

这是我的脚本(img_creator.php):

<?php
// Enable Composer autoloading
require '../vendor/autoload.php';

// Create image
$image = new \NMC\ImageWithText\Image('source.jpg');

// Add text to image
$text1 = new \NMC\ImageWithText\Text('Thanks for using our image text PHP library!', 3, 25);
$text1->align = 'left';
$text1->color = 'FFFFFF';

$text1->font = "font.ttf";
$text1->lineHeight = 36;
$text1->size = 24;
$text1->startX = 40;
$text1->startY = 40;
$image->addText($text1);

// Add more text to image
$text2 = new \NMC\ImageWithText\Text('No, really, thanks!', 1, 30);
$text2->align = 'left';
$text2->color = '000000';
$text2->font = 'fonts/Ubuntu-Medium.ttf';
$text2->lineHeight = 20;
$text2->size = 14;
$text2->startX = 40;
$text2->startY = 140;
$image->addText($text2);

// Render image
$image->render('destination.jpg');

我搜索了很多,但我找不到解决方案。

this对我不起作用。 我试图在库文件夹中移动字体,但这个解决方案都不起作用。

1 个答案:

答案 0 :(得分:1)

设置字体时尝试使用绝对路径,如下所示:

$text1->font = dirname(__FILE__) . '/path/to/font/Ubuntu-Medium.ttf';

或者如果您使用的是PHP 5.3 +

$text1->font = __DIR__ . '/path/to/font/Ubuntu-Medium.ttf';

在您的示例中,路径应为:

$text1->font = __DIR__ . '/font.ttf';