国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

186 linhas
5.9KB

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