captcha.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * Create the captcha image
  38. * @var string $code
  39. */
  40. public function image_captcha() {
  41. //includes files
  42. require dirname(__DIR__, 2) . "/resources/require.php";
  43. //includes files
  44. require_once "resources/functions.php";
  45. //start the session
  46. if (!isset($_SESSION)) { session_start(); }
  47. //$_SESSION["captcha"] = substr(md5(uuid()), 0, 6);
  48. //$text = $_SESSION["captcha"];
  49. $text = $this->code;
  50. // Set the font path
  51. $font_path = $_SERVER["DOCUMENT_ROOT"]."/resources/captcha/fonts";
  52. // Array of fonts
  53. //$fonts[] = 'ROUGD.TTF';
  54. //$fonts[] = 'Zebra.ttf';
  55. //$fonts[] = 'hanshand.ttf';
  56. $fonts = glob($font_path.'/*.[tT][tT][fF]');
  57. //print_r($fonts);
  58. //exit;
  59. // Randomize the fonts
  60. srand();
  61. $random = (rand()%count($fonts));
  62. //$font = $font_path.'/'.$fonts[$random];
  63. $font = $fonts[$random];
  64. // Set the font size
  65. $font_size = 16;
  66. if(@$_GET['fontsize']) {
  67. $font_size = $_GET['fontsize'];
  68. }
  69. // Create the image
  70. $size = $this->image_size($font_size, 0, $font, $text);
  71. $width = $size[2] + $size[0] + 8;
  72. $height = abs($size[1]) + abs($size[7]);
  73. //$width = 100;
  74. //$height = 40;
  75. // Set the image size
  76. $image = imagecreate($width, $height);
  77. // Create some colors
  78. $white = imagecolorallocate($image, 255, 255, 255);
  79. $black = imagecolorallocate($image, 0, 0, 0);
  80. // Set the transparent color
  81. imagecolortransparent($image, $white);
  82. // Add the text
  83. imagefttext($image, $font_size, 0, 0, abs($size[5]), $black, $font, $text);
  84. // Set the content-type
  85. //header("Content-type: image/png");
  86. //imagepng($image));
  87. ob_start();
  88. imagepng($image);
  89. $image_buffer = ob_get_clean();
  90. //echo "<img src=\"data:image/png;base64, ".base64_encode($image_buffer)."\" />\n";
  91. imagedestroy($image);
  92. return $image_buffer;
  93. }
  94. /**
  95. * return the image in base64
  96. */
  97. public function image_base64() {
  98. return base64_encode($this->image_captcha());
  99. }
  100. /**
  101. * Get the image size
  102. * @var string $value string image size
  103. */
  104. private function image_size($size, $angle, $font, $text) {
  105. $dummy = imagecreate(1, 1);
  106. $black = imagecolorallocate($dummy, 0, 0, 0);
  107. $bbox = imagettftext($dummy, $size, $angle, 0, 0, $black, $font, $text);
  108. imagedestroy($dummy);
  109. return $bbox;
  110. }
  111. }
  112. /*
  113. $captcha = new captcha;
  114. $captcha->code = 'abcdefg';
  115. $image_base64 = $captcha->base64();
  116. echo "<img src=\"data:image/png;base64, ".$image_base64."\" />\n";
  117. */
  118. ?>