程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php圖片上傳類

php圖片上傳類

編輯:關於PHP編程

class upload_class{
private $ptname;    //上傳表單名稱;
private $udname;    //是否以月份建立子目錄(0為否,其他為真);
private $ufname;    //是否以時間建立文件名(0為否,其他為真);
private $ultype;    //上傳文件類型;
private $ulsize;    //上傳文件大小;
private $ulname;    //輸出文件名稱;
private $ulpath;    //輸出文件路徑;
private $wm;     //水印附加(0為不加,其他為加);
private $wmtype;    //水印類型(0為文字,其他為圖片);
private $wmpic;     //水印圖片;
private $wmpicquality;   //圖片質量;
private $wmpictrans;   //圖片透明;
private $wmstr;     //水印字符;
private $wmstrsize;    //字符大小;
private $wmstrfont;    //字符字體;
private $wmstrcolor;   //字符顏色;
private $wmpos;     //水印位置(1為頂端居左,2為頂端居中,3為頂端居右……);
function __construct($ptname='upfile',$udname=1,$ufname=1,$ultype=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png'),$wm=1,$wmtype=1,$wmpic='images/wm.gif',$ulsize=2097152,$ulpath='images/temp/',$wmpictrans=20,$wmpicquality=80,$wmstr='DONLINE',$wmstrsize=5,$wmstrfont='./font/cour.ttf',$wmstrcolor='#ff0000',$wmpos=9){
  $this->ptname=$_FILES[$ptname];
  $this->udname=$udname;
  $this->ufname=$ufname;
  $this->ultype=$ultype;
  $this->ulsize=$ulsize;
  $this->ulpath=$ulpath;
  $this->wm=$wm;
  $this->wmtype=$wmtype;
  $this->wmpic=$wmpic;
  $this->wmpicquality=$wmpicquality;
  $this->wmpictrans=$wmpictrans;
  $this->wmstr=$wmstr;
  $this->wmstrsize=$wmstrsize;
  $this->wmstrfont=$wmstrfont;
  $this->wmstrcolor=$wmstrcolor;
  $this->wmpos=$wmpos;
}
function uploadfun(){
  if($_SERVER['REQUEST_METHOD']=='POST'){
   if(!is_uploaded_file($this->ptname['tmp_name']))$this->errorfun('上傳失敗!');
   if(!in_array($this->ptname['type'],$this->ultype))$this->errorfun('不支持的文件類型!');
   if($this->ulsize<$this->ptname['size'])$this->errorfun('文件太大!');
   if($this->udname){date_default_timezone_set('UTC');$this->ulpath=$this->ulpath.'month_'.date('Ym').'/';}
   else{$this->ulpath=$this->ulpath;}
   $this->createfun($this->ulpath);
   if($this->ufname){$t=pathinfo($this->ptname['name']);$this->ulname=$this->ulpath.time().'.'.$t['extension'];}
   else{$this->ulname=$this->ulpath.$this->ptname['name'];}
   if(file_exists($this->ulname))$this->errorfun('該文件已存在!');
   if(!move_uploaded_file($this->ptname['tmp_name'],$this->ulname))$this->errorfun('移動文件錯誤!');
   $this->wmfun();
   $this->errorfun('上傳成功!');
  }
}
function createfun($d){
  if(!file_exists($d)){$this->createfun(dirname($d));mkdir($d);}
}
function wmfun(){
  if($this->wm){
   if(file_exists($this->ulname)){
    $groundimg=getimagesize($this->ulname);
    $ow=$groundimg[0];
    $oh=$groundimg[1];
    switch($groundimg[2]){
     case 1:$g=imagecreatefromgif($this->ulname);break;
     case 2:$g=imagecreatefromjpeg($this->ulname);break;
     case 3:$g=imagecreatefrompng($this->ulname);break;
     case 4:$g=imagecreatefromwbmp($this->ulname);break;
     default:$this->errorfun('不支持的背景圖片類型!');
    }
   }
   else{$this->errorfun('背景圖片不存在!');}
   if(file_exists($this->wmpic)){
    $wmimg=getimagesize($this->wmpic);
    $ww=$wmimg[0];
    $wh=$wmimg[1];
    switch($wmimg[2]){
     case 1:$w=imagecreatefromgif($this->wmpic);break;
     case 2:$w=imagecreatefromjpeg($this->wmpic);break;
     case 3:$w=imagecreatefrompng($this->wmpic);break;
     case 4:$w=imagecreatefromwbmp($this->wmpic);break;
     default:$this->errorfun('不支持的水印圖片類型!');
    }
   }
   else{$this->errorfun('水印圖片不存在!');}
   switch($this->wmtype){
    case 0:$tp=imagettfbbox(ceil($this->wmstrsize*2.5),0,$this->wmstrfont,$this->wmstr);$ww=$tp[2]-$tp[6];$wh=$tp[3]-$tp[7];unset($tp);break;
    case 1:$ww=$ww;$wh=$wh;break;
    default:$ww=$ww;$wh=$wh;break;
   }
   if($ow<$ww || $oh<$wh)$this->errorfun('背景圖片太小!無法生成水印!');
   switch($this->wmpos){  
    case 0:$x=rand(0,($ow-$ww));$y=rand(0,($oh-$wh));break;//隨機
          case 1:$x=0;$y=0;break;//1為頂端居左     
          case 2:$x=($ow-$ww)/2;$y=0;break;//2為頂端居中     
          case 3:$x=$ow-$ww;$y=0;break;//3為頂端居右     
          case 4:$x=0;$y=($oh-$wh)/2;break;//4為中部居左     
          case 5:$x=($ow-$ww)/2;$y=($oh-$wh)/2;break;//5為中部居中   
          case 6:$x=$ow-$ww;$y=($oh-$wh)/2;break;//6為中部居右     
          case 7:$x=0;$y=$oh-$wh;break;//7為底端居左  
          case 8:$x=($ow-$ww)/2;$y=$oh-$wh;break;//8為底端居中     
          case 9:$x=$ow-$ww;$y=$oh-$wh;break;//9為底端居右     
          default:$x=rand(0,($ow-$ww));$y=rand(0,($oh-$wh));break;//隨機         
      }
   imagealphablending($g, true);
   switch($this->wmtype){
    case 0:
    if($this->wmstrcolor){$R=hexdec(substr($this->wmstrcolor,1,2));$G=hexdec(substr($this->wmstrcolor,3,2));$B=hexdec(substr($this->wmstrcolor,5));}
    else{$this->errorfun('水印文字顏色不存在!');}
    imagestring($g,$this->wmstrfont,$x,$y,$this->wmstr,imagecolorallocate($g,$R,$G,$B));break;
    case 1;imagecopymerge($g,$w,$x,$y,0,0,$ww,$wh,$this->wmpictrans);break;
    default:imagecopymerge($g,$w,$x,$y,0,0,$ww,$wh,$this->wmpictrans);break;
   }
   @unlink($this->ulname);
   switch($groundimg[2]){
          case 1:imagegif($g,$this->ulname);break;
          case 2:imagejpeg($g,$this->ulname,$this->wmpicquality);break;
          case 3:imagepng($g,$this->ulname);break;
    case 4:imagewbmp($g,$this->ulname);break;
          default:$this->errorfun('生成圖片失敗!');
   }
   if(isset($wmimg))unset($wmimg);
      if(isset($w))imagedestroy($w);
      unset($groundimg);
      imagedestroy($g);
  }
}
function errorfun($e='未知錯誤!'){
  $msg='<script language="javascript" type="text/javascript">';
  $msg.='alert("'.$e.'");';
  $msg.='history.back();';
  $msg.='</script>';
  echo $msg;
  exit;
}
}
?>


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved