繁体   English   中英

损坏的图像PHP(GD)

[英]broken image PHP (GD)

我的图像如下所示:

在此处输入图片说明

它从antibot.php获取图像

这是我的antibot.php

<?php
    session_start();
    $width      = 75;
    $height     = 40;
    $length     = 3;
    $baseList   = '1234567890';
    $code       = "";
    $counter    = 0;
    $image      = @imagecreate($width, $height) or die('Kunne ikke velge GD!');

    for($i=0; $i<10; $i++){
       imageline($image, 
             mt_rand(0,$width), mt_rand(0,$height), 
             mt_rand(0,$width), mt_rand(0,$height), 
             imagecolorallocate($image, mt_rand(150,255), 
                                        mt_rand(150,255), 
                                        mt_rand(150,255)))) or die('Kunne ikke velge line!');
    }

    for($i=0, $x=0; $i<$length; $i++){
       $actChar = substr($baseList, rand(0, strlen($baseList)-1), 1);
       $x += 10 + mt_rand(0,10);
       imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $actChar, 
          imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155)))) or die('Kunne ikke velge GD2!');
       $code .= strtolower($actChar)) or die('Kunne ikke velge GD3!');
    }

    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
    $_SESSION['securityCode'] = $code;
?> 

我已经安装了GD和freetype,有人可以帮助我解决此问题吗?

出现语法错误,ComFreek是正确的:

   imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $actChar, 
      imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155)))) or die('Kunne ikke velge GD2!');
//                                                                            /\4?

右括号太多,应该是

   imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $actChar, 
      imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155))) or die('Kunne ikke velge GD2!');
//                                                                       3 is enough

哦, 不要使用or die 如果有错误,请修复。 不要使用毫无意义的消息or die消息。 让PHP显示错误( 如X行:Unexpected) )。
另外:不要使用@错误抑制运算符。 如果有错误:修复它,不要掩盖它。 将您的ini设置为E_STRICT | E_ALL E_STRICT | E_ALL并将显示错误设置为1,并根据您的代码进行操作,直到不引起注意。

$image      = @imagecreate($width, $height) or die('Kunne ikke velge GD!');
//should be:
$image = imagecreate($width, $height);

它使您的代码更具可读性,并且如果出现问题,至少您有更好的机会实际知道要解决的问题。 诸如“ Kunne ikke velge GD!”之类的东西 几乎没有什么意义,就像“某事未完全起作用……不知道在什么地方或在哪里”

您的代码没有很多问题。 for循环内的imagecolorallocate的末尾删除多余的括号。 它只是有一些语法错误。

<?php
    session_start();
    $width      = 75;
    $height     = 40;
    $length     = 3;
    $baseList   = '1234567890';
    $code       = "";
    $counter    = 0;
    $image      = @imagecreate($width, $height) or die('Kunne ikke velge GD!');

    for($i=0; $i<10; $i++){
       imageline($image, 
             mt_rand(0,$width), mt_rand(0,$height), 
             mt_rand(0,$width), mt_rand(0,$height), 
             imagecolorallocate($image, mt_rand(150,255), 
                                        mt_rand(150,255), 
                                        mt_rand(150,255))) or die('Kunne ikke velge line!'); 
    }

    for($i=0, $x=0; $i<$length; $i++){
       $actChar = substr($baseList, rand(0, strlen($baseList)-1), 1);
       $x += 10 + mt_rand(0,10);
       imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $actChar, 
          imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155))) or die('Kunne ikke velge GD2!');
       $code .= strtolower($actChar) or die('Kunne ikke velge GD3!');
    }

    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
    $_SESSION['securityCode'] = $code;
?> 

但是,喜欢使用诸如recaptcha之类的东西,它更容易实现,并且您可以让您的用户通过使用它为书本的数字化做出贡献。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM