Question

its working perfect, until i try to use a font for text.

This is my code.. The 3er line commented now is the one that works fine... but in this example im trying to replace this line with imagettftext.. not lucky.

Whats my error?

$newImage = imagecreatefromjpeg( "orsil_secure.jpg" ); 

$txtColor = imagecolorallocate($newImage, 0, 0, 0); 

//imagestring($newImage, 5, 10, 27, $ranStr,  $txtColor); 

imagettftext($newImage, 5, 10, 27, $txtColor, $font_path, $ranStr);

header( "Content-type: image/jpeg" ); 

imagejpeg($newImage); 

Oh yes in the previous lines is the route to the font here:

// random number

$ranStr = md5(microtime()); 

$ranStr = substr($ranStr, 0, 6); 

// Set Path to Font File
  $font_path = 'captcha.TTF';
Was it helpful?

Solution

It looks like you have the wrong parameters in your call to imagettftext.

Presumably, the 5 corresponds to the font used in imagestring, but has no place in imagettftext. You also need to specify the size and angle.

For example:

imagettftext($newImage, 12, 45, 10, 27, $txtColor, $font_path, $ranStr);
                        ^^  ^^
                        ||  ||
                        ||   ------ angle
                        ----------- size

In this example:

size=12px or 12pt, depending on whether you're using GD1 or GD2

angle = 45°

Obviously, you'll want to use your own values here.

If it still isn't working, then it's likely that the path to your font file is wrong. Check to see if it's in the same folder as the PHP script.

OTHER TIPS

I think you are missing a parameter in your imagettftext. If you look at http://php.net/manual/en/function.imagettftext.php it has size, angle, x and y.
You are missing one of the 4 (im guessing the angle).

So it needs to be something like:

imagettftext($newImage, 5, 0, 10, 27, $txtColor, $font_path, $ranStr);

Where 0 is for angle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top