0

我在 php 中编写了以下代码来生成验证码。该代码在 https:// 下运行,在 IE9、Mozilla、Chrome 和 Safari 中运行良好。但是,在 IE8 中,有时它可以工作,有时它不能(显示损坏的图像)。有谁知道发生了什么?任何人都可以看到下面的代码有什么问题会导致 IE8 出现问题:

     class Captcha_generator{

        function __construct()
        {

        }

        public function get_image($callType)
        {
            $font = './image/MONOFONT.TTF';
            $width='140';
            $height='40';
            $font_size = $height * 0.75;
            $angle = 3; 

            //Now lets use md5 to generate a totally random string 
            $md5 = md5(microtime() * mktime()); 

            /* 
            We dont need a 32 character long string so we trim it down to 5 
            */ 
            $string = substr($md5,0,5);
            /* 
            Now for the GD stuff, for ease of use lets create 
            the image from a background image. 
            */ 


            $captcha = imagecreate($width, $height);
rand(0,255));


            /* 
            Lets set the colours, the colour $line is used to generate lines. 
            Using a blue misty colours. The colour codes are in RGB 
            */ 

            $background_color = imagecolorallocate($captcha, 255, 255, 255);
            $text_color = imagecolorallocate($captcha, 20, 40, 100);
            $noise_color = imagecolorallocate($captcha, 100, 120, 180);

            /* generate random dots in background */
            for( $i=0; $i<($width*$height)/3; $i++ ) {
             imagefilledellipse($captcha, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
            }
            /* generate random lines in background */
            for( $i=0; $i<($width*$height)/150; $i++ ) {
             imageline($captcha, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
            }



            /* 
            Encrypt and store the key inside of a session 
            */ 

            $_SESSION['captcha_key'] = md5("SDFSDTRSDERGTGGT".$string."AAAAA"); 

            /* 
            Output the image 
            */ 

            imagettftext($captcha, $font_size, $angle, 30, 30, $text_color, $font , $string);
            imagepng($captcha, './image/captcha.png');


            if($callType == "internal")
            {
                return "<img style='z-index:90' width='130' height='35' src='image/captcha.png?".time()."' />; 
                        <input type='hidden' name='powerCode' value='".$_SESSION['captcha_key']."'/>";
            }
            else 
            {
                echo "<img style='z-index:90' width='130' height='35' src='image/captcha.png?".time()."' />";
                echo "<input type='hidden' name='powerCode' value='".$_SESSION['captcha_key']."'/>";
            }
        }


     }
4

1 回答 1

1

这更像是一个评论而不是一个答案,并且只展示了如何将验证码 PNG 图像转换为 data-uri:

        /* 
        Output the image 
        */ 

        imagettftext($captcha, $font_size, $angle, 30, 30, $text_color, $font , $string);
        ob_start();
        imagepng($captcha);
        $captchaURI= 'data:image/png;base64,'.base64_encode(ob_get_clean());

请注意此处输出控制功能的使用。它们通常非常有用,例如让你的函数回显或返回字符串而不复制代码:

        ob_start();
        echo "<img style='z-index:90' width='130' height='35' src='", $captchaURI, "' />";
        echo "<input type='hidden' name='powerCode' value='", $_SESSION['captcha_key'], "'/>";
        if ($callType == "internal")
        {
             return ob_get_clean();
        }
        ob_end_flush();
于 2012-06-27T23:33:21.003 回答