+ -
当前位置:首页 → 问答吧 → 初次写类,生成略缩图和水印图的类,望大家批评,修改

初次写类,生成略缩图和水印图的类,望大家批评,修改

时间:2009-04-17

来源:互联网

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
class WaterAndSmallImg
{
public $im;     //图片资源
public $Owidht;
public $Oheight;
public $firstFolder;
public $monthFolder;
public $waterFolder;
public $goodsFolder;
public $allowType;
//public $creatImgType;  //上传的图片类型

function __construct($firstFolder="images")
    {
     $this->firstFolder=$firstFolder;
     $this->monthFolder=$this->firstFolder.'/'.date("Ym");
     $this->waterFolder=$this->monthFolder.'/'."water_img";
     $this->goodsFolder=$this->monthFolder.'/'."goods_img";
     $this->CreateFolder($this->firstFolder);       //建立文件夹
     $this->CreateFolder($this->monthFolder);       //建立月份文件夹
    $this->allowType = "gif/jpg/jpeg/png";         // 允许上传的文件类型  
    }
   
     //获得要处理的图片的资源
    function createimg($oimg)
    {
     $img= GetImageSize($oimg);
  switch($img['2'])
     {
      case 1:
    $this->im [email==@ImageCreateFromGIF($oimg]=@ImageCreateFromGIF($oimg[/email]);
    break;
   case 2:
    $this->im [email==@ImageCreateFromJPEG($oimg]=@ImageCreateFromJPEG($oimg[/email]);
    break;
   case 3:
       $this->im [email==@ImageCreateFromPNG($oimg]=@ImageCreateFromPNG($oimg[/email]);
       break;
  }
  if(!$this->im)
  {
      return false;
  }
  else
  {
      $this->Owidth=ImageSX($this->im);         // 取得原图宽度;
      $this->Oheight=ImageSY($this->im);        // 取得原图高度;
  }
    }
// 创建文件夹
function CreateFolder($foldername)
{
     if(!is_dir($foldername))
     {
      mkdir("$foldername");
     }
}

// 获得图片类型
function createImgType($oimg)
{
     $fleth = explode(".",$oimg);
     $imgType = $fleth[(int)count($fleth)-1];           // 获取文件后缀;
     return $imgType;
}

//创建图片新名称
function createNewImgName($oimg)
{
     $newName = date('YmdHis') .mt_rand(0, 99999). "." .$this->createImgType($oimg);        // 创建新文件名;
     return $newName;
}

//检查文件类型是否合格
function chk_ImgType($oimg)
{
  $iwTrue = 0;
  $fle = explode("/",$this->allowType);
  for($i=0; $i < count($fle); $i++)
  {
      if( $this->createImgType($oimg) == $fle[$i] )
      {
    $iwTrue = (int) $iwTrue + 1;
      }
  }
  if( $iwTrue == 0 )
  {
      $this->msg("文件不符合 ".$this->allowType." 格式!");
     }
}

// 提示错误信息并终止操作
function msg($Error)
{
     echo "<script language=\"javascript\">\n";
     echo " alert('".$Error."');\n";
     echo " window.history.back();\n";
     echo "</script>\n";
     die();
}
   
    //水印图
function waterImg($oimg,$water="logo.gif",$place="4",$toumindu="75%")
{
  //获得水印图片的资源  
  $data = GetImageSize($water);
     switch($data[2])
     {
     case 1:
       $wimg = @ImageCreateFromGIF($water);
       break;
      case 2:
       $wimg = @ImageCreateFromJPEG($water);
       break;
      case 3:
       $wimg = @ImageCreateFromPNG($water);
       break;
     }
     $wsrcW=ImageSX($wimg);     //水印图片的宽   
     $wsrcH=ImageSY($wimg);    //水印图片的高
     
     switch($place)
     {
      case 0:
       $x=$this->Owidth/2-$wsrcW/2;
       $y=$this->Oheight/2-$wsrcH/2;
       break;
      case 1:
       $x=0;
       $y=0;
       break;
      case 2:
       $x=$this->Owidth-$wsrcW;
       $y=0;
       break;
      case 3:
       $x=0;
       $y=$this->Oheight-$wsrcH;
       break;
      case 4:
       $x=$this->Owidth-$wsrcW;
       $y=$this->Oheight-$wsrcH;
       break;
     }
     $this->CreateFolder($this->waterFolder);    //创建放水印图片的文件夹
     $waterNewName=$this->waterFolder.'/'.$this->createNewImgName($oimg);
  imagecopymerge($this->im,$wimg,$x,$y,0,0,$wsrcW,$wsrcH,$toumindu);
     ImageJpeg($this->im,$waterNewName);
     imagedestroy($this->im);
  if(file_exists($waterNewName))
     {
      return $waterNewName;
     }
     else
     {
      $this->msg("水印生成失败!");
     }

}

//缩略图
function smallImg($oimg,$small_width,$small_height=0,$rate=100)
{  
     $dstX=0;
     $dstY=0;
     if($small_height==0)
     {
      $small_height = $small_width/$this->Owidth*$this->Oheight;
      }
     if ($this->Owidht*$small_height > $this->Oheight*$small_width)
     {
      $fsmall_height = round($this->Oheight*$small_width/$this->Owidth);
      $dstY = floor(($small_height-$fsmall_height)/2);
      $fsmall_width = $small_width;
     }
     else
     {
      $fsmall_width = round($this->Owidth*$small_height/$this->Oheight);
      $dstX = floor(($small_width-$fsmall_width)/2);
      $fsmall_height = $small_height;
     }
     
     $dstX = ($dstX<0)?0:$dstX;
     $dstY = ($dstX<0)?0:$dstY;
     $dstX = ($dstX>($small_width/2))?floor($small_width/2):$dstX;
     $dstY = ($dstY>($small_height/2))?floor($small_height/s):$dstY;
     $this->CreateFolder($this->goodsFolder);    //创建放水印图片的文件夹
     $goodsNewName=$this->goodsFolder.'/'.$this->createNewImgName($oimg);
     $ni = ImageCreateTrueColor($fsmall_width,$fsmall_height);
     imagecopyresampled($ni,$this->im,$dstX,$dstY,0,0,$fsmall_width,$fsmall_height,$this->Owidth,$this->Oheight);
     ImageJpeg($ni,$goodsNewName,$rate);     // 生成缩略图;
     imagedestroy($this->im);           // imagedestroy(resource) 释放image关联的内存
}

//生成图片
function getImg($oimg,$small_width,$dis=0,$small_height=0,$rate=100,$water="logo.gif",$place="4",$toumindu="75%")
{
  $this->chk_ImgType($oimg);
  $this->createimg($oimg);
  
  switch($dis)
  {
   case  0:
    $this->smallImg($oimg,$small_width,$small_height=0,$rate=100);
    break;
   case 1:
    $woi=$this->waterimg($oimg,$water="logo.gif",$place="4",$toumindu="75%");
    $this->chk_ImgType($woi);
    $this->createimg($woi);
    $this->smallImg($woi,$small_width,$small_height=0,$rate=100);
    break;   
  }
}
}

class Upimg extends WaterAndSmallImg
{
public $bigFolder;    //保存上传图片的文件夹
public $maxSize;    // 文件大小 500KB

    function __construct($firstFolder="images")
    {
       parent:: __construct( $firstFolder="images");   
        $this->bigFolder=$this->monthFolder.'/'."big_img";
  $this->maxSize = 200*1024;    //图片大小
    }
   
// 检测文件大小
function chk_fileSize($imgSize)
{
     if( $imgSize > $this->maxSize )
     {
      parent::msg("目标文件不能大于". $this->maxSize/1024 ." KB");
     }
}
   
//保存上传文件
function savefile($imgName,$imgSize,$upImg)
{
     parent::chk_ImgType($imgName);
     $this->chk_fileSize($imgSize);
     return $this->chk_savefile($imgName,$upImg);
}

// 检测上传结果是否成功
function chk_savefile($imgName,$upImg)
{
  parent::CreateFolder($this->bigFolder);         //创建放图片的文件夹
  $bigNewName=$this->bigFolder.'/'.parent::createNewImgName($imgName);
     $copymsg = move_uploaded_file($upImg,$bigNewName);
     if( $copymsg )
     {
      return $bigNewName;
     }
     else
     {
      parent::msg("文件上传失败! \n\n请重新上传! ");
     }
}

//上传并生成缩略图
function saveAllImg($imgName,$imgSize,$upImg,$small_width,$dis=0,$small_height=0,$rate=100,$water="logo.gif",$place="4",$toumindu="75%")
{
  $orImg=$this->savefile($imgName,$imgSize,$upImg);
  parent::getImg($orImg,$small_width,$dis,$small_height=0,$rate=100,$water="logo.gif",$place="4",$toumindu="75%");
}
}
//实例
/*********************************/
//单个图片上传
if($_FILES['poto']['tmp_name']!='')
{
$upImg=$_FILES['poto']['tmp_name'];
$imgName = $_FILES['poto']['name'];        //获取上传的文件名
$imgSize = $_FILES['poto']['size'];  //获取上传的文件大小(字节)
$getUpimg=new Upimg($firstFolder="images");
$getUpimg->saveAllImg($imgName,$imgSize,$upImg,400,1,$small_height=0,$rate=100,$water="logo.gif",$place="4",$toumindu="75%");
}
/*********************************/
?>
<!--单个图片上传实例表单-->
<form action="" method="POST" enctype="multipart/form-data">
选择上传图片:<input type="file" name="poto">
<input type="submit" value="上传" name="submit">
</form>
<!---->
</body>
</html>

作者: fkuepl   发布时间: 2009-04-17

复制代码直接运行及可,首次写类,望各位大位帮忙修改,指点不足之处,并说明为什么要这么改,最好发份改正过的。

作者: fkuepl   发布时间: 2009-04-17

确定能用? 38行报错呢

作者: kisa77   发布时间: 2009-04-17

不会吧,我试过了的呀

作者: fkuepl   发布时间: 2009-04-17

38行有错

作者: farsee   发布时间: 2009-12-20