2012-12-07 41 views
1

我該如何加載?現在,它根本沒有顯示任何圖像...我在創建captcha時並不是那麼棒,因爲我通常不會這麼做。驗證碼圖片未能加載?

captcha.php:

<?php 
$string = ''; 
for ($i = 0; $i < 5; $i++) { 
    // this numbers refer to numbers of the ascii table (lower case) 
    $string .= chr(rand(97, 122)); 
} 
$image = imagecreatetruecolor(170, 60); 
$color = imagecolorallocate($image, 200, 100, 90); // red 
$white = imagecolorallocate($image, 255, 255, 255); 
imagefilledrectangle($image,0,0,399,99,$white); 
imagettftext ($image, 30, 0, 10, 40, $color, ASSETPATH."arial.ttf", $string); 
header("Content-type: image/png"); 
imagepng($image); 
?> 

register.php:

<?php echo '<img src="'.ASSETPATH.'img/captcha.php" />'; ?> 

我的資產的路徑是正確的,因爲我用它在其他地方,它加載完美的罰款。這個項目的MVC格式是否搞亂了它?

+0

如果您必須使用CAPTCHA才能利用reCAPTCHA這樣的服務 - 您的自定義腳本(白色背景上的純文本)生成的CAPTCHA將很容易破解。 – leepowers

回答

0

如何使用驗證碼服務之一,如recaptcha?

0

看你使用這個常數ASSETPATH都爲你的圖像URI以及在imagettftextfontfile路徑,我只能假設你已經存儲在爲你的形象相同的路徑你fontfile?

如果情況並非如此,或者該文件無法在那裏打開,PHP會發出imagettftext(): Invalid font filename的警告。如果display_errors設置爲在您的php.ini中的(您可以檢查phpinfo來驗證這一點),這意味着錯誤消息將與您的圖像數據一起發送到輸出流(即破壞圖像數據並導致瀏覽器不能顯示圖像)。這也會阻止標題被修改,因爲在調用標題之前錯誤會發生。

但是,如果display_errors未打開且PHP找不到您提供的字體文件或者由於任何原因無法打開(例如權限),則結果將是空白的170x60 PNG圖像。

如果我在本地測試您的代碼,它證明可以按預期工作,只要我向PHP提供正確的絕對路徑到我的truetype字體文件即可。

例如,我在我的系統上存儲了一些truetype字體,存儲在/usr/share/fonts/truetype/,這絕對不在我的webroot中,因爲我通常不會將字體文件保留在那裏。另請注意,我的PHP用戶有足夠的權限從此路徑讀取。

現在,如果我提供的PHP一起,我想用我用你的代碼獲得下面的圖片TrueType字體文件的正確絕對路徑...

$string = ''; 
for ($i = 0; $i < 5; $i++) { 
    // this numbers refer to numbers of the ascii table (lower case) 
    $string .= chr(rand(97, 122)); 
} 
$image = imagecreatetruecolor(170, 60); 
$color = imagecolorallocate($image, 200, 100, 90); // red 
$white = imagecolorallocate($image, 255, 255, 255); 
imagefilledrectangle($image,0,0,399,99,$white); 
imagettftext ($image, 30, 0, 10, 40, $color, '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-ExtraLight.ttf', $string); 
header("Content-type: image/png"); 
imagepng($image); 

Image output from the above code

什麼你應該嘗試做以進一步調試這個是運行PHP腳本,它自己生成的圖像,打開display_errors和error_reporting設置爲-1,如果確實是你的字體文件的問題,你會看到這個在您的error_log或testin期間顯示的錯誤輸出中g帶有display_errors的腳本。