I'm tempted to say that the problem is that the system is not finding the 
font... you'd need to include the full path to the font (and it must be 
readable for the user PHP runs on behalf).

Try the following:

Just for testing put the font and the script that generates the image in the 
SAME directory, so let's say you have something like this:

arial.ttf (<<-- I just copied it from my windows fonts folder)
PrintImage.php

BOTH IN THE SAME DIRECTORY...
Then, the code for PrintImage.php would look like the following...

<?php
/************************/
/* Begin PrintImage.php */

// dirname(__FILE__) means "the full path to the directory where this file 
<PrintImage.php> is sitting"
$fontFile = dirname(__FILE__).'/arial.ttf';

$imW = 200;
$imH = 100;
$im = imagecreatetruecolor($imgW, $imgH);
$bgColor = imagecolorallocate($im, 238, 239, 239);
$borderColor = imagecolorallocate($im, 208, 208, 208);
$textColor = imagecolorallocate($im, 46, 60, 31);
$whiteColor = imagecolorallocate($im, 255, 255, 255);
$fontSize = 18;
$textAngle = 0;
$codeString = 'Works!';

// Print rectangle
imagefilledrectangle($im, 0, 0, $imgW, $imgH, $bgColor);
imagerectangle($im, 0, 0, $imW-1, $imH-1, $borderColor);

// Print Text (Calculate Position)
$box = imagettfbbox($fontSize, $fontAngle, $fontFile, $codeString);
$x = (int)($imgW - $box[4]) / 2;
$y = (int)($imgH - $box[5]) / 2;
imagettftext($im, $fontSize, $fontAngle, $x, $y, $textColor, $fontFile, 
$codeString);

// Output... no caching
header("Content-type: image/png");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
imagepng($im);

/* End PrintImage.php */
/**********************/
?>

Then you know what you do... you load PrintImage.php into your browser and 
you'll get a nice gray rectangle with the word "Works!" in the center of it. If 
you don't get that... then you have a problem that is not related to path or to 
PHP per-se... maybe it's a GD issue, or the font is broken... or whatever other 
issue... but this code works in windows and linux provided that you get the 
"arial.ttf" in the same directory as PrintImage.php.

And... use dirname(__FILE__) or similar (meaning ABSOLUTE PATHS), as much as 
possible to reference other files for inclusion or processing... doing 
"./myfile.php" or "../myfile.php" leads to headaches... and usually in the 
moment you can't take a headache (which is project deadlines).

Hope this helps,

Rob
> -----Original Message-----
> From: Dave M G [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, December 18, 2007 12:31 AM
> To: Andrés Robinet
> Cc: 'PHP List'
> Subject: Re: [PHP] Writing text into images, and setting text size
> 
> Andrés,
> 
> Thank you for responding.
> 
> > Deploy the fonts along with your scripts... that's the only way I
> know.
> > ... I do so for a custom CAPTCHA script I've made.
> 
> This sounds like a good solution. I'm having a little trouble
> implementing it, however.
> 
> I have what I believe is a freely distributable font called
> "FreeSans.ttf", and I put it in a directory called "fonts" in the base
> directory of my site.
> 
> However, this seems not to work:
> 
> $font = 'fonts/FreeSans.ttf';
> imagettftext($image, 20, 0, $x, $y, $textColour, $font, $text);
> 
> Putting a slash in front to specify starting from the base directory -
> '/fonts/FreeSans.ttf' - does not seem to work either.
> 
> Looking in the manual, it seemed that maybe I needed to set the font
> path with putenv:
> 
> $path = realpath('fonts');
> putenv('GDFONTPATH=' . $path);
> 
> But this yielded no results.
> 
> Am I still getting the syntax wrong somehow?
> 
> Thank you for any advice.
> 
> --
> Dave M G
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to