captcha.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2019
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. /**
  22. * captcha class
  23. *
  24. * @method string get
  25. */
  26. class captcha {
  27. /**
  28. * Called when the object is created
  29. */
  30. public $code;
  31. /**
  32. * Class constructor
  33. */
  34. public function __construct() {
  35. }
  36. /**
  37. * Called when there are no references to a particular object
  38. * unset the variables used in the class
  39. */
  40. public function __destruct() {
  41. foreach ($this as $key => $value) {
  42. unset($this->$key);
  43. }
  44. }
  45. /**
  46. * Create the captcha image
  47. * @var string $code
  48. */
  49. public function image_captcha() {
  50. //includes
  51. include "root.php";
  52. require_once "config.php";
  53. require_once "resources/functions.php";
  54. error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ); //hide notices and warnings
  55. //start the session
  56. ini_set("session.cookie_httponly", True);
  57. if (!isset($_SESSION)) { session_start(); }
  58. //$_SESSION["captcha"] = substr(md5(uuid()), 0, 6);
  59. //$text = $_SESSION["captcha"];
  60. $text = $this->code;
  61. // Set the font path
  62. $font_path = $_SERVER["DOCUMENT_ROOT"]."/resources/captcha/fonts";
  63. // Array of fonts
  64. //$fonts[] = 'ROUGD.TTF';
  65. //$fonts[] = 'Zebra.ttf';
  66. //$fonts[] = 'hanshand.ttf';
  67. $fonts = glob($font_path.'/*.[tT][tT][fF]');
  68. //print_r($fonts);
  69. //exit;
  70. // Randomize the fonts
  71. srand(uuid());
  72. $random = (rand()%count($fonts));
  73. //$font = $font_path.'/'.$fonts[$random];
  74. $font = $fonts[$random];
  75. // Set the font size
  76. $font_size = 16;
  77. if(@$_GET['fontsize']) {
  78. $font_size = $_GET['fontsize'];
  79. }
  80. // Create the image
  81. $size = $this->image_size($font_size, 0, $font, $text);
  82. $width = $size[2] + $size[0] + 8;
  83. $height = abs($size[1]) + abs($size[7]);
  84. //$width = 100;
  85. //$height = 40;
  86. // Set the image size
  87. $image = imagecreate($width, $height);
  88. // Create some colors
  89. $white = imagecolorallocate($image, 255, 255, 255);
  90. $black = imagecolorallocate($image, 0, 0, 0);
  91. // Set the transparent color
  92. imagecolortransparent($image, $white);
  93. // Add the text
  94. imagefttext($image, $font_size, 0, 0, abs($size[5]), $black, $font, $text);
  95. // Set the content-type
  96. //header("Content-type: image/png");
  97. //imagepng($image));
  98. ob_start();
  99. imagepng($image);
  100. $image_buffer = ob_get_clean();
  101. //echo "<img src=\"data:image/png;base64, ".base64_encode($image_buffer)."\" />\n";
  102. imagedestroy($image);
  103. return $image_buffer;
  104. }
  105. /**
  106. * return the image in base64
  107. */
  108. public function image_base64() {
  109. return base64_encode($this->image_captcha());
  110. }
  111. /**
  112. * Get the image size
  113. * @var string $value string image size
  114. */
  115. private function image_size($size, $angle, $font, $text) {
  116. $dummy = imagecreate(1, 1);
  117. $black = imagecolorallocate($dummy, 0, 0, 0);
  118. $bbox = imagettftext($dummy, $size, $angle, 0, 0, $black, $font, $text);
  119. imagedestroy($dummy);
  120. return $bbox;
  121. }
  122. }
  123. /*
  124. $captcha = new captcha;
  125. $captcha->code = 'abcdefg';
  126. $image_base64 = $captcha->base64();
  127. echo "<img src=\"data:image/png;base64, ".$image_base64."\" />\n";
  128. */
  129. ?>