2009-02-26 51 views
0

我試圖爲我的網站開發一個驗證碼類一切都很好,直到我試圖在我的訂閱表單中嵌入使用PHP GD生成的圖像!用GD插入圖像的問題!

這裏是我的類的代碼:

<?php 
//captcha.php 
header("Content-type: image/png"); 


class Captcha { 


    // some attributes bla bla 

    public function __construct($new_string_length,$new_width_picture, 
           $new_height_picture,$new_string_color) { 

    $this->string_length = $new_string_length; 
    $this->width_picture = $new_width_picture; 
    $this->height_picture = $new_height_picture; 
    $this->string_color = $new_string_color; 

    } 

    public function getString() { 
    return $this->string; 
    } 

    public function generateRandomString() { 
    $str = ""; 
    $basket = "abcdefghijklmnopqrstuvwxyz"; 
    $basket_length = strlen($basket); 
    srand ((double) microtime() * 1000000); 
    for($i=0;$i<$this->string_length;$i++) { 

     $generated_pos = rand(0,$basket_length); 
     $str_substr = substr($basket,$generated_pos-1,1); 
     if(!is_numeric($str_substr)) { 
      // if the character picked up isn't numeric 
      if(rand(0,1)==1) { 
      // we randomly upper the character 
      $str_substr = strtoupper($str_substr); 
      } 
     } 
     $str = $str.$str_substr; 
    } 

    $this->string = $str; 
    } 

    **public function generatePictureFromString($new_string) { 
     $root_fonts = '../fonts/'; 
     srand ((double) microtime() * 1000000); 
     $list_fonts = array('ABODE.ttf','acme.ttf','Alcohole.ttf', 
        'Anarchistica.ttf','AMERIKAA.ttf'); 

     $image = @imagecreatetruecolor($this->width_picture,$this->height_picture); 
     $noir = imagecolorallocate($image,0,0,0); 
     $clr = explode('/',$this->string_color); 
     $clr = imagecolorallocate($image,$clr[0],$clr[1],$clr[2]); 




     for($i=0;$i<strlen($new_string);$i++) {  
      imagettftext($image,rand(($this->height_picture/4.3),($this->height_picture)/4.2), 
      rand(-45,45),($this->width_picture)/(5*$this->string_length)+($this->width_picture)/($this->string_length)*$i,0.6*($this->height_picture),$clr, 
      $root_fonts.$list_fonts[rand(0,count($list_fonts)-1)],substr($new_string,$i,1)); 
     } 

     imagepng($image); 
     imagedestroy($image); 

    }** 

} 

我心甘情願地避免以顯示類的一些無用的部分。類本身完美的作品時,我稱之爲generatePictureFromString(..)方法是這樣的:

<?php 
//testeur_classe.php 
require_once '../classes/captcha.php'; 

$captcha = new Captcha(5,200,80,"255/255/255"); 
$captcha->generateRandomString(); 
$str = $captcha->getString(); 
$captcha->generatePictureFromString($str); 
?> 

但是當我嘗試使用插入我的形式產生的畫面:顯示

<img src="<?php echo PATH_ROOT.'classes\testeur_classe.php'; ?>"/> 

什麼!

我該怎麼做?

謝謝!

+0

teseur_classe.php是否自行加載? – 2009-02-26 20:24:05

回答

1

您需要確保圖像src是腳本的有效URL。看看那裏的反斜槓,我的猜測是這實際上是一個文件系統路徑。

+0

是的,它是一個文件系統路徑,我在本地主機上運行我的測試,路徑是有效的,我測試過它.. – 2009-02-26 21:10:31

+0

如果您在瀏覽器中查看源代碼,圖像src中有什麼? – 2009-02-26 21:53:18

2

確定:如果您在瀏覽器中打開classes \ testeur_classe.php,您會看到什麼? (附註:瑞安·格雷厄姆同樣的問題問你問題的評論)

OK:我想你一定喜歡的圖片輸出前設置正確的標題:

header('Content-type: image/png'); 

附:

此代碼的工作原理,只是在我的機器上試過。如果你有一個,你必須有< img src =「...」或< base href =「」的錯誤。你能告訴我們你的html輸出嗎,所以我們可以看到可能是什麼問題?