Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    395
    Rep Power
    76

    Creating a cpatcha with random fonts


    I have two versions of code to create a CAPTCHA. They both work. But one will not show random fonts for each character. It will display random fonts but they are all the same font.

    This one shows random fonts for each character:
    PHP Code:
    session_start();

    error_reporting(0);

    $image = @imagecreatetruecolor(12030) or die("Cannot Initialize new GD image stream");

    $background imagecolorallocate($image0x660x990x66);
    imagefill($image00$background);
    $linecolor imagecolorallocate($image0x990xCC0x99);
    $textcolor1 imagecolorallocate($image0x000x000x00);
    $textcolor2 imagecolorallocate($image0xFF0xFF0xFF);
    // draw random lines on canvas
    for($i=0$i 6$i++) { imagesetthickness($imagerand(1,3));
    imageline($image0rand(0,30), 120rand(0,30) , $linecolor); }

    // using a mixture of TTF fonts 
    $fonts = array(); 
    $fonts[] = "fonts/font1.ttf";
    $fonts[] = "fonts/font2.ttf";
    $fonts[] = "fonts/font3.ttf";

    session_start();

    $digit ''; for($x 15$x <= 95$x += 20) {
    $textcolor = (rand() % 2) ? $textcolor1 $textcolor2;
    $digit .= ($num rand(09)); 

    imagettftext($image16rand(-20,20), $xrand(15,30), $textcolor$fonts[array_rand($fonts)], $num); 


    }

    $_SESSION['digit'] = $digit
    header('Content-type: image/png'); imagepng($image); imagedestroy($image); 
    This one I want to fix to show random fonts for each character. Any idea how I can revise this one?

    PHP Code:
    session_start();

    error_reporting(E_ALL E_NOTICE);
        
    $image_width 280;
    $image_height 50;
    $characters_on_image 8;
    $fonts = array(); 

    $fonts[] = "fonts/font1.ttf";
    $fonts[] = "fonts/font2.ttf";
    $fonts[] = "fonts/font3.ttf";

    $possible_letters '23456789bcdfghjkmnpqrstvwxyz';
    $random_dots 0;
    $random_lines 20;
    $captcha_text_color="ee432e";
    $captcha_noice_color "ee432e";

    $code '';
    $i 0;
    while (
    $i $characters_on_image

        
    $code .= substr($possible_lettersmt_rand(0strlen($possible_letters)-1), 1);
        
    $i++;
    }
    $font_size $image_height 0.75;
    $image = @imagecreate($image_width$image_height);

    $background_color imagecolorallocate($image255255255);

    $arr_text_color RGB_HEX($captcha_text_color);
    $text_color imagecolorallocate($image$arr_text_color['red'], 
    $arr_text_color['green'], $arr_text_color['blue']);
    $arr_noice_color RGB_HEX($captcha_noice_color);
    $image_noise_color imagecolorallocate($image$arr_noice_color['red'], 
    $arr_noice_color['green'], $arr_noice_color['blue']);

    for( 
    $i=0$i<$random_dots$i++ ) 
    {
        
    imagefilledellipse($imagemt_rand(0,$image_width),
         
    mt_rand(0,$image_height), 23$image_noise_color);
    }


    for( 
    $i=0$i<$random_lines$i++ ) 
    {
        
    imageline($imagemt_rand(0,$image_width), mt_rand(0,$image_height),
         
    mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
    }


    $textbox imagettfbbox($font_size0$fonts[array_rand($fonts)], $code); 
    $x = ($image_width $textbox[4])/2;
    $y = ($image_height $textbox[5])/2;
    imagettftext($image$font_size0$x$y$text_color$fonts[array_rand($fonts)]  , $code);


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

    function 
    RGB_HEX ($hexstr)
    {
        
    $int hexdec($hexstr);
        return array(
    "red" => 0xFF & ($int >> 0x10),"green" => 0xFF & ($int >> 0x8),"blue" => 0xFF $int);

  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    instead of copypasting random code from the Internet, use a proven solution like reCAPTCHA.

    That stuff is nonsense. I'm sure that even the crappiest OCR software can break this every time. This would actually be an interesting exercise.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    395
    Rep Power
    76
    I am looking at this line and comparing how each example displays the fonts differently.

    PHP Code:
    imagettftext($image$font_size0$x$y$text_color$fonts[array_rand($fonts)]  , $code); 
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Have you read my reply? What good is a CAPTCHA if it can be broken by anybody?

    reCAPTCHA, on the other hand, is a proven solution, and it only takes a few minutes to install it. If you don't like Google, maybe there's some other CAPTCHA provider. But copypasting homemade scripts you found on the Internet won't get you anywhere.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    I, for one, applaud you for doing it yourself... so long as "a really good captcha" isn't the end result and you need this for more than just an academic exercise. Like Jacques said, if you need a good solution soon then use reCAPTCHA: not only do they have experience doing it, solved captchas go towards helping scan books.
    No offense - I wouldn't write a captcha myself either because (I'm lazy, but also because) if I needed it "for real" I know that I should prioritize having a functional, high-quality, and effective captcha ahead of my desire to do it myself.

    As for the problem, your code only has one call to imagettftext() while the other has multiple calls for each letter. You need to mimic that, including not just calls per-letter but calculating character positions too (since you need to place each letter after the previous). You can also vary on color at the same time.

    Basically, that while loop you have over $i needs to extend to include most of the code coming after it.
    Last edited by requinix; December 12th, 2013 at 05:27 PM.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by requinix
    I, for one, applaud you for doing it yourself...
    Do you really think this is his/her own code?

    Not a single line is. It's the usual copypasta. So somebody else wrote it, and now somebody else fixes it. Great.

    Personally, I find this game of "Repair the stolen code" rather silly. It neither yields good code nor greater knowledge. But go ahead.
    Last edited by Jacques1; December 12th, 2013 at 06:27 PM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    You want to tell people that they are wrong and stupid and horrible programmers and that should abandon what they are doing because they will never get it right and will only screw it up? Fine. I stopped caring about that. But I'm still going to try to be constructive.
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    What are you talking about? I'm suggesting to stay away from bad grabbed from random websites and instead write your own code or use a good library. Do you disagree with that? Is it unbecoming?

    To me, the fun of programming consists of learning, being creative and dealing with great tools. And I would like other people to have that same experience. You've stopped caring? Well, I do care.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    395
    Rep Power
    76
    Thank you very much Requinix for your positive reply and help. I am learning by working through the code and fixing things helps me learn. Predictable and often negative replies from others users is not productive. I hope some users here would not take questions so personal. If you have a negative comment, or disagree with someones approach , then ignore it and move on. I would rather my question go unanswered then get the usual negative reply.

    Comments on this post

    • ManiacDan agrees
    Last edited by mallen; December 17th, 2013 at 02:17 PM.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    395
    Rep Power
    76
    I have it working. Now I just need to figure out why it shows 5 characters. I changed the value and can't get it to show anything but 5 characters.

    PHP Code:
    session_start();
    error_reporting(E_ALL E_NOTICE);

    $image = @imagecreatetruecolor(12030) or die("Cannot Initialize new GD image stream");

    $background imagecolorallocate($image0x660x990x66);
    imagefill($image00$background);
    $linecolor imagecolorallocate($image0x990xCC0x99);
    $textcolor1 imagecolorallocate($image0x000x000x00);
    $textcolor2 imagecolorallocate($image0xFF0xFF0xFF);

    for(
    $i=0$i 3$i++) { imagesetthickness($imagerand(1,3));
    imageline($image0rand(0,30), 120rand(0,30) , $linecolor); }


    $string '';
    $total_characters 4;
    $random_dots 5;
    $font_size 16;
    $possible_letters '2345678abcdefghjkmnpqrtwyABCDEFGHJKMNPQRTWY';
    for (
    $i 0$i $total_characters$i++) { 
          
    $string .= $possible_letters[rand(0strlen($possible_letters) - 1)];
     }


    $fonts = array(); 
    $fonts[] = "fonts/font1.ttf";
    $fonts[] = "fonts/font2.ttf";
    $fonts[] = "fonts/font3.ttf";

    for( 
    $i=0$i<$random_dots$i++ ) 
    {
        
    imagefilledellipse($imagemt_rand(0,120),
         
    mt_rand(0,30), 23$image_noise_color);
    }


    session_start();
    $digit ''
    for(
    $x 15$x <= 95$x += 20) {
    $textcolor = (rand() % 2) ? $textcolor1 $textcolor2;
    $digit .= ($num $possible_letters[rand(0,44)]);
    imagettftext($image$font_sizerand(-20,20), $xrand(15,30), $textcolor$fonts[array_rand($fonts)], $num);}

    $_SESSION['digit'] = $digit;
    header('Content-type: image/png'); imagepng($image); imagedestroy($image);
    ?> 
  20. #11
  21. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    What did you change? $total_characters is still 4.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0
    What do you want to change your ($random_dots = 5 is 5 and your total characters is still 4 see it first ($total_characters = 4
  24. #13
  25. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by mallen
    If you have a negative comment, or disagree with someones approach , then ignore it and move on.
    You know, most people are here to get technical advice, improve their code and gain knowledge. It's called learning.

    But I understand you're looking for something else, so I'm sorry for bothering you. Have a good sleep.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    395
    Rep Power
    76
    I can change $total_characters to any number and it won't effect the total numbers and letters of the captcha that is created. It always displays 5 characters.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    395
    Rep Power
    76
    Ok I got it working. Its now displaying random fonts, two colors and I can change the length of the characters.

    PHP Code:
    session_start();
    error_reporting(E_ALL E_NOTICE);

    $image = @imagecreatetruecolor(12030) or die("Cannot Initialize new GD image stream");
    // set background and allocate drawing colours
    $background imagecolorallocate($image0x660x990x66);
    imagefill($image00$background);
    $linecolor imagecolorallocate($image0x990xCC0x99);
    $textcolor1 imagecolorallocate($image0x000x000x00);
    $textcolor2 imagecolorallocate($image0xFF0xFF0xFF);
    $lines 5;
    // draw random lines on canvas
    for($i=0$i $lines$i++) { imagesetthickness($imagerand(1,3));
    imageline($image0rand(0,30), 120rand(0,30) , $linecolor); }


    $string '';
    $total_characters 5;
    $random_dots 5;
    $font_size 16;
    $possible_letters '2345678abcdefghjkmnpqrtwyABCDEFGHJKMNPQRTWY';

     
    $fonts = array(); 
    $fonts[] = "fonts/font1.ttf";
    $fonts[] = "fonts/font2.ttf";
    $fonts[] = "fonts/font3.ttf";


    for( 
    $i=0$i<$random_dots$i++ ) 
    {
        
    imagefilledellipse($imagemt_rand(0,120),
        
    mt_rand(0,30), 23$image_noise_color);
    }

    session_start();
    $i 0;
    $digit '';
    for(
    $x 15$x <= 95$x += 20 ) {

    while (
    $i $total_characters ) { $i++; $x += 15;
    $textcolor = (rand() % 2) ? $textcolor1 $textcolor2;
    $digit .= ($num $possible_letters[rand(0,strlen($possible_letters)-1)]);

    imagettftext($image$font_size0$x20$textcolor$fonts[array_rand($fonts)], $num);}}

    $_SESSION['digit'] = $digit;  
    header('Content-type: image/png'); imagepng($image); imagedestroy($image); 
    Last edited by mallen; December 31st, 2013 at 01:56 PM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo