PHP:image*()-函数提供无效图像

PHP:image*()-函数提供无效图像,php,Php,以下代码: class FilesController extends Controller { public function icon($fileId) { $this->uses("FileAttachment"); $FA = new FileAttachment($fileId); $fnth = $FA->path . 'th___' . $FA->filename; $this->View->render =

以下代码:

class FilesController extends Controller {
  public function icon($fileId) {
    $this->uses("FileAttachment");
    $FA = new FileAttachment($fileId);  
    $fnth = $FA->path . 'th___' . $FA->filename;  
    $this->View->render = false;

    if (file_exists($fnth)) {                        
        $imageinfo = getimagesize($fnth);

        switch ($imageinfo[2]) {
            case IMAGETYPE_JPEG:
                $image = imagecreatefromjpeg($fnth);                                    
                Header("Content-type: image/jpg");
                imagejpeg($image);
                break;
            case IMAGETYPE_GIF:
                $image = imagecreatefromgif($fnth);
                Header("Content-type: image/gif");                                            
                imagegif($image);
                break;
            case IMAGETYPE_PNG:
                $image = imagecreatefrompng($fnth);                                                           
                //Header("Content-type: image/png");
                imagepng($image);
                break;  
            case IMAGETYPE_BMP:
                $image = imagecreatefromwbmp($fnth); 
                Header("Content-type: image/bmp");                   
                imagewbmp($image);
                break;                  
          }      
    }
    imagedestroy($image);        
  }
}
生成损坏的图像。该图像在浏览器中显示为断开的图像。文件存在,否则将一无所获。我的窗口正确显示图片。原因可能是什么

另外,如果我删除标题,浏览器将显示图像的二进制内容,因此它实际上看起来正常…

imagecreatefromfromgif()//返回图像资源

您是否尝试在底部添加

readfile($image);  //reads a file and writes it to the output buffer.
imagecreatefromformgif()//返回图像资源

您是否尝试在底部添加

readfile($image);  //reads a file and writes it to the output buffer.

你的代码是毫无意义的——你强迫PHP(和gd)做一些无用的工作来加载文件,将其解压缩到内存中,然后重新压缩输出

为什么不干脆拥有:

$info = getimagesize($path_to_file);
header('Content-type: ' . $info['mime']);
readfile($path_to_file);
?


至于您的代码,请检查函数的整个输出。如果产生任何php警告/错误,它们会嵌入到输出中,通常会导致“损坏”图像。

您的代码毫无意义-您强制php(和gd)做大量无用的工作来加载文件,将其解压缩到内存中,然后重新压缩以进行输出

为什么不干脆拥有:

$info = getimagesize($path_to_file);
header('Content-type: ' . $info['mime']);
readfile($path_to_file);
?


至于您的代码,请检查函数的整个输出。如果产生任何php警告/错误,它们会嵌入到输出中,并导致图像“损坏”。

注意在imagecreatefromgif()调用之后的
imageigf()
调用,其他类型的调用也一样。@MarcB:不,不是这样。没有理由这样做。注意
imageigf()
在imagecreatefromgif()调用之后立即调用,其他所有类型的调用也一样。@MarcB:不,不是真的,没有理由这样做。它不起作用。图像仍然被破坏。如果我输入ommit header(),则没有警告或错误,只有二进制内容。还有其他想法吗?好的,我发现问题了。输出前有几个空格字符。我必须弄清楚他们是从哪里来的。但我接受了你的回答,因为你提出了另一种实现该功能的好方法。干杯您需要清理缓冲区,只需使用ob_start();和ob_end_clean();它不起作用。图像仍然被破坏。如果我输入ommit header(),则没有警告或错误,只有二进制内容。还有其他想法吗?好的,我发现问题了。输出前有几个空格字符。我必须弄清楚他们是从哪里来的。但我接受了你的回答,因为你提出了另一种实现该功能的好方法。干杯您需要清理缓冲区,只需使用ob_start();和ob_end_clean();