legobear154
legobear154

Reputation: 431

PHP imagettftext invalid font filename

I am trying to create an image with text by using imagettftext. It is telling me Warning: imagettftext(): Invalid font filename in C:\xampp\htdocs\recentpost.php on line 32. Here is the code on line 32 I am using to add the text

imagettftext($img, 12, 0, 20, 1, $black, "../fonts/arial.ttf", "News!");

I copied the font right out of the C:/Windows/Fonts folder so it is a valid font.

Upvotes: 8

Views: 29861

Answers (6)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Try something like:

$font = dirname(__FILE__) . '/arial.ttf';
//OR
$font = dirname(__FILE__) . '/fonts/arial.ttf';    
imagettftext($img, 12, 0, 20, 1, $black, $font, "News!");

Upvotes: 23

Udor Blessing
Udor Blessing

Reputation: 11

I tried using the suggestions above but none worked for me.

So, I will be adding what worked for me in case, someone else was in my shoes.

I used

imagettftext($img, 12, 0, 20, 1, $black, getcwd()."\fonts\\arial.ttf", "News!");

Please note that the extra backslash before 'arial.ttf' is to escape the backslash in the original font file directory.

getcwd() is used for getting the current working directory. You can read more here.

You can use echo getcwd()."\fonts\\arial.ttf" to get the full directory and change all the backslash to forward slash if you wish. It will still work fine

Upvotes: 0

gardist
gardist

Reputation: 1

Try to use double slash:

imagettftext($img, 12, 0, 20, 1, $black, "..\\fonts\\arial.ttf", "News!");

Upvotes: 0

legobear154
legobear154

Reputation: 431

Oh wow, stupid me... Its too late for me to be up working on php :p I was trying to get to get to a non existing folder. The real code should be

imagettftext($img, 12, 0, 20, 1, $black, "fonts/arial.ttf", "News!");

Thank you everyone for trying to fix my silly mistake :p

Upvotes: 1

Sebr
Sebr

Reputation: 110

Have you tried using

imagettftext($img, 12, 0, 20, 1, $black, "../fonts/arial.TTF", "News!");

instead? (.TTF instead of .ttf)

and @Fab, you can use both \ and / on windows.

Upvotes: 1

Fab V.
Fab V.

Reputation: 1082

If you are on Windows, you should use "..\fonts\arial.ttf" as path I think

Upvotes: 0

Related Questions