用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1492|回复: 1
收起左侧

[HELP] 验证码类怎么修改都无法改变字体大小

[复制链接]
发表于 2019-9-4 21:03:56 | 显示全部楼层 |阅读模式
  1.         #生成验证码
  2.         public function code(){
  3.                 #调用函数生成验证码
  4.                 $font_path = base_url('public/fonts/consola.ttf');
  5.                 $vals = array(

  6.                         'img_width' => '150',
  7.                     'img_height'    => 60,
  8.                     'font_size' => 32,
  9.                     'font_path' =>$font_path,

  10.                 );
  11.                 $code = create_captcha($vals);
  12.                 #将验证码字符串保存到session中
  13.                 $this->session->set_userdata('code',$code);
  14.         }
复制代码
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package                CodeIgniter
  8. * @author                ExpressionEngine Dev Team
  9. * @copyright        Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license                http://codeigniter.com/user_guide/license.html
  11. * @link                http://codeigniter.com
  12. * @since                Version 1.0
  13. * @filesource
  14. */

  15. // ------------------------------------------------------------------------

  16. /**
  17. * CodeIgniter CAPTCHA Helper
  18. *
  19. * @package                CodeIgniter
  20. * @subpackage        Helpers
  21. * @category        Helpers
  22. * @author                ExpressionEngine Dev Team
  23. * @link                http://codeigniter.com/user_guide/helpers/xml_helper.html
  24. */

  25. // ------------------------------------------------------------------------

  26. /**
  27. * Create CAPTCHA
  28. *
  29. * @access        public
  30. * @param        array        array of data for the CAPTCHA
  31. * @param        string        path to create the image in
  32. * @param        string        URL to the CAPTCHA image folder
  33. * @param        string        server path to font
  34. * @return        string
  35. */
  36. if ( ! function_exists('create_captcha'))
  37. {
  38.         function create_captcha($data = '', $font_path = '',$font_size='16')
  39.         {
  40.                 $defaults = array('word' => '', 'word_length' => 4, 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);

  41.                 foreach ($defaults as $key => $val)
  42.                 {
  43.                         if ( ! is_array($data))
  44.                         {
  45.                                 if ( ! isset($key) OR $key == '')
  46.                                 {
  47.                                         $key = $val;
  48.                                 }
  49.                         }
  50.                         else
  51.                         {
  52.                                 $key = ( ! isset($data[$key])) ? $val : $data[$key];
  53.                         }
  54.                 }

  55.            if ($word == '')
  56.            {
  57.                         $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

  58.                         $str = '';
  59.                         for ($i = 0; $i < $word_length; $i++)
  60.                         {
  61.                                 $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  62.                         }

  63.                         $word = $str;
  64.            }

  65.                 // -----------------------------------
  66.                 // Determine angle and position
  67.                 // -----------------------------------

  68.                 $length        = strlen($word);
  69.                 $angle        = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
  70.                 $x_axis        = rand(6, (360/$length)-16);
  71.                 $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);

  72.                 // -----------------------------------
  73.                 // Create image
  74.                 // -----------------------------------

  75.                 // PHP.net recommends imagecreatetruecolor(), but it isn't always available
  76.                 if (function_exists('imagecreatetruecolor'))
  77.                 {
  78.                         $im = imagecreatetruecolor($img_width, $img_height);
  79.                 }
  80.                 else
  81.                 {
  82.                         $im = imagecreate($img_width, $img_height);
  83.                 }

  84.                 // -----------------------------------
  85.                 //  Assign colors
  86.                 // -----------------------------------

  87.                 $bg_color                = imagecolorallocate ($im, 255, 255, 255);
  88.                 $border_color        = imagecolorallocate ($im, 153, 102, 102);
  89.                 $text_color                = imagecolorallocate ($im, 204, 153, 153);
  90.                 $grid_color                = imagecolorallocate($im, 255, 182, 182);
  91.                 $shadow_color        = imagecolorallocate($im, 255, 240, 240);

  92.                 // -----------------------------------
  93.                 //  Create the rectangle
  94.                 // -----------------------------------

  95.                 ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);

  96.                 // -----------------------------------
  97.                 //  Create the spiral pattern
  98.                 // -----------------------------------

  99.                 $theta                = 1;
  100.                 $thetac                = 7;
  101.                 $radius                = 16;
  102.                 $circles        = 20;
  103.                 $points                = 32;

  104.                 for ($i = 0; $i < ($circles * $points) - 1; $i++)
  105.                 {
  106.                         $theta = $theta + $thetac;
  107.                         $rad = $radius * ($i / $points );
  108.                         $x = ($rad * cos($theta)) + $x_axis;
  109.                         $y = ($rad * sin($theta)) + $y_axis;
  110.                         $theta = $theta + $thetac;
  111.                         $rad1 = $radius * (($i + 1) / $points);
  112.                         $x1 = ($rad1 * cos($theta)) + $x_axis;
  113.                         $y1 = ($rad1 * sin($theta )) + $y_axis;
  114.                         imageline($im, $x, $y, $x1, $y1, $grid_color);
  115.                         $theta = $theta - $thetac;
  116.                 }

  117.                 // -----------------------------------
  118.                 //  Write the text
  119.                 // -----------------------------------

  120.                 $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;

  121.                 if ($use_font == FALSE)
  122.                 {
  123.                         $font_size = 5;
  124.                         $x = rand(0, $img_width/($length/3));
  125.                         $y = 0;
  126.                 }
  127.                 else
  128.                 {
  129.                        
  130.                         $x = rand(0, $img_width/($length/1.5));
  131.                         $y = $font_size+2;
  132.                 }

  133.                 for ($i = 0; $i < strlen($word); $i++)
  134.                 {
  135.                         if ($use_font == FALSE)
  136.                         {
  137.                                 $y = rand(0 , $img_height/2);
  138.                                 imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
  139.                                 $x += ($font_size*2);
  140.                         }
  141.                         else
  142.                         {
  143.                                 $y = rand($img_height/2, $img_height-3);
  144.                                 imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
  145.                                 //imagestring ( $im, $font_size,$x , $y , substr($word, $i, 1),  $text_color );
  146.                                 $x += $font_size;
  147.                         }
  148.                 }


  149.                 // -----------------------------------
  150.                 //  Create the border
  151.                 // -----------------------------------

  152.                 imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);

  153.                 // -----------------------------------
  154.                 //  Generate the image
  155.                 // -----------------------------------

  156.                 // $img_name = $now.'.jpg';

  157.                 // ImageJPEG($im, $img_path.$img_name);

  158.                 // $img = "<img src="$img_url$img_name" width="$img_width" height="$img_height" style="border:0;" alt=" " />";
  159.                 #直接输出
  160.                 header("Content-Type:image/jpeg");
  161.                 imagejpeg($im);

  162.                 ImageDestroy($im);
  163.                 #返回生成的验证码字符串
  164.                 return $word;
  165.                 // return array('word' => $word, 'time' => $now, 'image' => $img);
  166.         }
  167. }

  168. // ------------------------------------------------------------------------

  169. /* End of file captcha_helper.php */
  170. /* Location: ./system/heleprs/captcha_helper.php */
复制代码


我修改了一下图片显示,不让他生成图片,直接输出
另外发现里面的函数根本就没传入字体大小的参数,给加上了
可还是不行。
我知道有人用其他第三方验证码类,我只是研究一下这个是哪里出了问题
我也用了其他字体

发表于 2019-9-26 21:44:20 | 显示全部楼层
这种验证码太老了,建议使用 新的验证方式, 比如像本站的,腾讯的都行

本版积分规则