PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/captcha/CaptchaSecurityImages.php

http://github.com/Mythos94/Minimanager-for-TrinityCore
PHP | 97 lines | 40 code | 13 blank | 44 comment | 4 complexity | c528156d4f7edc24185dccacba3d176d MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (C) 2010-2011 TrinityScripts <http://www.trinityscripts.xe.cx/>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  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. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. session_start();
  20. /*
  21. * File: CaptchaSecurityImages.php
  22. * Author: Simon Jarvis
  23. * Copyright: 2006 Simon Jarvis
  24. * Date: 03/08/06
  25. * Updated: 07/02/07
  26. * Requirements: PHP 4/5 with GD and FreeType libraries
  27. * Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
  28. *
  29. * This program is free software; you can redistribute it and/or
  30. * modify it under the terms of the GNU General Public License
  31. * as published by the Free Software Foundation; either version 2
  32. * of the License, or (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details:
  38. * http://www.gnu.org/licenses/gpl.html
  39. */
  40. class CaptchaSecurityImages
  41. {
  42. var $font = 'monofont.ttf';
  43. function generateCode($characters)
  44. {
  45. /* list all possible characters, similar looking characters and vowels have been removed */
  46. $possible = '23456789bcdfghjkmnpqrstvwxyz';
  47. $code = '';
  48. for($i=0; $i<$characters; ++$i)
  49. $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
  50. return $code;
  51. }
  52. function CaptchaSecurityImages($width='350', $height='130', $characters='6')
  53. {
  54. $code = $this->generateCode($characters);
  55. /* font size will be 75% of the image height */
  56. $font_size = $height * 0.75;
  57. $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
  58. /* set the colours */
  59. $background_color = imagecolorallocate($image, 20, 20, 20);
  60. $text_color = imagecolorallocate($image, 215, 235, 255);
  61. $noise_color = imagecolorallocate($image, 155, 175, 195);
  62. /* generate random dots in background */
  63. for($i=0; $i<($width*$height)/3; ++$i)
  64. imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
  65. /* generate random lines in background */
  66. for($i=0; $i<($width*$height)/150; ++$i)
  67. imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
  68. /* create textbox and add text */
  69. $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
  70. $x = ($width - $textbox[4])/2;
  71. $y = ($height - $textbox[5])/2;
  72. imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
  73. /* output captcha image to browser */
  74. header('Content-Type: image/jpeg');
  75. imagejpeg($image);
  76. imagedestroy($image);
  77. $_SESSION['security_code'] = $code;
  78. }
  79. }
  80. $width = isset($_GET['width']) ? $_GET['width'] : '350';
  81. $height = isset($_GET['height']) ? $_GET['height'] : '130';
  82. $characters = isset($_GET['characters']) && 1 < $_GET['characters'] ? $_GET['characters'] : '8';
  83. $captcha = new CaptchaSecurityImages($width, $height, $characters);
  84. ?>