Captcha script bug

NOTE: I ran across this Captcha scriptie with a web search at this guy’s personal web site’s forum (rohitab.com). I’ve attempted frequently to correspond with the author about a bug I’m experiencing. I hate posting work from another forum, but I’m anxious to get this to work.

That said…I’m asking y’all…
With this bug not all of the numbers are visible in the generated image. I’ve added an echo of the session var and found it is true. The session var will be the correct number of characters; however, there are times when the image will be missing one character. This occurs most notably on the first load of the page.

I have modified the captcha preferences. I made the final image and character size a little smaller, removed from char_set the zero and one numerals (as they can appear as el and oh), I’ve repeated the arial font in each of the four elements of the $font_list array (to reduce overhead), and chosen only four characters instead of seven.

Since I see the session var on the page, I know the missing font character is not a removed character.

01	--------------------------------------
02	test     $_SESSION VAR     IMG VALUE
03	--------------------------------------
04	0                                  2UV
05	1            Q2UV             UMV
06	2            CUMV             KON
07	3            5KON             D93
08	4            MD93             4GH
09	5            T4GH             8BH
10	6            V8BH             UBJ
11	7            WUBJ             AMN9
12	8            AMN9 ...Next 50+ are OK

As you can see its always the first character.

Chris

01	<?php
02	    session_start();
03	    header('Content-type: image/png');
04	    //a few globals to start us off
/* Original preference line...
        $captcha_det = array(   "session_id" => "captcha_str",
                                                        "width" => 200,
                                                        "height" => 50,
                                                        "num_chars" => 7,
                                                        "num_noise" => 15,
                                                        "char_set" => "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789",
                                                        "min_font_size" => 16,
                                                        "max_font_size" => 22);
*/
05	    $captcha_det = array(   "session_id" => "captcha_test",
06	                            "width" => 120, // =!
07	                            "height" => 35, // =!
08	                            "num_chars" => 4, // =!
09	                            "num_noise" => 15,
10	                            "char_set" => "ABCDEFGHJKLMNOPQRSTUVWXY2345789",// =!
11	                            "min_font_size" => 14, // =!
12	                            "max_font_size" => 20); // =!
13	    $char_spacing = $captcha_det['width'] / $captcha_det['num_chars'];
14	    /*$font_list = array("arial.ttf", "castelar.ttf", "gibli.ttf", "lfaxi.ttf");*/
15	    $font_list = array("arial.ttf", "arial.ttf", "arial.ttf", "arial.ttf");
16	    //start image creation
17	    if (!function_exists('imagecreate') || !function_exists("imagepng") || !function_exists("imagecolorallocate") || !function_exists("imagettftext") || !function_exists("imagettfbbox") || !function_exists("imagedestroy"))
18	    {
19	        return false;
20	    }
21	    $image = @imagecreate($captcha_det['width'], $captcha_det['height']);
22	    if(!$image){
23	        return false;
24	    }
25	    $background_color = imagecolorallocate($image, rand(150,255), rand(150,255), rand(150,255));   
26	    //draw in some noise
27	    for($i = 0; $i < $captcha_det['num_noise']; $i++){
28	        $rand_colour = imagecolorallocate($image, rand(120, 250), rand(120, 250), rand(120, 250));
29	        imageline($image, rand(0, $captcha_det['width']), rand(0, $captcha_det['height']), rand(0, $captcha_det['width']), rand(0, $captcha_det['height']), $rand_colour);
30	    }
31	 
32	    //generate random string
33	    for ($s = '',
34	        $i = 0,
35	        $z = strlen(
36	            $captcha_det['char_set']
37	        )-1;
38	        $i != $captcha_det['num_chars'];
39	        $x = rand(0,$z),
40	        $s .= $captcha_det['char_set']{
41	            $x
42	        },
43	        $i++
44	    );
45	    $_SESSION[$captcha_det['session_id']] = $s;
46	    for($i = 0; $i < strlen($s); $i++){
47	        $font = $font_list[array_rand($font_list)];
48	        $colour = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
49	        $font_size = rand($captcha_det['min_font_size'], $captcha_det['max_font_size']);
50	        $angle = rand(-30, 30);
51	        $char_dets = imagettfbbox($font_size, $angle, $font, $s[$i]);
52	        $x = ($char_spacing / 4) + ($i * $char_spacing);
53	        $y = ($captcha_det['height'] / 2) + (($char_dets[2] - $char_dets[4]) / 4) + rand(5, 10);
54	        imagettftext($image, $font_size, $angle, $x, $y, $colour, $font, $s[$i]);
55	    }
56	    imagepng($image);
57	    imagedestroy($image);
58	?>

Here is the original link where you can download the script and associated files.

Thanks. I’ll check it out.

This Captcha is very small. Its only 58 lines of code. There’s not much to debug. And besides, I’m not looking for a line by line examination (although it is only 58 lines) what I expect to learn, and the primary interest to ask the question in the forum, what I expect is to learn if there are problematic propensities for the php functions:
imagecolorallocate( )
imagecreate( )
imagepng( )
imagettftext( )
imagettfbbox( )

It would be good to know, or hear experiences from other programmers…
C

I cannot help you with your original question. However, I can recommend a free CAPTCHA script that works great.

http://www.phpcaptcha.org/

You can add it to your site with only a few lines of code. I use it and I love it. I suggest you check it out. It would be easier to use something that is known to work right than to spend a lot of time debugging something that doesn’t.