Jump to content

Recommended Posts

Hi,

 

I have no idea why the code below does not work. I cannot see anything wrong. ! get the following error:

 

<b>Warning</b>:  imagettftext() [<a href='function.imagettftext'>function.imagettftext</a>]: Invalid font filename in....on line 64

 

There is no line 64

 

Any help much appreciated.

 

COde:


<?php
  session_start();


// Set some important CAPTCHA constants
  
define('CAPTCHA_NUMCHARS', 6);   
define('CAPTCHA_WIDTH', 100);    
define('CAPTCHA_HEIGHT', 25);   

// Generate the random pass-phrase
  

$pass_phrase = "";
  
for ($i = 0; $i < CAPTCHA_NUMCHARS; $i++) {
    
$pass_phrase .= chr(rand(97, 122));
  }

  

// Store the encrypted pass-phrase in a session variable
  
$_SESSION['pass_phrase'] = SHA1($pass_phrase);

  

// Create the image
  
$img = imagecreatetruecolor(CAPTCHA_WIDTH, CAPTCHA_HEIGHT);

  

// Set a white background with black text and gray graphics
  
$bg_color = imagecolorallocate($img, 255, 255, 255);     // white
  
$text_color = imagecolorallocate($img, 0, 0, 0);         // black
  
$graphic_color = imagecolorallocate($img, 64, 64, 64);   // dark gray

  

// Fill the background
  imagefilledrectangle($img, 0, 0, CAPTCHA_WIDTH, CAPTCHA_HEIGHT, $bg_color);
  

// Draw some random lines
  
for ($i = 0; $i < 5; $i++) {
    imageline($img, 0, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphic_color);
  }

  

// Sprinkle in some random dots
  
for ($i = 0; $i < 50; $i++) {
    imagesetpixel($img, rand() % CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphic_color);
  }
  

// Draw the pass-phrase string

  imagettftext($img, 18, 0, 5, CAPTCHA_HEIGHT - 5, $text_color, 'Courier New Bold.ttf', $pass_phrase);

  

// Output the image as a PNG using a header
  
header("Content-type: image/png");
  imagepng($img);


  // Clean up
  
imagedestroy($img);



?>

 

Link to comment
https://forums.phpfreaks.com/topic/268223-captcha/
Share on other sites

My best guess is that it is referring to:

 

imagettftext($img, 18, 0, 5, CAPTCHA_HEIGHT - 5, $text_color, 'Courier New Bold.ttf', $pass_phrase)

 

Given that is the name of the function. So your "Courier New Bold.ttf" is most likely not setup in the directory you are calling the script from. Looking at the manual for imagettftext

 

fontfile

The path to the TrueType font you wish to use.

 

Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

 

When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.

 

In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.

 

<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>

 

It would seem that the spaces in the font file may be causing the problem as well as the .ttf you added on.

Link to comment
https://forums.phpfreaks.com/topic/268223-captcha/#findComment-1376758
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.