国内流行的内容管理系统(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.

175 lines
5.5KB

  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' => 14,
  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,3).'.ttf',
  21. 'wordlist_file' => DEDEINC.'/data/words/words.txt',
  22. 'filter_type' => 5);
  23. $enkey = substr(md5(substr($cfg_cookie_encode,0,5)),0,10);
  24. $sessSavePath = DEDEDATA."/sessions_{$enkey}";
  25. if ( !is_dir($sessSavePath) ) mkdir($sessSavePath);
  26. // Session保存路径
  27. if(is_writeable($sessSavePath) && is_readable($sessSavePath)){ session_save_path($sessSavePath); }
  28. if(!empty($cfg_domain_cookie)) session_set_cookie_params(0,'/',$cfg_domain_cookie);
  29. if (!echo_validate_image($config))
  30. {
  31. // 如果不成功则初始化一个默认验证码
  32. @session_start();
  33. $_SESSION['securimage_code_value'] = strtolower('abcd');
  34. $im = @imagecreatefromjpeg(dirname(__FILE__).'/data/vdcode.jpg');
  35. header("Pragma:no-cache\r\n");
  36. header("Cache-Control:no-cache\r\n");
  37. header("Expires:0\r\n");
  38. imagejpeg($im);
  39. imagedestroy($im);
  40. }
  41. function echo_validate_image( $config = array() )
  42. {
  43. @session_start();
  44. if ( !function_exists('imagettftext') )
  45. {
  46. return false;
  47. }
  48. //主要参数
  49. $font_size = isset($config['font_size']) ? $config['font_size'] : 14;
  50. $img_height = isset($config['img_height']) ? $config['img_height'] : 24;
  51. $img_width = isset($config['img_width']) ? $config['img_width'] : 68;
  52. $font_file = isset($config['font_file']) ? $config['font_file'] : PATH_DATA.'/data/font/'.mt_rand(1,3).'.ttf';
  53. $use_boder = isset($config['use_boder']) ? $config['use_boder'] : TRUE;
  54. $filter_type = isset($config['filter_type']) ? $config['filter_type'] : 0;
  55. //创建图片,并设置背景色
  56. $im = @imagecreate($img_width, $img_height);
  57. imagecolorallocate($im, 255,255,255);
  58. //文字随机颜色
  59. $fontColor[] = imagecolorallocate($im, 0x15, 0x15, 0x15);
  60. $fontColor[] = imagecolorallocate($im, 0x95, 0x1e, 0x04);
  61. $fontColor[] = imagecolorallocate($im, 0x93, 0x14, 0xa9);
  62. $fontColor[] = imagecolorallocate($im, 0x12, 0x81, 0x0a);
  63. $fontColor[] = imagecolorallocate($im, 0x06, 0x3a, 0xd5);
  64. //获取随机字符
  65. $rndstring = '';
  66. if ($config['word_type'] != 3)
  67. {
  68. for($i=0; $i<4; $i++)
  69. {
  70. if ($config['word_type'] == 1)
  71. {
  72. $c = chr(mt_rand(48, 57));
  73. } else if($config['word_type'] == 2)
  74. {
  75. $c = chr(mt_rand(65, 90));
  76. if( $c=='I' ) $c = 'P';
  77. if( $c=='O' ) $c = 'N';
  78. }
  79. $rndstring .= $c;
  80. }
  81. } else {
  82. $chars='abcdefghigklmnopqrstuvwxwyABCDEFGHIGKLMNOPQRSTUVWXWY0123456789';
  83. $rndstring='';
  84. $length = rand(4,4);
  85. $max = strlen($chars) - 1;
  86. for($i = 0; $i < $length; $i++) {
  87. $rndstring .= $chars[mt_rand(0, $max)];
  88. }
  89. }
  90. $_SESSION['securimage_code_value'] = strtolower($rndstring);
  91. $rndcodelen = strlen($rndstring);
  92. //背景横线
  93. $lineColor1 = imagecolorallocate($im, 0xda, 0xd9, 0xd1);
  94. for($j=3; $j<=$img_height-3; $j=$j+3)
  95. {
  96. imageline($im, 2, $j, $img_width - 2, $j, $lineColor1);
  97. }
  98. //背景竖线
  99. $lineColor2 = imagecolorallocate($im, 0xda,0xd9,0xd1);
  100. for($j=2;$j<100;$j=$j+6)
  101. {
  102. imageline($im, $j, 0, $j+8, $img_height, $lineColor2);
  103. }
  104. //画边框
  105. if( $use_boder && $filter_type == 0 )
  106. {
  107. $bordercolor = imagecolorallocate($im, 0x9d, 0x9e, 0x96);
  108. imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $bordercolor);
  109. }
  110. //输出文字
  111. $lastc = '';
  112. for($i=0;$i<$rndcodelen;$i++)
  113. {
  114. $bc = mt_rand(0, 1);
  115. $rndstring[$i] = strtoupper($rndstring[$i]);
  116. $c_fontColor = $fontColor[mt_rand(0,4)];
  117. $y_pos = $i==0 ? 4 : $i*($font_size+2);
  118. $c = mt_rand(0, 15);
  119. @imagettftext($im, $font_size, $c, $y_pos, 19, $c_fontColor, $font_file, $rndstring[$i]);
  120. $lastc = $rndstring[$i];
  121. }
  122. //图象效果
  123. switch($filter_type)
  124. {
  125. case 1:
  126. imagefilter ( $im, IMG_FILTER_NEGATE);
  127. break;
  128. case 2:
  129. imagefilter ( $im, IMG_FILTER_EMBOSS);
  130. break;
  131. case 3:
  132. imagefilter ( $im, IMG_FILTER_EDGEDETECT);
  133. break;
  134. default:
  135. break;
  136. }
  137. header("Pragma:no-cache\r\n");
  138. header("Cache-Control:no-cache\r\n");
  139. header("Expires:0\r\n");
  140. //输出特定类型的图片格式,优先级为 gif -> jpg ->png
  141. //dump(function_exists("imagejpeg"));
  142. if(function_exists("imagejpeg"))
  143. {
  144. header("content-type:image/jpeg\r\n");
  145. imagejpeg($im);
  146. }
  147. else
  148. {
  149. header("content-type:image/png\r\n");
  150. imagepng($im);
  151. }
  152. imagedestroy($im);
  153. exit();
  154. }