2013-08-19 77 views
1

我的形象看起来是这样的:破碎的形象PHP(GD)

enter image description here

其从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,有谁能帮我解决这个问题吗?

+1

为什么要经历重新发明轮子的所有麻烦。如果你问我,使用captcha就更容易了。以前全部完成... –

+0

请在浏览器中打开图像URI并粘贴输出。您可能在第二个* for *循环中出现语法错误。 – ComFreek

+0

[Image不会显示(PHP和GD)]的可能重复](http://stackoverflow.com/questions/18300535/image-wont-show-php-and-gd)。你之前已经问过这个确切的问题。当然,这次你有一个不同的错误信息,但这是你的编码。一半的时间,这是关于调试,这是你必须自己做的事情,这不是一个免费的调试器服务 –

回答

1

您的代码没有太多问题。删除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这是实现相当容易,你可以让你的用户造成一些通过使用其数字化的书籍。

+0

语法错误怎么不是一个问题? –

+0

@EliasVanOotegem对不起,这是一个错字:) – Stranger

+0

谢谢。你能告诉你做了什么改变吗?:) –

1

有一个语法错误,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:意外))。
另外:不要使用@错误抑制算子。如果有错误:修复它,不要掩盖它。将您的ini设置为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!”“毫无用处......不知道什么或在哪里”毫无意义