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

Php 使用GD在表单中插入图像时出现问题!,php,Php,我正在尝试为我的网站开发一个captcha类。在我尝试将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,

我正在尝试为我的网站开发一个captcha类。在我尝试将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);

  }**

}

您需要确保图像src是脚本的有效URL。查看其中的反斜杠,我猜这实际上是一个文件系统路径。

您需要确保映像src是脚本的有效URL。查看其中的反斜杠,我猜这实际上是一个文件系统路径。

好的:如果在浏览器中打开classes\testeur\u classe.php,您会看到什么? (注:问题与Ryan Graham在评论中向您提出的问题相同)

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

header('Content-type: image/png');
p、 美国

这个代码有效,只是在我的机器上试过了。您必须在上有bug确定:如果在浏览器中打开classes\testeur\u classe.php,您会看到什么? (注:问题与Ryan Graham在评论中向您提出的问题相同)

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

header('Content-type: image/png');
p、 美国


这个代码有效,只是在我的机器上试过了。你一定有漏洞teseur_classe.php自己正确加载了吗?teseur_classe.php自己正确加载了吗?是的,这是一个文件系统路径,我在本地主机上运行测试,路径是有效的,我测试了它。如果你在浏览器中查看源代码,图像src中是什么?你不能在html设计中放置系统绝对路径,这不是这样的。是的,这是一个文件系统路径,我正在本地主机上运行测试,路径是有效的,我测试了它。如果你在浏览器中查看源代码,图像src中有什么?你不能在html设计中放置系统绝对路径,这就是不工作。如果我打开classes\testeur\u classe.php,我会看到一个完美的captcha图片:)Ups,我没有看到代码顶部的标题(“”)。我们可以看到您的输出html吗?您的意思是:Vérification:这就是bug!正如我在前面的评论中所说,在html设计中不能使用系统路径。如果你有一个指向C:\Program Files\EasyHP 2.0b1\www\polydotnet的域,那么你应该使用src=“classes/testeur\u classe.php”而不是其他任何东西。如果我打开classes\testeur\u classe.php,我会看到一个完美的验证码图片:)Ups,我没有看到你代码顶部的标题(“”)。我们可以看到您的输出html吗?您的意思是:Vérification:这就是bug!正如我在前面的评论中所说,在html设计中不能使用系统路径。如果您有指向C:\Program Files\EasyPHP 2.0b1\www\polydotnet的域,那么您应该使用src=“classes/testeur\u classe.php”而不是其他。
header('Content-type: image/png');