<?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:
$thi->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);
$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")
{ 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?>
这个是一个图像处理的类,测试了没有问题。问题是在另一个PHP页面的问题:<?php
require_once("ImageProcess_Class.php");
?><?php
$str =  $_FILES['file']['tmp_name'];
$im = new ImageProcess($str);
$im->OutputImage($im->src_image);
?><!DOCTYPE html>
<html lang="en"><head>
  <title>ImageProcess_Class_Test</title>
</head>
<body>
<form action="test.php" method="post" enctype="multipart/form-data"> 
<label for="file">Filename:</label> 
<input type="file" name="file" id="file" /> <br /> 
<input type="submit" name="submit" value="Submit" /> </form> 
<img src="image3.php" alt="" width="" height="" /></body></html>问题是这样的,我想让这个Test.php在提交了一个图像后在这个提交的按钮下面就出现这个原图。可是一提交就什么都没有了,图像也不显示,就是一个叉叉。原来的提交框也没有了。PHP和HTML是怎么交互的呀,好晕!希望谁有时间可以看看!

解决方案 »

  1.   

    action="test.php" 
    这个是说你要执行的的php,比如你submit,提交框没有是正常的,因为已经执行到test.php里面了,想有什么内容出现,是要看你test.php里面的定义
      

  2.   

    也是在test.php页面做处理,你可以用ajax,提交后更新图片的src就好
      

  3.   

    可是第二个页面就是Test.php啊……
    就是由第二个页面执行,我的意思是让图片显示在提交框下面。
      

  4.   

    <?php
        require_once("ImageProcess_Class.php");
    ?><?php
        $str =  $_FILES['file']['tmp_name'];
        $im = new ImageProcess($str);
    //    $im->OutputImage($im->src_image);
    ?><!DOCTYPE html>
    <html lang="en"><head>            
      <title>ImageProcess_Class_Test</title>
    </head>
    <body>
    <form action="test.php" method="post" enctype="multipart/form-data"> 
    <label for="file">Filename:</label> 
    <input type="file" name="file" id="file" /> <br /> 
    <input type="submit" name="submit" value="Submit" /> </form>
    <!--这样行不行?-->
    <img src="<?php $im->OutputImage($im->src_image); ?>" alt="" width="" height="" /></body></html>