简体   繁体   English

损坏的图像PHP(GD)

[英]broken image PHP (GD)

My image looks like this: 我的图像如下所示:

在此处输入图片说明

Its getting the image from antibot.php 它从antibot.php获取图像

Here is my 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;
?> 

I have installed GD and freetype, could anyone help me to fix this? 我已经安装了GD和freetype,有人可以帮助我解决此问题吗?

There's a syntax error, ComFreek is right: 出现语法错误,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?

Has one closing parentheses too many, it should be 右括号太多,应该是

   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

Oh and please , don't use or die . 哦, 不要使用or die If there's an error, fix it. 如果有错误,请修复。 Don't use a pointless or die message. 不要使用毫无意义的消息or die消息。 Let PHP show the errors ( as in line X: Unexpected ) ). 让PHP显示错误( 如X行:Unexpected) )。
Also: don't use the @ error supressing operator. 另外:不要使用@错误抑制运算符。 If there's an error: Fix it, don't cover it up. 如果有错误:修复它,不要掩盖它。 Have your ini set to E_STRICT | E_ALL 将您的ini设置为E_STRICT | E_ALL E_STRICT | E_ALL and display errors set to 1, and work at your code until it's notice-free. E_STRICT | E_ALL并将显示错误设置为1,并根据您的代码进行操作,直到不引起注意。

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

It makes your code more readable, and if something goes wrong, at least you stand a better chance to actually know what to fix. 它使您的代码更具可读性,并且如果出现问题,至少您有更好的机会实际知道要解决的问题。 Something like "Kunne ikke velge GD!" 诸如“ Kunne ikke velge GD!”之类的东西 is about as meaningless as "Something didn't quite work... no idea what or where" 几乎没有什么意义,就像“某事未完全起作用……不知道在什么地方或在哪里”

Your code doesn't have many problems. 您的代码没有很多问题。 Remove the extra brackets at the end of the imagecolorallocate inside the for loops. for循环内的imagecolorallocate的末尾删除多余的括号。 It is just having some syntax errors. 它只是有一些语法错误。

<?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;
?> 

But prefer to use something like recaptcha which is quite easier to implement and you can let your users contributing something to digitizing the books by using it. 但是,喜欢使用诸如recaptcha之类的东西,它更容易实现,并且您可以让您的用户通过使用它为书本的数字化做出贡献。

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

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