【问题标题】:php captcha is not working properlyphp验证码无法正常工作
【发布时间】:2014-01-27 13:39:50
【问题描述】:

我在 mozilla 上遇到这个错误

"图片 “*/form%20and%20its%20validation/New%20folder/captcha/captcha.php” 无法显示,因为它包含错误。”

fonts.ttf 位于同一文件夹中。

并且这段代码在 chrome 上没有显示任何内容。这段代码有什么问题?

我想在我的表单页面中包含这个..

$rnum='';

//generating random number
$rnum = rand(100000,9999999);

//creating image with size 300*60
$imgo = imagecreatetruecolor(300,60);

$white = imagecolorallocate($imgo, 255, 255, 255);
$grey = imagecolorallocate($imgo, 128, 128, 128);
$red = imagecolorallocate($imgo, 200, 100,90);
$black = imagecolorallocate($imgo, 0, 0, 0);

imagefilledrectangle($imgo, 0, 0, 200, 35, $black);

//getting font  

$cfont= 'font.ttf'; 

imagettftext($imgo, 35, 0, 22, 24, $red, $cfont, $rnum);

header ("Content-type: image/png");
imagepng($imgo);
imagedestroy($imgo);

【问题讨论】:

    标签: php


    【解决方案1】:

    这是 99%,因为脚本中的错误会打印到浏览器,也是

    您可以捕获错误并打印它而不是损坏的图像:

    error_reporting(E_ALL);
    ini_set('display_errors', true);
    ini_set('display_startup_errors', true);
    ini_set('xmlrpc_errors', true);
    ob_start();
    $rnum='';
    
    //generating random number
    $rnum = rand(100000,9999999);
    
    //creating image with size 300*60
    $imgo = imagecreatetruecolor(300,60);
    
    $white = imagecolorallocate($imgo, 255, 255, 255);
    $grey = imagecolorallocate($imgo, 128, 128, 128);
    $red = imagecolorallocate($imgo, 200, 100,90);
    $black = imagecolorallocate($imgo, 0, 0, 0);
    
    imagefilledrectangle($imgo, 0, 0, 200, 35, $black);
    
    //getting font  
    
    $cfont= 'font.ttf'; 
    
    imagettftext($imgo, 35, 0, 22, 24, $red, $cfont, $rnum);
    $errors = ob_get_clean();
    ob_end();
    if ($errors) {
        echo $errors;
    } else {
        header ("Content-type: image/png");
        imagepng($imgo);
    }
    
    imagedestroy($imgo);
    

    如果它不起作用或没有帮助,您只需从脚本中删除 header ("Content-type: image/png");,然后重试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多