where are the fonts filed away for imagettftext() function

  • Thread starter Thread starter Anonymous
  • Start date Start date
A

Anonymous

Guest
I am trying to load the fonts from my GD libraray and I have no idea what to type into the path. Where does that usually get saved when you load PHP? Any examples of the line pointing to the PATH that actually work? I keep getting the "failed to find/open the font" error. :?
 
Afaik fonts are not included with GD. You should upload TTF fonts yourself - anywhere you like - and specify the path to them in your PHP script.

Coditor
 
I can't seem to get the code to identify the path correctly. I am using the following to try and get it to work. Am I typing the path incorrectly?

Code:
<?php
//create the canvas
$myImage = imagecreate(150,150);

//set up some colors
$white = imagecolorallocate ($myImage, 255, 255, 255);
$green = imagecolorallocate ($myImage, 50, 150, 50);
$blue = imagecolorallocate ($myImage, 0, 0, 100);
$lt_red = imagecolorallocate($myImage, 255, 150, 150);
$lt_green = imagecolorallocate($myImage, 100, 200, 100);
$lt_blue = imagecolorallocate($myImage, 50, 50, 150);

//display some text
imagettftext($myImage, 0, 10, 0, 0, 50, $white, 'C:\Program Files\ApacheGroup\Apache2\htdocs\Fonts', 'text to display');
?>

I downloaded the TTF files into a folder called 'Fonts' in the document root of the webserver using the path above. What am I doing wrong?

Cheers!
Justin
 
The path should contain the name of the fontfile as well.

Eg:
imagettftext($myImage, 0, 10, 0, 0, 50, $white, 'C:\Program Files\ApacheGroup\Apache2\htdocs\Fonts\Arial.ttf', 'text to display');

Coditor
 
I added the Arial.ttf to the end of the path, there is an Arial.ttf font file in the folder too. Still no go. Is there a php.ini setting that I am missing?
 
Why doesn't this code work?! I did check the .ini settings. The GD library is enabled with FreeType.

Code:
<?php
//create the canvas
$myImage = imagecreate(300,150);

//set up some colors
$white = imagecolorallocate ($myImage, 255, 255, 255);
$green = imagecolorallocate ($myImage, 50, 150, 50);
$blue = imagecolorallocate ($myImage, 0, 0, 100);

//set the path to fonts
$font_path = "C:/WINDOWS/Fonts/Arial.ttf";

//insert text to image
imagettftext($myImage, 5, 0, 0, 0, 50, $blue, $font_path, 'texttoadd');

//output the image to the browser
header ("Content-type: image/png");
imagePng($myImage);

//clean up after myself
imagedestroy($myImage);
?>

The exact reply that I get is the following

Warning: imagettftext() [function.imagettftext]: Could not find/open font in C:\Program Files\Apache Group\Apache2\htdocs\imagettftest.php on line 14

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\imagettftest.php:14) in C:\Program Files\Apache Group\Apache2\htdocs\imagettftest.php on line 17
‰PNG IHDR,–5]A PLTEÿÿÿ2–2dyXH "IDATxœíÁ1 õOm >,ˆžÖÿdIEND®B`‚
 
imagettftext requires the following parameters:

resource image, float size, float angle, int x, int y, int color, string fontfile, string text

You're calling it like:
Code:
imagettftext($myImage, 5, 0, 0, 0, 50, $blue, $font_path, 'texttoadd');
There is one number too much, so $blue is now the value your providing for the font. Try this:
Code:
imagettftext($myImage, 5, 0, 0, 50, $blue, $font_path, 'texttoadd');
(assuming that you want y to be 50)

Coditor
 
My gosh. I wasted hours on that problem lol! I guess that's a noob programer for ya! Thanks Coditor!

Cheers!

Justin
 
Back
Top