1

My image looks like this:

enter image description here

Its getting the image from antibot.php

Here is my antibot.php

<?php
    session_start();
    $width      = 75;
    $height     = 40;
    $length     = 3;
    $baseList   = '1234567890';
    $code       = "";
    $counter    = 0;
    $image      = @imagecreate($width, $height) or die('Kunne ikke velge GD!');

    for($i=0; $i<10; $i++){
       imageline($image, 
             mt_rand(0,$width), mt_rand(0,$height), 
             mt_rand(0,$width), mt_rand(0,$height), 
             imagecolorallocate($image, mt_rand(150,255), 
                                        mt_rand(150,255), 
                                        mt_rand(150,255)))) or die('Kunne ikke velge line!');
    }

    for($i=0, $x=0; $i<$length; $i++){
       $actChar = substr($baseList, rand(0, strlen($baseList)-1), 1);
       $x += 10 + mt_rand(0,10);
       imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $actChar, 
          imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155)))) or die('Kunne ikke velge GD2!');
       $code .= strtolower($actChar)) or die('Kunne ikke velge GD3!');
    }

    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
    $_SESSION['securityCode'] = $code;
?> 

I have installed GD and freetype, could anyone help me to fix this?

  • 1
    Why go through all that trouble of reinventing the wheel. Using captcha is just _way_ easier, if you ask me. It's all been done before... – Elias Van Ootegem Aug 19 '13 at 11:37
  • Open the image URI in the browser and paste the output, please. You probably have a syntax error in your second *for* loop. – ComFreek Aug 19 '13 at 11:38
  • possible duplicate of [Image wont show (PHP and GD)](http://stackoverflow.com/questions/18300535/image-wont-show-php-and-gd). You've asked this exact question before. Sure, this time around you got a different error message, but that's coding for you. half of the time, it's about debugging, which is something you have to do yourself, this isn't a free debugger service – Elias Van Ootegem Aug 19 '13 at 13:12

2 Answers2

1

There's a syntax error, ComFreek is right:

   imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $actChar, 
      imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155)))) or die('Kunne ikke velge GD2!');
//                                                                            /\4?

Has one closing parentheses too many, it should be

   imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $actChar, 
      imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155))) or die('Kunne ikke velge GD2!');
//                                                                       3 is enough

Oh and please, don't use or die. If there's an error, fix it. Don't use a pointless or die message. Let PHP show the errors (as in line X: Unexpected )).
Also: don't use the @ error supressing operator. If there's an error: Fix it, don't cover it up. Have your ini set to E_STRICT | E_ALL and display errors set to 1, and work at your code until it's notice-free.

$image      = @imagecreate($width, $height) or die('Kunne ikke velge GD!');
//should be:
$image = imagecreate($width, $height);

It makes your code more readable, and if something goes wrong, at least you stand a better chance to actually know what to fix. Something like "Kunne ikke velge GD!" is about as meaningless as "Something didn't quite work... no idea what or where"

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
1

Your code doesn't have many problems. Remove the extra brackets at the end of the imagecolorallocate inside the for loops. It is just having some syntax errors.

<?php
    session_start();
    $width      = 75;
    $height     = 40;
    $length     = 3;
    $baseList   = '1234567890';
    $code       = "";
    $counter    = 0;
    $image      = @imagecreate($width, $height) or die('Kunne ikke velge GD!');

    for($i=0; $i<10; $i++){
       imageline($image, 
             mt_rand(0,$width), mt_rand(0,$height), 
             mt_rand(0,$width), mt_rand(0,$height), 
             imagecolorallocate($image, mt_rand(150,255), 
                                        mt_rand(150,255), 
                                        mt_rand(150,255))) or die('Kunne ikke velge line!'); 
    }

    for($i=0, $x=0; $i<$length; $i++){
       $actChar = substr($baseList, rand(0, strlen($baseList)-1), 1);
       $x += 10 + mt_rand(0,10);
       imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $actChar, 
          imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155))) or die('Kunne ikke velge GD2!');
       $code .= strtolower($actChar) or die('Kunne ikke velge GD3!');
    }

    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
    $_SESSION['securityCode'] = $code;
?> 

But prefer to use something like recaptcha which is quite easier to implement and you can let your users contributing something to digitizing the books by using it.

Stranger
  • 10,332
  • 18
  • 78
  • 115