国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.8KB

  1. <?php
  2. /**
  3. * 验证图片
  4. *
  5. * @version $Id: vdimgck.php 1 15:21 2010年7月5日 tianya $
  6. * @package DedeCMS.Libraries
  7. * @copyright Copyright (c) 2007 - 2020, DesDev, Inc.
  8. * @license http://help.dedecms.com/usersguide/license.html
  9. * @link http://www.dedecms.com
  10. */
  11. require_once(dirname(__FILE__) . '/../include/common.inc.php');
  12. require_once(DEDEDATA . '/safe/inc_safe_config.php');
  13. require_once(DEDEDATA . '/config.cache.inc.php');
  14. $config = array(
  15. 'font_size' => 20,
  16. 'img_height' => $safe_wheight,
  17. 'word_type' => (int)$safe_codetype, // 1:数字 2:英文 3:单词
  18. 'img_width' => $safe_wwidth,
  19. 'use_boder' => TRUE,
  20. 'font_file' => DEDEINC . '/data/fonts/' . mt_rand(1, 6) . '.ttf',
  21. 'wordlist_file' => DEDEINC . '/data/words/words.txt',
  22. 'filter_type' => 5
  23. );
  24. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  25. $sessSavePath = DEDEDATA . "/sessions_{$enkey}";
  26. if (!is_dir($sessSavePath)) mkdir($sessSavePath);
  27. // Session保存路径
  28. if (is_writeable($sessSavePath) && is_readable($sessSavePath)) {
  29. session_save_path($sessSavePath);
  30. }
  31. if (!empty($cfg_domain_cookie)) session_set_cookie_params(0, '/', $cfg_domain_cookie);
  32. if (!echo_validate_image($config)) {
  33. // 如果不成功则初始化一个默认验证码
  34. @session_start();
  35. $_SESSION['securimage_code_value'] = strtolower('abcd');
  36. if (function_exists('imagecreatefromjpeg')) {
  37. $im = @imagecreatefromjpeg(DEDEINC . '/data/vdcode.jpg');
  38. header("Pragma:no-cache\r\n");
  39. header("Cache-Control:no-cache\r\n");
  40. header("Expires:0\r\n");
  41. imagejpeg($im);
  42. imagedestroy($im);
  43. } else {
  44. header("Pragma:no-cache\r\n");
  45. header("Cache-Control:no-cache\r\n");
  46. header("Expires:0\r\n");
  47. $c = file_get_contents(DEDEINC . '/data/vdcode.jpg', true);
  48. $size = filesize(DEDEINC . '/data/vdcode.jpg');
  49. header('Content-Type: image/x-icon');
  50. header("Content-length: $size");
  51. echo $c;
  52. }
  53. }
  54. function echo_validate_image($config = array())
  55. {
  56. @session_start();
  57. if (!function_exists('imagettftext')) {
  58. return false;
  59. }
  60. //主要参数
  61. $font_size = isset($config['font_size']) ? $config['font_size'] : 14;
  62. $img_height = isset($config['img_height']) ? $config['img_height'] : 38;
  63. $img_width = isset($config['img_width']) ? $config['img_width'] : 68;
  64. $font_file = isset($config['font_file']) ? $config['font_file'] : DEDEINC . '/data/font/' . mt_rand(1, 6) . '.ttf';
  65. $use_boder = isset($config['use_boder']) ? $config['use_boder'] : TRUE;
  66. $filter_type = isset($config['filter_type']) ? $config['filter_type'] : 0;
  67. //创建图片,并设置背景色
  68. $im = @imagecreate($img_width, $img_height);
  69. imagecolorallocate($im, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
  70. //文字随机颜色
  71. $fontColor[] = imagecolorallocate($im, 0x15, 0x15, 0x15);
  72. $fontColor[] = imagecolorallocate($im, 0x95, 0x1e, 0x04);
  73. $fontColor[] = imagecolorallocate($im, 0x93, 0x14, 0xa9);
  74. $fontColor[] = imagecolorallocate($im, 0x12, 0x81, 0x0a);
  75. $fontColor[] = imagecolorallocate($im, 0x06, 0x3a, 0xd5);
  76. //获取随机字符
  77. $rndstring = '';
  78. if ($config['word_type'] != 3) {
  79. for ($i = 0; $i < 4; $i++) {
  80. if ($config['word_type'] == 1) {
  81. $c = chr(mt_rand(48, 57));
  82. } else if ($config['word_type'] == 2) {
  83. $c = chr(mt_rand(65, 90));
  84. if ($c == 'I') $c = 'P';
  85. if ($c == 'O') $c = 'N';
  86. }
  87. $rndstring .= $c;
  88. }
  89. } else {
  90. $chars = 'abcdefghigklmnopqrstuvwxwyABCDEFGHIGKLMNOPQRSTUVWXWY0123456789';
  91. $rndstring = '';
  92. $length = rand(4, 4);
  93. $max = strlen($chars) - 1;
  94. for ($i = 0; $i < $length; $i++) {
  95. $rndstring .= $chars[mt_rand(0, $max)];
  96. }
  97. }
  98. $_SESSION['securimage_code_value'] = strtolower($rndstring);
  99. $rndcodelen = strlen($rndstring);
  100. // //背景横线
  101. // $lineColor1 = imagecolorallocate($im, 0xda, 0xd9, 0xd1);
  102. // for ($j = 3; $j <= $img_height - 3; $j = $j + 3) {
  103. // imageline($im, 2, $j, $img_width - 2, $j, $lineColor1);
  104. // }
  105. // //背景竖线
  106. // $lineColor2 = imagecolorallocate($im, 0xda, 0xd9, 0xd1);
  107. // for ($j = 2; $j < 100; $j = $j + 6) {
  108. // imageline($im, $j, 0, $j + 8, $img_height, $lineColor2);
  109. // }
  110. // 增加一些噪线
  111. for ($i=0; $i < 5; $i++) {
  112. $red = mt_rand(50, 255);
  113. $green = mt_rand(50, 255);
  114. $blue = mt_rand(50, 255);
  115. $tcol = imagecolorallocate($im, $red, $green, $blue);
  116. if (mt_rand(0, 1)) { // Horizontal
  117. $Xa = mt_rand(0, $img_width/2);
  118. $Ya = mt_rand(0, $img_height);
  119. $Xb = mt_rand($img_width/2, $img_width);
  120. $Yb = mt_rand(0, $img_height);
  121. } else { // Vertical
  122. $Xa = mt_rand(0, $img_width);
  123. $Ya = mt_rand(0, $img_height/2);
  124. $Xb = mt_rand(0, $img_width);
  125. $Yb = mt_rand($img_height/2, $img_height);
  126. }
  127. imagesetthickness($im, mt_rand(1, 3));
  128. imageline($im, $Xa, $Ya, $Xb, $Yb, $tcol);
  129. }
  130. //画边框
  131. if ($use_boder && $filter_type == 0) {
  132. $bordercolor = imagecolorallocate($im, 0x9d, 0x9e, 0x96);
  133. imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $bordercolor);
  134. }
  135. //输出文字
  136. $lastc = '';
  137. for ($i = 0; $i < $rndcodelen; $i++) {
  138. $rndstring[$i] = strtoupper($rndstring[$i]);
  139. $c_fontColor = $fontColor[mt_rand(0, 4)];
  140. $y_pos = $i == 0 ? 4 : $i * ($font_size + 2);
  141. $c = mt_rand(10, 30);
  142. @imagettftext($im, $font_size, $c, $y_pos, 28, $c_fontColor, $font_file, $rndstring[$i]);
  143. $lastc = $rndstring[$i];
  144. }
  145. //图象效果
  146. switch ($filter_type) {
  147. case 1:
  148. imagefilter($im, IMG_FILTER_NEGATE);
  149. break;
  150. case 2:
  151. imagefilter($im, IMG_FILTER_EMBOSS);
  152. break;
  153. case 3:
  154. imagefilter($im, IMG_FILTER_EDGEDETECT);
  155. break;
  156. default:
  157. break;
  158. }
  159. header("Pragma:no-cache\r\n");
  160. header("Cache-Control:no-cache\r\n");
  161. header("Expires:0\r\n");
  162. //输出特定类型的图片格式,优先级为 gif -> jpg ->png
  163. //dump(function_exists("imagejpeg"));
  164. if (function_exists("imagejpeg")) {
  165. header("content-type:image/jpeg\r\n");
  166. imagejpeg($im);
  167. } else {
  168. header("content-type:image/png\r\n");
  169. imagepng($im);
  170. }
  171. imagedestroy($im);
  172. exit();
  173. }