Nico
Nico

Reputation: 7256

PHP imagettftext(): Could not find/open font with "Image with Text" PHP library

I'm trying to generate images with this library, i've followed the example here, but when i run the script i got these errors on my error.log file:

[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

this is my website structure:

-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

and this is my script(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');

i've searched a lot but i cant find a solution.

this does not works for me. i've tried to move font in the library folder but neither this solution works.

Upvotes: 1

Views: 10939

Answers (1)

Jamie Bicknell
Jamie Bicknell

Reputation: 2326

Try to use an absolute path when setting your font, like so:

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

or if you're on PHP 5.3+

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

In your example, the path should be:

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

Upvotes: 2

Related Questions