Posted By

Znupi on 08/20/09


Tagged

image Captcha


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

vali29
nreliu
umang_nine


Captcha


 / Published in: PHP
 

URL: http://it-ride.blogspot.com/2009/08/captcha.html

Please visit the URL of this snippet to see how these captchas look.

  1. <?php
  2.  
  3. $W = 160; // width
  4. $H = 60; // height
  5. $L = 6; // length of the key
  6. $BG = 'light'; // can be 'light' or 'dark', accorting to the background color of
  7. // the page it will be on
  8. $F = './DejaVuSans.ttf'; // path to true-type font file
  9.  
  10. function makeKey($length) {
  11. // generate a random sequence of characters
  12. $a = 'abcdefghijklmnopqrstuvwxyz';
  13. $s = '';
  14. for ($i=0; $i < $length; $i++) {
  15. $s .= $a[mt_rand(0, strlen($a) - 1)];
  16. }
  17. return $s;
  18. }
  19.  
  20. $img = imagecreatetruecolor($W, $H);
  21. // make the image alpha-aware
  22. imagesavealpha($img, true);
  23. // make colors 'blend', not overwrite
  24. imagealphablending($img, true);
  25. // make the image transparent
  26. imagefill($img, 1, 1, imagecolorallocatealpha($img, 0, 0, 0, 127));
  27.  
  28. // generate two random colors and decide which one goes where
  29. $dark = Array (mt_rand(0, 126), mt_rand(0, 126), mt_rand(0, 126));
  30. $light = Array (mt_rand(127, 255), mt_rand(127, 255), mt_rand(127, 255));
  31. if ($BG == 'dark') {
  32. $bg_color = imagecolorallocatealpha($img, $dark[0], $dark[1], $dark[2], mt_rand(64, 96));
  33. $fg_color = imagecolorallocatealpha($img, $light[0], $light[1], $light[2], mt_rand(32, 64));
  34. }
  35. else {
  36. $bg_color = imagecolorallocatealpha($img, $light[0], $light[1], $light[2], mt_rand(64, 96));
  37. $fg_color = imagecolorallocatealpha($img, $dark[0], $dark[1], $dark[2], mt_rand(32, 64));
  38. }
  39.  
  40. // write background static
  41. $angle = mt_rand(20, 35);
  42. for ($i=0; $i < 15; $i++) {
  43. imagettftext($img, 12, $angle, 0, $i*15, $bg_color, $F, makeKey(30));
  44. }
  45.  
  46. $key = makeKey($L); // you should store this in the user session to check it later
  47.  
  48. // write the actual key, in two parts
  49. imagettftext($img, mt_rand(16, 22), mt_rand(10, 30), mt_rand(5, 30), mt_rand($H-16, $H-22), $fg_color, $F, substr($key, 0, 3));
  50. imagettftext($img, mt_rand(16, 22), mt_rand(-30, -10), mt_rand($W/2+5, $W/2+30), mt_rand(16, 22), $fg_color, $F, substr($key, 3, 3));
  51.  
  52. // output the image
  53. header("Content-Type: image/png");
  54. imagepng($img);
  55.  
  56. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Znupi on August 20, 2009

I'm new to Snipplr and just posted this. I'm curious as to why it didn't preserve indentation? Did I do something wrong?

Posted By: Znupi on August 20, 2009

Sorry, I copied and pasted it from my blog and Firefox seems to remove indentation when copying for some strange reason.

Posted By: noticz on February 7, 2010

Ok I didn't like this at all when I first looked at it but it honestly is the image captcha I have found and not to mention simple. Take the above code and place it in it's own file lets say captcha.php. On the form that you want to use it on just link to it like and make sure you add sessionstart(); to the above file and the form page. Also I would add the $SESSION['captcha_key'] = $key; under the $key variable and that is about it.

Znupi, sorry about the email figured I would post this so you didn't have to :)

You need to login to post a comment.