1.ImageProcess_Class.php 这是我编写的图片处理的类,单独使用确定是没问题的。<?php
class ImageProcess
{
var $allow_type = array(IMAGETYPE_GIF, IMAGETYPE_JPEG,IMAGETYPE_PNG,IMAGETYPE_BMP,IMAGETYPE_WBMP);//允许处理图片类型
var $src_path;//原路径
var $src_image;//原图图片资源
var $src_str_image;//原图片数据流
var $src_width;//原图宽度
var $src_height;//原图高度
var $src_type;//原图类型
var $thumb_max_width = 200;//
var $thumb_max_height = 200;//
var $thumb_image;//缩略图资源
var $thumb_str_image;//缩略图数据流
var $thumb_width;//缩略图宽度
var $thumb_height;//缩略图高度

var $crop_image;//剪切生成图片资源
var $crop_str_image;//剪切生成图片数据流
var $crop_top;//
var $crop_left;//
var $crop_width;//
var $crop_height;// var $dst_type;//要转换成的图片类型
var $dst_image;//
var $dst_str_image;//


/**
 
*/
function __construct($path="")
{
$this->src_path = $path;//设置路径
list($this->src_width,$this->src_height,$this->src_type)= getimagesize($path);//获取文件信息,暂时假设文件合法

if(!in_array($this->src_type,$this->allow_type))//不支持文件格式判断
{
die("不支持这个格式");
return ;
}
switch ($this->src_type)
{
case 1:
$this->src_image = imagecreatefromgif($this->src_path);
break;
case 2:
$this->src_image = imagecreatefromjpeg($this->src_path);
break;
case 3:
$this->src_image = imagecreatefrompng($this->src_path);
break;
default:
$this->src_image = "";
}

$this->src_str_image=fread(fopen($this->src_path,'rb'),filesize($this->src_path));//假设是可以打开的 } // end func
function GetThumbSize ( $src_width, $src_height )//获取缩略图的长和宽
  {    $max_width = $this-> thumb_max_width;
    $max_height = $this->thumb_max_height;    $x_ratio = $max_width / $src_width;
    $y_ratio = $max_height / $src_height;    $is_small = ( $src_width >= $max_width && $src_height >= $max_height );    if ( !$is_small )
    {
      $dest_width = $src_width;
      $dest_height = $src_height;
    }
    elseif ( $x_ratio * $src_height < $max_height )
    {
      $dest_width = $max_width;
      $dest_height = ceil ( $x_ratio * $src_height );
    }
    else
    {
      $dest_width = ceil ( $y_ratio * $src_width );
      $dest_height = $max_height;
    }    return array ( $dest_width, $dest_height );  }
public function makethumb()//图片缩略图功能
{
$ori_width = $this->src_width;
$ori_height = $this->src_height;
list($this->thumb_width,$this->thumb_height) = $this->GetThumbSize($ori_width,$ori_height);
//echo $this->thumb_width.'<br>';
//echo $this->thumb_height.'<br>';
$new_image = imagecreatetruecolor($this->thumb_width,$this->thumb_height); 
imagecopyresampled($new_image, $this->src_image, 0, 0, 0, 0, $this->thumb_width,$this->thumb_height, $this->src_width ,$this->src_height);
$this->thumb_image= $new_image; 

} // end func

/*
var $crop_image;//剪切生成图片资源
var $crop_str_image;//剪切生成图片数据流
var $crop_top;//
var $crop_left;//
var $crop_width;//
var $crop_height;//
*/
public function imagecrop($top,$left,$width,$height)//图像剪裁,暂没考虑边界问题。
{
$this->crop_image = imagecreatetruecolor($width,$height);
imagecopy($this->crop_image,$this->src_image,0,0,$top,$left,$width,$height);
    
} // end func/*
var $dst_type;//要转换成的图片类型
var $dst_image;//
var $dst_str_image;//
*/
public function ChangeType($type)
{
$this->dst_type = $type;} // end func

public function OutputImage($source="",$mime="2")
{
if ($source=="") {
$source = $this->src_image;
    
} switch ($mime) {
    case 1:
header('Content-Type: image/gif');
    imagegif($source);
break;
case 2:
header('Content-Type: image/jpeg');
imagejpeg($source);
break;
case 3:
header('Content-Type: image/png');
imagepng($source);
break;
        
}
imagedestroy($source);
    
} // end func
} // end class?>2. Showorigin.php 显示上传图片的<?php
require_once("ImageProcess_Class.php");
session_start();
if (isset($_SESSION['img'])) {
//header(" content-type: image/gif ");
echo $_SESSION['img']->src_str_image; //这种方法可以显示图片
//echo $_SESSION['img']->OutputImage( $_SESSION['img']->src_image); //这种方法显示不了图片,是一个叉叉
//echo $_SESSION['img'];
    
}
?>这里就有问题了,用没注释的就可以显示,而用下面的确不能显示 ,是一个叉。不知道为什么。请大家帮忙看一下有什么问题
3. Process.php <?php
require_once("ImageProcess_Class.php");
session_start();
?>
<head>
  <title></title>
</head> <body>
跳转成功 !
原图:<br><img src="showorigin.php" />
</body></html>

解决方案 »

  1.   

    你为什么不指定你已知的类型,而要一个默认的类型呢?
      //echo $_SESSION['img']->OutputImage( $_SESSION['img']->src_image, $_SESSION['img']->src_type)); //这种方法显示不了图片,是一个叉叉
    另外一个问题在浏览器中输出流如果没有指定类型的话,浏览器会有一定的容错性但是使用img标签的话,不指定具体文件类型的话,肯定不会解析
      

  2.   

    你至少需要使用
    $_SESSION['img']->OutputImage( $_SESSION['img']->src_image); 
    而不是
    echo $_SESSION['img']->OutputImage( $_SESSION['img']->src_image); OutputImage 方法中 使用了 imageXXX 函数直接生成图片数据流
    如果你再 echo 的话,就将 imageXXX 的返回值也当作图片的一部分了。这必然给图片解码带来错误
      

  3.   

    ob_end_clean()一下,
    另外把
    这个header('Content-Type: image/gif');去掉
    右键->属性->打开那个图片路径,看看什么问题
      

  4.   

    你好,这个是我写错了,我用的是$_SESSION['img']->OutputImage( $_SESSION['img']->src_image); 
    但是还是不可以。