secimg.inc.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /*
  3. * This file is part of GBook - PHP Guestbook.
  4. *
  5. * (c) Copyright 2016 by Klemen Stirn. All rights reserved.
  6. * http://www.phpjunkyard.com
  7. * http://www.phpjunkyard.com/php-guestbook-script.php
  8. *
  9. * For the full copyright and license agreement information, please view
  10. * the docs/index.html file that was distributed with this source code.
  11. */
  12. /* Check if this is a valid include */
  13. if (!defined('IN_SCRIPT')) {
  14. die('Invalid attempt');
  15. }
  16. class PJ_SecurityImage
  17. {
  18. function __construct($key)
  19. {
  20. $this->code = '';
  21. $this->key = $key;
  22. }
  23. function encrypt($plain_text)
  24. {
  25. $this->code = trim(sha1($plain_text . $this->key));
  26. } // End encrypt
  27. function checkCode($mystring, $checksum)
  28. {
  29. $this->encrypt($mystring);
  30. if ($this->code == $checksum)
  31. return true;
  32. else
  33. return false;
  34. } // End checkCode
  35. function printImage($random_number)
  36. {
  37. $im = @imagecreate(150, 40) or die("Cannot Initialize new GD image stream");
  38. $background_color = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
  39. for ($i = 0; $i < strlen($random_number); $i++) {
  40. $text_color = imagecolorallocate($im, mt_rand(180, 255), mt_rand(180, 255), mt_rand(100, 255));
  41. $display = substr($random_number, $i, 1);
  42. $x = ($i * 30) + mt_rand(3, 16);
  43. $y = mt_rand(3, 26);
  44. imagestring($im, 5, $x, $y, $display, $text_color);
  45. }
  46. if (function_exists('imagejpeg')) {
  47. header("Content-type: image/jpeg");
  48. imagejpeg($im);
  49. } elseif (function_exists('imagepng')) {
  50. header("Content-type: image/png");
  51. imagepng($im);
  52. } elseif (function_exists('imagegif')) {
  53. header("Content-type: image/gif");
  54. imagegif($im);
  55. } else {
  56. die("GD was not compiled with JPEG or PNG support");
  57. }
  58. imagedestroy($im);
  59. } // End printImage
  60. function get()
  61. {
  62. return $this->code;
  63. } // End get
  64. } // End class PJ_SecurityImage