php支援產生縮圖檔案上傳代碼

來源:互聯網
上載者:User

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
</head>

<body>
<?php

class upfileclass {

 var $upfile, $upfile_name, $upfile_size;
 # $upfile 臨時檔案名稱 $_files['tmp_name'] ,$upfile_name 檔案名稱 $_files['name'] ,$upfile_size 檔案大小$_files['size'];

 var $new_upfile_name;   # 上傳後的檔案名稱 ;
 var $fleth, $fileextent; # 副檔名(類型) ;
 var $f1, $f2, $f3;   # 檔案儲存路徑(多級) upfiles/2008-01/08/;
 var $filename;    # 檔案(帶路徑) ;
 var $filepath; #相對路徑用來刪除檔案;
 var $maxsize, $file_type; # 允許上傳檔案的大小 允許上傳檔案的類型 ;

 var $buildfile,$newfile,$file_width,$file_height,$rate;

 function upfileclass($upfile,$upfile_name,$upfile_size){
   $this->upfile = $upfile;
   $this->upfile_name = $upfile_name;
   $this->upfile_size = $upfile_size;
   $this->new_upfile_name = $this->createnewfilename($this->upfile_name);
   $this->f1 = "upfiles";
   $this->f2 = $this->f1."/".date('y')."-".date('m');
   $this->f3 = $this->f2."/".date('d');
   $this->filename = $this->f3 . "/" . $this->new_upfile_name;
   $this->maxsize = 500*1024;    # 檔案大小 500kb
   $this->file_type = "gif/jpg/jpeg/png/bmp"; # 允許上傳的檔案類型
 }

 # 建立新檔案名稱 (原檔案名稱)
 function createnewfilename($file_name){
    $this->fleth = explode(".",$file_name);
    $this->fileextent = $this->fleth[(int)count($this->fleth)-1]; # 擷取檔案尾碼;
    $tmps教程tr = date('ymd').rand(0,time()) . "." .$this->fileextent;    # 建立新檔案名稱;
    return $tmpstr;
 }

 # 檢測檔案類型是否正確
 function chk_fileextent(){
    $iwtrue = 0;
    $fle = explode("/",$this->file_type);
    for($i=0; $i < count($fle); $i++){
     if($this->fileextent == $fle[$i]){
     $iwtrue = (int) $iwtrue + 1;
     }
    }
    if( $iwtrue == 0 ){
  $this->msg("檔案不符合 ".$this->file_type." 格式!");
    }
 }

 # 提示錯誤資訊並終止操作
 function msg($error){
    echo "<script language="網頁特效"> ";
    echo " alert('".$error."'); ";
    echo " window.history.back(); ";
    echo "</script> ";
    die();
 }

 # 儲存檔案
 function savefile(){
    $this->chk_fileextent();
    $this->chk_filesize();
    $this->createfolder( "../".$this->f1 );
    $this->createfolder( "../".$this->f2 );
    $this->createfolder( "../".$this->f3 );
    return $this->chk_savefile();
 }

 # 檢測上傳結果是否成功
 function chk_savefile(){
    $copymsg = copy($this->upfile,"../".$this->filename);
    if( $copymsg ){
   return $this->filename;
    }
    else{
   $this->msg("檔案上傳失敗! 請重新上傳! ");
    }
 }

 # 建立檔案夾
 function createfolder($foldername){
    if( !is_dir($foldername) ){
   mkdir($foldername,0777);
    }
 }

 # 檢測檔案大小
 function chk_filesize(){
    if( $this->upfile_size > $this->maxsize ){
  $this->msg("目標檔案不能大於". $this->maxsize/1024 ." kb");
    }
 }

 # 刪除檔案($filepath 檔案相對路徑)
 function deletefile($filepath){
    if( !is_file($filepath) ){
   return false;
    }
    else{
   $ending = @unlink($filepath);
   return $ending;
    }
 }

 /*
    函數:產生縮圖
  makebuild("/www.111cn.net/a.jpg","news/b.jpg","100");
    參數:
    echo $buildfile;   原圖 帶路徑
    echo $newfile;    產生的縮圖 帶路徑
    echo $file_width;   縮圖寬度值
    echo $file_height;   縮圖高度值 (預設為寬度的比例值)
    echo $rate;     縮圖象品質;
 */
 function makebuild($buildfile,$newfile,$file_width,$file_height=0,$rate=100) {
    if(!is_file($buildfile)){
   $this->msg("檔案 ".$buildfile." 不是一個有效圖形檔案! 系統無法產生該檔案的縮圖!");
   return false;
    }
    $data = getimagesize($buildfile);
    switch($data[2]){
  case 1:
   $im = @imagecreatefromgif($buildfile);
   break;
  case 2:
   $im = @imagecreatefromjpeg($buildfile);
   break;
  case 3:
   $im = @imagecreatefrompng($buildfile);
   break;
    }
    if(!$im){
   return false;
    }
    else{
   $srcw = imagesx($im);  # 取得原圖寬度;
   $srch = imagesy($im); # 取得原圖高度;
   $dstx = 0;
   $dsty = 0;
   
  if($file_height==0){
   $file_height = $file_width/$srcw*$srch;
  }
   
  if ($srcw*$file_height>$srch*$file_width){
   $ffile_height = round($srch*$file_width/$srcw);
   $dsty = floor(($file_height-$ffile_height)/2);
   $ffile_width = $file_width;
  }
  else {
   $ffile_width = round($srcw*$file_height/$srch);
   $dstx = floor(($file_width-$ffile_width)/2);
   $ffile_height = $file_height;
  }
  $ni = imagecreatetruecolor($file_width,$file_height);
  $dstx = ($dstx<0)?0:$dstx;
  $dsty = ($dstx<0)?0:$dsty;
  $dstx = ($dstx>($file_width/2))?floor($file_width/2):$dstx;
  $dsty = ($dsty>($file_height/2))?floor($file_height/s):$dsty;
  imagecopyresized($ni,$im,$dstx,$dsty,0,0,$ffile_width,$ffile_height,$srcw,$srch);
   
  imagejpeg($ni,$newfile,$rate); # 產生縮圖;
  imagedestroy($im);     # imagedestroy(resource) 釋放image關聯的記憶體
    }
 }

}
?>

</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.