 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
milandi
Joined: 14 Aug 2007 Posts: 1
|
Posted: Tue Aug 14, 2007 7:40 pm Post subject: Error initializing Image_Text (font missing?!) |
|
|
Hi,
I got captcha working fine on my machine (windows). When I moved to hosting machine which is linux I installed PEAR packages I need and adjusted paths. Everything works fine except CAPTCHAS. I tried all possible paths however I am getting:"Error initializing Image_Text (font missing?!)" message. My web site is located in /home/myusername/mywebsite.com/dir/index.php. I copied arial.ttf from my windows machine to /home/myusername/mywebsite.com/dir/fonts/arial.ttf
What font_path should be or something else is missing (my user has right to write all directories and folders should, is that enough?).
Note: I can't log in as su, because it is shared hosting. Anyhelp would be highly appreciated, because I am stuck.
The code is:
$options = array( 'font_size' => 24,'font_path' => './fonts/', 'font_file' => 'arial.ttf' );
$c = Text_CAPTCHA::factory('Image');
$retval = $c->init(200, 80, null, $options);
if (PEAR::isError($retval)) {
die($$retval->getMessage() . '<br />' . $retval->getDebugInfo());
exit;
}
$png = $c->getCAPTCHAAsPNG();
if (PEAR::isError($png)) {
// it fails here !!!!!!!!!!!!!!!!!!!!!!!!1
die($png->getMessage() . '<br />' . $png->getDebugInfo());
exit;
} // if
thx
Milan |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1051
|
Posted: Tue Aug 14, 2007 10:10 pm Post subject: |
|
|
| If something is working on Windows, but not on Linux, in many cases upper and lower case letters can be the problem. On Windows, Arial.ttf and arial.ttf is the same file, but on Linux there can be two files, one named Arial.ttf, one named arial.ttf (and aRial.ttf, arIal.ttf, ...). |
|
| Back to top |
|
 |
BA
Joined: 31 Aug 2007 Posts: 2
|
Posted: Fri Aug 31, 2007 4:48 am Post subject: |
|
|
I have the exact same problem. But since milandi had it working already on a Unix box maybe the origin of our problem is different. But there is definitely a bug in the CAPTCHA code, depending on which code example you refer to. Let me explain...
Like milandi, I tried every combination of paths I could think of...nothing worked. So I got fed up and just tried to debug it myself. Here's a summary of the problem:
First, there are 2 relevant files:
- image CAPTCHA's code is contained largely in Image.php
- image CAPTCHA is dependent upon the Image_text package, whose code is contained in a file called Text.php
CAPTCHA is setting the default image options (in Image.php) like this:
| Code: | var $_imageOptions = array(
'font_size' => 24,
'font_path' => './',
'font_file' => 'COUR.TTF',
'text_color' => '#000000',
'lines_color' => '#CACACA',
'background_color' => false);
|
Notice the boolean value for 'background_color'...this is where the problem lies. This default value must be either a HEX value, or NULL.
The init() function of the Image_text class, contained in Text.php, is not expecting a boolean in 'background_color', as you can see below, it is expecting a HEX value, or null:
| Code: | if ($this->options['background_color'] === null) {
$this->options['enable_alpha'] = true; |
So this is where the bug is. But it only occurs if you do not specify an array of options that includes the 'background_color', because in that case image CAPTCHA passes incorrect default values to the Image_text class. So.... you can get around this if you specify an $imageOptions array...use this code, it works (change the path to your fonts first):
| Code: | require_once 'Text/CAPTCHA.php';
//Specify your own background_color and stuff here. If you don't do
// this, image CAPTCHA will instead use default values that are not
// the correct data types!
$imageOptions = array(
'font_size' => 24,
'font_path' => '/path/to/your/fonts/',
'font_file' => 'arial.ttf',
'text_color' => '#DDFF99',
'lines_color' => '#CCEEDD',
'background_color' => '#555555'
);
// Set CAPTCHA options
$options = array(
'width' => 200,
'height' => 80,
'output' => 'jpeg',
'imageOptions' => $imageOptions //make sure to specify this!
);
// Generate a new Text_CAPTCHA object, Image driver
$CAPTCHA = Text_CAPTCHA::factory('Image');
$retval = $CAPTCHA->init($options); //pass only the $options array
...etc...
|
the key here is that you must specify the $imageOptions array, then on the init() function, pass in ONLY the $options array (which contains $imageOptions inside it as a sub-array, which in turn has all the necessary values contained inside it).
The example code found at http://pear.php.net/manual/en/package.text.text-captcha.php is totally inadequate, and in fact, it is wrong. The code in this example does not show how to set the $imageOptions array, which is CRITICAL if you want to get image CAPTCHAs working. The sample code that you can view inside the PEAR web installer, however, does indeed show you how to set the $imageOptions array. Bit of a contradiction there, I think....
This is my first experience at using a PEAR package. I have to say, the documentation is very misleading, and in some cases (as I just pointed out) flat out incorrect. If this is what I can expect from all PEAR packages I think I'll just stick to my own libraries. Very disappointed so far...I wasted hours on this today.
To fix this bug, the CAPTCHA developer(s) need to change this code inside their Image.php file:
| Code: |
var $_imageOptions = array(
'font_size' => 24,
'font_path' => './',
'font_file' => 'COUR.TTF',
'text_color' => '#000000',
'lines_color' => '#CACACA',
'background_color' => false); |
so that background_color is either null, or some HEX value such as #FFFFFF etc... |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1051
|
Posted: Fri Aug 31, 2007 7:19 am Post subject: |
|
|
| If you think that there is a bug, please report it via the bug tracker on pear.php.net. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|