使用GD在表单中插入图像时出现问题!

时间:2009-02-26 20:16:45

标签: php

我正在尝试为我的网站开发一个验证码类,一切都很好,直到我试图在我的订阅表单中嵌入用PHP 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 = "abcdefghijklmnopqrstuvwxyz0123456789";
    $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'; ?>"/>

没有显示任何内容!

我该怎么做?

谢谢!

2 个答案:

答案 0 :(得分:2)

确定:如果您在浏览器中打开classes \ testeur_classe.php,您会看到什么? (p.s.与Ryan Graham提出的相同问题的评论)

确定:我认为你必须在图片输出之前设置正确的标题,如:

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

P.S。

此代码有效,只是在我的机器上尝试过。你必须有&lt; img src =“...”或者&lt; base href =“”的错误(如果你有的话)。你能告诉我们你的html输出,以便我们可以看到可能出现的问题吗?

答案 1 :(得分:1)

您需要确保图像src是脚本的有效URL。看看那里的反斜杠,我的猜测实际上是文件系统路径。