2014-02-13 96 views
0

我想使用PHP imagettftext生成驗證碼圖像。以下是我的代碼。它給出了一個空的圖像。我想解決這個問題。生成驗證碼圖像PHP

我測試了代碼,但無法找到爲什麼圖像不是bieng顯示,雖然我的隨機文本被傳遞到驗證碼圖像生成功能。

<?php 

    function generateRandomString($length = 10) { 
     $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
     $randomString = ''; 
     for ($i = 0; $i < $length; $i++) { 
      $randomString .= $characters[rand(0, strlen($characters) - 1)]; 
     } 
     return $randomString; 
    } 

    function generateCaptchaImage($text = 'good'){ 
     echo $text; 
     echo 'OK'; 
     // Set the content-type 
     header('Content-Type: image/png'); 

     $width = 200; 
     $height = 30; 
     $font = 'ThisisKeSha.ttf'; 

     // Create the image 
     $im = imagecreatetruecolor($width, $height); 

     //ADD NOISE - DRAW background squares 
     $square_count = 6; 
     for ($i = 0; $i < 10; $i++) { 
      $cx = (int)rand(0, $width/2); 
      $cy = (int)rand(0, $height); 
      $h = $cy + (int)rand(0, $height/5); 
      $w = $cx + (int)rand($width/3, $width); 
      imagefilledrectangle($im, $cx, $cy, $w, $h, $white); 
     } 

     //ADD NOISE - DRAW ELLIPSES 
     $ellipse_count = 5; 
     for ($i = 0; $i < $ellipse_count; $i++) { 
      $cx = (int)rand(-1*($width/2), $width + ($width/2)); 
      $cy = (int)rand(-1*($height/2), $height + ($height/2)); 
      $h = (int)rand($height/2, 2*$height); 
      $w = (int)rand($width/2, 2*$width); 
      imageellipse($im, $cx, $cy, $w, $h, $grey); 
     } 

     // Add the text 
     imagettftext($im, 20, 0, 10, 20, $black, $font, $text); 

     imagepng($im); 

    } 

    $randomString = generateRandomString(); 
    generateCaptchaImage($randomString); 

    ?> 
+0

任何錯誤日誌? –

+1

'$ white'和'$ black'沒有定義。刪除'echo $ text;'和'echo'OK';'。您的服務器上是否存在'ThisisKeSha.ttf',並且位於與此相同的目錄中? –

+0

非常感謝$白和$黑!!!! –

回答

2

從評論

  • $white並沒有定義$black解決。
  • 刪除echo $text;echo 'OK';
  • ThisisKeSha.ttf在您的服務器上,並在這個相同的目錄?

$white$black未定義。

5

試試這個代碼;

<?php 
    function generateRandomString($length = 10) { 
     $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
     $randomString = ''; 
     for ($i = 0; $i < $length; $i++) { 
      $randomString .= $characters[rand(0, strlen($characters) - 1)]; 
     } 
     return $randomString; 
    } 
    function generateCaptchaImage($text = 'good'){ 
     // Set the content-type 
     header('Content-Type: image/png'); 
     $width = 200; 
     $height = 30; 
     // Create the image 
     $im = imagecreatetruecolor($width, $height); 

     // Create some colors 
     $white = imagecolorallocate($im, 255, 255, 255); 
     $grey = imagecolorallocate($im, 128, 128, 128); 
     $black = imagecolorallocate($im, 0, 0, 0); 
     imagefilledrectangle($im, 0, 0, 399, 29, $white); 

     //ADD NOISE - DRAW background squares 
     $square_count = 6; 
     for($i = 0; $i < $square_count; $i++){ 
      $cx = rand(0,$width); 
      $cy = (int)rand(0, $width/2); 
      $h = $cy + (int)rand(0, $height/5); 
      $w = $cx + (int)rand($width/3, $width); 
      imagefilledrectangle($im, $cx, $cy, $w, $h, $white); 
     } 

     //ADD NOISE - DRAW ELLIPSES 
     $ellipse_count = 5; 
     for ($i = 0; $i < $ellipse_count; $i++) { 
      $cx = (int)rand(-1*($width/2), $width + ($width/2)); 
      $cy = (int)rand(-1*($height/2), $height + ($height/2)); 
      $h = (int)rand($height/2, 2*$height); 
      $w = (int)rand($width/2, 2*$width); 
      imageellipse($im, $cx, $cy, $w, $h, $grey); 
     } 

     // Replace path by your own font path 
     $font = 'ThisisKeSha.ttf'; 

     // Add some shadow to the text 
     imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); 

     // Add the text 
     imagettftext($im, 20, 0, 10, 20, $black, $font, $text); 

     // Using imagepng() results in clearer text compared with imagejpeg() 
     imagepng($im); 
     imagedestroy($im); 
    } 

    $randomString = generateRandomString(); 
    generateCaptchaImage($randomString); 
?> 

你沒有產生顏色$white$black$grey。此外,該字體必須在這個php文件的相同文件夾中。

0

這是建立在PHP captacha一個簡單的方法,你可以試試這個,在此使用

<?php 
@session_start(); 
header("Content-type: image/png"); 
$_SESSION["captcha"] = substr(md5(time()),0,5); 
$im = imagecreate(110, 30); 
$white = imagecolorallocate($im, 244, 255, 255); 
$red = imagecolorallocate($im, 255, 255, 255); 
$black = imagecolorallocate($im, 0, 0, 0); 
$size = $_SESSION["captcha"]; 
$text = "$size"; 
$font = 'comic.TTF'; 
imagettftext($im, 20, 0, 25, 20, $red, $font, $text); 
imagettftext($im, 20, 0, 25, 20, $black, $font, $text); 
imagepng($im); 
imagedestroy($im); 
?> 

PHP會議。 這裏有更多關於Captcha in PHP的詳細信息