PageRenderTime 91ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/wigit/imagebuilder.php

https://bitbucket.org/gopchu/gopchu.org
PHP | 161 lines | 85 code | 22 blank | 54 comment | 10 complexity | 966376755e1845ff5cb7fb6d9300ed96 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. ============================
  4. QuickCaptcha 1.0 - A bot-thwarting text-in-image web tool.
  5. Copyright (c) 2006 Web 1 Marketing, Inc.
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. ============================
  15. See settings.php for common settings. You shouldn't need to change
  16. anything in this file.
  17. ============================
  18. */
  19. // This string contains allowable characters for the image.
  20. // To reduce confusion, zero and the letter 'o' have been removed,
  21. // and QuickCaptcha is NOT case-sensitive.
  22. $acceptedChars = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
  23. // Number of characters in image.
  24. $stringlength = 5;
  25. // A value between 0 and 100 describing how much color overlap
  26. // there is between text and other objects. Lower is more
  27. // secure against bots, but also harder to read.
  28. $contrast = 60;
  29. // Various obfuscation techniques.
  30. $num_polygons = 3; // Number of triangles to draw. 0 = none
  31. $num_ellipses = 6; // Number of ellipses to draw. 0 = none
  32. $num_lines = 0; // Number of lines to draw. 0 = none
  33. $num_dots = 0; // Number of dots to draw. 0 = none
  34. $min_thickness = 2; // Minimum thickness in pixels of lines
  35. $max_thickness = 8; // Maximum thickness in pixles of lines
  36. $min_radius = 5; // Minimum radius in pixels of ellipses
  37. $max_radius = 15; // Maximum radius in pixels of ellipses
  38. // How opaque should the obscuring objects be. 0 is opaque, 127
  39. // is transparent.
  40. $object_alpha = 75;
  41. // Keep #'s reasonable.
  42. $min_thickness = max(1,$min_thickness);
  43. $max_thickness = min(20,$max_thickness);
  44. // Make radii into height/width
  45. $min_radius *= 2;
  46. $max_radius *= 2;
  47. // Renormalize contrast
  48. $contrast = 255 * ($contrast / 100.0);
  49. $o_contrast = 1.3 * $contrast;
  50. $width = 15 * imagefontwidth (5);
  51. $height = 2.5 * imagefontheight (5);
  52. $image = imagecreatetruecolor ($width, $height);
  53. imagealphablending($image, true);
  54. $black = imagecolorallocatealpha($image,0,0,0,0);
  55. // Build the validation string
  56. $max = strlen($acceptedChars)-1;
  57. $password = NULL;
  58. for($i=0; $i < $stringlength; $i++) {
  59. $cnum[$i] = $acceptedChars{mt_rand(0, $max)};
  60. $password .= $cnum[$i];
  61. }
  62. // Add string to image
  63. $rotated = imagecreatetruecolor (70, 70);
  64. $x = 0;
  65. for ($i = 0; $i < $stringlength; $i++) {
  66. $buffer = imagecreatetruecolor (20, 20);
  67. $buffer2 = imagecreatetruecolor (40, 40);
  68. // Get a random color
  69. $red = mt_rand(0,255);
  70. $green = mt_rand(0,255);
  71. $blue = 255 - sqrt($red * $red + $green * $green);
  72. $color = imagecolorallocate ($buffer, $red, $green, $blue);
  73. // Create character
  74. imagestring($buffer, 5, 0, 0, $cnum[$i], $color);
  75. // Resize character
  76. imagecopyresized ($buffer2, $buffer, 0, 0, 0, 0, 25 + mt_rand(0,12), 25 + mt_rand(0,12), 20, 20);
  77. /*
  78. // Rotate characters a little
  79. $rotated = imagerotate($buffer2, mt_rand(-25, 25),imagecolorallocatealpha($buffer2,0,0,0,0));
  80. imagecolortransparent ($rotated, imagecolorallocatealpha($rotated,0,0,0,0));
  81. // Move characters around a little
  82. $y = mt_rand(1, 3);
  83. $x += mt_rand(2, 6);
  84. imagecopymerge ($image, $rotated, $x, $y, 0, 0, 40, 40, 100);
  85. $x += 22;
  86. */
  87. imagecolortransparent ($buffer2, imagecolorallocatealpha($buffer2,0,0,0,0));
  88. // Move characters around a little
  89. $y = mt_rand(1, 3);
  90. $x += mt_rand(2, 6);
  91. imagecopymerge ($image, $buffer2, $x, $y, 0, 0, 40, 40, 100);
  92. $x += 22;
  93. imagedestroy ($buffer);
  94. imagedestroy ($buffer2);
  95. }
  96. // Draw polygons
  97. if ($num_polygons > 0) for ($i = 0; $i < $num_polygons; $i++) {
  98. $vertices = array (
  99. mt_rand(-0.25*$width,$width*1.25),mt_rand(-0.25*$width,$width*1.25),
  100. mt_rand(-0.25*$width,$width*1.25),mt_rand(-0.25*$width,$width*1.25),
  101. mt_rand(-0.25*$width,$width*1.25),mt_rand(-0.25*$width,$width*1.25)
  102. );
  103. $color = imagecolorallocatealpha ($image, mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), $object_alpha);
  104. imagefilledpolygon($image, $vertices, 3, $color);
  105. }
  106. // Draw random circles
  107. if ($num_ellipses > 0) for ($i = 0; $i < $num_ellipses; $i++) {
  108. $x1 = mt_rand(0,$width);
  109. $y1 = mt_rand(0,$height);
  110. $color = imagecolorallocatealpha ($image, mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), $object_alpha);
  111. // $color = imagecolorallocate($image, mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), mt_rand(0,$o_contrast));
  112. imagefilledellipse($image, $x1, $y1, mt_rand($min_radius,$max_radius), mt_rand($min_radius,$max_radius), $color);
  113. }
  114. // Draw random lines
  115. if ($num_lines > 0) for ($i = 0; $i < $num_lines; $i++) {
  116. $x1 = mt_rand(-$width*0.25,$width*1.25);
  117. $y1 = mt_rand(-$height*0.25,$height*1.25);
  118. $x2 = mt_rand(-$width*0.25,$width*1.25);
  119. $y2 = mt_rand(-$height*0.25,$height*1.25);
  120. $color = imagecolorallocatealpha ($image, mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), $object_alpha);
  121. imagesetthickness ($image, mt_rand($min_thickness,$max_thickness));
  122. imageline($image, $x1, $y1, $x2, $y2 , $color);
  123. }
  124. // Draw random dots
  125. if ($num_dots > 0) for ($i = 0; $i < $num_dots; $i++) {
  126. $x1 = mt_rand(0,$width);
  127. $y1 = mt_rand(0,$height);
  128. $color = imagecolorallocatealpha ($image, mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), mt_rand(0,$o_contrast),$object_alpha);
  129. imagesetpixel($image, $x1, $y1, $color);
  130. }
  131. session_start();
  132. $_SESSION['capstring'] = $password;
  133. header('Content-type: image/png');
  134. imagepng($image);
  135. imagedestroy($image);
  136. ?>