在codeigniter中的验证码

时间:2014-06-17 07:03:24

标签: php codeigniter captcha

您好我在codeigniter中生成验证码。我在生成验证码时遇到问题。我在控制器内部使用验证码,但是在我错了的情况下会出错。

这是我的控制器代码。

public function captcha(){

        //$this->load->library('monofont.ttf');

        $image_width = 120;
        $image_height = 40;
        $characters_on_image = 6;
        //include('monofont.ttf');
        $font = './monofont.ttf';


        $possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
        $random_dots = 0;
        $random_lines = 20;
        $captcha_text_color="0x142864";
        $captcha_noice_color = "0x142864";

        $code = '';


        $i = 0;
        while ($i < $characters_on_image) { 
        $code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
        $i++;
        }

        $newPwd = $this->hexrgb();

        $font_size = $image_height * 0.75;
        $image = @imagecreate($image_width, $image_height);

        $background_color = imagecolorallocate($image, 255, 255, 255);

        $arr_text_color = hexrgb($captcha_text_color);
        $text_color = imagecolorallocate($image, $arr_text_color['red'], 
                $arr_text_color['green'], $arr_text_color['blue']);

        $arr_noice_color = hexrgb($captcha_noice_color);
        $image_noise_color = imagecolorallocate($image, $arr_noice_color['red'], 
                $arr_noice_color['green'], $arr_noice_color['blue']);


        for( $i=0; $i<$random_dots; $i++ ) {
        imagefilledellipse($image, mt_rand(0,$image_width),
         mt_rand(0,$image_height), 2, 3, $image_noise_color);
        }

        for( $i=0; $i<$random_lines; $i++ ) {
        imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
         mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
        }

        $textbox = imagettfbbox($font_size, 0, $font, $code); 
        $x = ($image_width - $textbox[4])/2;
        $y = ($image_height - $textbox[5])/2;
        imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);

        header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
        imagejpeg($image);//showing the image
        imagedestroy($image);//destroying the image instance


        function hexrgb ($hexstr)
        {
          $int = hexdec($hexstr);

          return array("red" => 0xFF & ($int >> 0x10),
                       "green" => 0xFF & ($int >> 0x8),
                       "blue" => 0xFF & $int);
        }

        echo $code;
        exit;

    }

当我回复$ code给出错误时

Call to undefined function hexrgb()

如何解决这个问题? 我的monofont.ttf文件放在控制器内。

2 个答案:

答案 0 :(得分:0)

您还应该将参数传递给函数hexrgb(param),或者在函数声明中,您必须使此参数可选。简单问题在于函数参数

答案 1 :(得分:0)

试试这个,

function captcha()
{
        $image_width = 120;
        $image_height = 40;
        $characters_on_image = 6;

        $font = FCPATH.'assets/monofont.ttf';
        /* set font path */
        $possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
        $random_dots = 0;
        $random_lines = 20;
        $captcha_text_color="0x142864";
        $captcha_noice_color = "0x142864";
        $code = '';
        $i = 0;
        while ($i < $characters_on_image) {
            $code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
            $i++;
        }
        $this->session->set_flashdata('latter_code',$code);
        $font_size = $image_height * 0.75;
        $image = @imagecreate($image_width, $image_height);
        $this->output->set_content_type('jpeg'); // You could also use ".jpeg" which will have the full stop removed before looking in config/mimes.php
        $background_color = imagecolorallocate($image, 255, 255, 255);

        $arr_text_color = $this->hexrgb($captcha_text_color); 
        /* changes of hexrgb to $this->hexrgb */
        $text_color = imagecolorallocate($image, $arr_text_color['red'],
        $arr_text_color['green'], $arr_text_color['blue']);

        $arr_noice_color = $this->hexrgb($captcha_noice_color);
      /* changes of hexrgb to $this->hexrgb */
        $image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
                $arr_noice_color['green'], $arr_noice_color['blue']);

        /* generating the dots randomly in background */
        for( $i=0; $i<$random_dots; $i++ ) {
            imagefilledellipse($image, mt_rand(0,$image_width),
                    mt_rand(0,$image_height), 2, 3, $image_noise_color);
        }

        /* generating lines randomly in background of image */
        for( $i=0; $i<$random_lines; $i++ ) {
            imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
                    mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
        }

        /* create a text box and add 6 letters code in it */
        $textbox = imagettfbbox($font_size, 0, $font, $code);
        $x = ($image_width - $textbox[4])/2;
        $y = ($image_height - $textbox[5])/2;
        imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
        /* Show captcha image in the page html page */
        imagejpeg($image);//showing the image
        imagedestroy($image);//destroying the image instance

} 

在验证码功能

之后设置此功能
function hexrgb ($hexstr)
{
        $int = hexdec($hexstr);
        return array("red" => 0xFF & ($int >> 0x10),
                "green" => 0xFF & ($int >> 0x8),
                "blue" => 0xFF & $int);
}

并且您想要设置视图文件

<img src="<?php echo site_url('auth/captch')?>" alt="" class="captcha_image" id='captchaimg' style="height: 30px;width:100px"/>