2013-08-19 74 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!」「毫無用處......不知道什麼或在哪裏」毫無意義