php 圖片上傳代碼(具有產生縮圖與增加浮水印功能)

來源:互聯網
上載者:User

 

 代碼如下 複製代碼

class upfile {
 public $filepath = "www.111cn.net/"; //上傳檔案存放檔案夾

 public $filesize = 1000000; //允許上傳的大小

 //如果要修改允許上傳檔案的類型  請搜尋 【 switch ($upfiletype) { //檔案類型  】

 public $reimagesize = array (
  true, //是否產生縮圖
  400, //縮圖寬
  300,//縮圖高
  "" //縮圖存放檔案夾 如果為空白和當前要產生縮圖的檔案在同一目錄 檔案首碼r_
 ); //是否產生縮圖 array(產生或不產生,縮圖寬,縮圖高,存放檔案夾); 注意:存放檔案夾後跟 '/'

 public $india = true; //是否打浮水印 true打 false不打

 public $indiaimage = ""; //浮水印圖片地址為空白則不打圖片浮水印 如果有文字浮水印建議不要開啟圖片浮水印

 public $indiaimagex = 100; //圖片距離圖片左邊距離

 public $indiaimagey = 10; //圖片距離圖片上面距離

 public $indiatext = "www.111cn.net"; //浮水印文字

 public $fontsize = 6; //浮水印文字大小,1最小6最大

 public $indiatextx = 10; //文字距離圖片左邊距離

 public $indiatexty = 10; //文字距離圖片上面距離

 public $r = 250; //圖片顏色三原色 $r紅

 public $g = 250; //$g綠

 public $b = 250; //$b藍

 public $indiapath = ""; //加了浮水印的圖片儲存路徑,如果為空白就直接替代原來的圖片

 //開始上傳處理
 function uploadfile($upfile) {
  if ($upfile == "") {
   die("uploadfile:參數不足");
  }
  if (!file_exists($this->filepath)) {
   mkdir($this->filepath);
  }
  $upfiletype = $upfile['type'];
  $upfilesize = $upfile['size'];
  $upfiletmpname = $upfile['tmp_name'];
  $upfilename = $upfile['name'];
  $upfileerror = $upfile['error'];
  if ($upfilesize > $this->filesize) {
   return false; //檔案過大
  }
  switch ($upfiletype) { //檔案類型
   case 'image/jpeg' :
    $type = 'jpg';
    break;
   case 'image/pjpeg' :
    $type = 'jpg';
    break;
   case 'image/png' :
    $type = 'png';
    break;
   case 'image/gif' :
    $type = 'gif';
    break;
  }
  if (!isset ($type)) {
   return false; //不支援此類型
  }
  if (!is_uploaded_file($upfiletmpname) or !is_file($upfiletmpname)) {
   return false;
   ; //檔案不是經過正規上傳的;
  }
  if ($this->upfileerror != 0) {
   return false; //其他錯誤
  }
  if ($this->upfileerror == 0) {
   if (!file_exists($upfiletmpname)) {
    return false; //臨時檔案不存在
   } else {
    $filename = date("ymdhis", time() + 3600 * 8); //圖片已目前時間命名
    $filename = $this->filepath . $filename . "." . $type;
    if (!move_uploaded_file($upfiletmpname, $filename)) {
     return false; //檔案在移動中丟失
    } else {
     if ($this->india == true) {
      $this->goindia($filename, $type,true);
     } else {
      if ($this->reimagesize[0] == true) {
       $this->goreimagesize($filename, $type);
      } else {
       return true; //上傳成功!
       unlink($upfiletmpname);
      }
     }
    }

   }
  }

 }
 //添加浮水印處理
 function goindia($filename, $filetype,$reimage=false) {
  if (!file_exists($filename)) {
   $this->reerror(7); //要添加浮水印的檔案不存在
  } else {
   if ($filetype == "jpg") {
    $im = imagecreatefromjpeg($filename);
   } else
    if ($filetype == "gif") {
     $im = imagecreatefromgif($filename);
    } else
     if ($filetype == "png") {
      $im = imagecreatefrompng($filename);
     }
   if ($this->indiatext != "") { //如果浮水印文字不為空白
    $textcolor = imagecolorallocate($im, $this->r, $this->g, $this->b); //設定文字顏色
    imagestring($im, $this->fontsize, $this->indiatextx, $this->indiatexty, $this->indiatext, $textcolor); //將文字寫入圖片
   }
   if ($this->indiaimage != "") {//如果浮水印圖片不為空白
    $indiaimagetype = getimagesize($this->indiaimage);
    $logow = $indiaimagetype[0]; //得到浮水印圖片的寬
    $logoh = $indiaimagetype[1]; //得到浮水印圖片的高
    switch ($indiaimagetype[2]) { //判斷浮水印圖片的格式
     case 1 :
      $indiaimagetype = "gif";
      $logo = imagecreatefromgif($this->indiaimage);
      break;
     case 2 :
      $indiaimagetype = "jpg";
      $logo = imagecreatefromjpeg($this->indiaimage);
      break;
     case 3 :
      $indiaimagetype = "png";
      $logo = imagecreatefrompng($this->indiaimage);
      break;
    }
    imagealphablending($im, true); //開啟混色模式
    imagecopy($im, $logo, $this->indiaimagex, $this->indiaimagey, 0, 0, $logow, $logoh);
    imagedestroy($im);
    imagedestroy($logo);
   }
  }
  if ($this->indiapath == "") { //如果浮水印存放地址不為空白
   if ($filetype == "jpg") {
    imagejpeg($im, $filename);
   } else
    if ($filetype == "gif") {
     imagegif($im, $filename);
    } else
     if ($filetype == "png") {
      imagepng($im, $filename);
     }
   if($reimage == true){
    $this->goreimagesize($filename,$filetype);
   }else{
    return true; //添加浮水印成功
   }
  } else {
   if (!file_exists($this->indiapath)) {
    mkdir($this->indiapath);
    return false; //請重新上傳
   } else {
    $indianame = basename($filename);
    $indianame = $this->indiapath . $indianame;
    if ($filetype == "jpg") {
     imagejpeg($im, $indianame);
    } else
     if ($filetype == "gif") {
      imagegif($im, $indianame);
     } else
      if ($filetype == "png") {
       imagepng($im, $indianame);
      }
    if($reimage == true){
     $this->goreimagesize($indianame,$filetype);
     echo $indianame;
    }else{
     return true; //添加浮水印成功
    }
   }
  }
 }
 function goreimagesize($filename, $filetype) {
  if (!file_exists($filename)) {
   return false; //要產生縮圖的圖片不存在
  } else {
   if ($filetype == 'jpg') {
    $reimage = imagecreatefromjpeg($filename);
   }
   elseif ($filetype == 'png') {
    $reimage = imagecreatefrompng($filename);
   } else
    if ($filetype == 'gif') {
     $reimage = imagecreatefromgif($filename);
    }
   if (isset ($reimage)) {
    $srcimagetype = getimagesize($filename);
    $srcimagetypew = $srcimagetype[0]; //得到原始圖片寬度
    $srcimagetypeh = $srcimagetype[1]; //得到原始圖片高度
    $reim = imagecreatetruecolor($this->reimagesize[1], $this->reimagesize[2]);
    imagecopyresized($reim, $reimage, 0, 0, 0, 0, $this->reimagesize[1], $this->reimagesize[2], $srcimagetypew, $srcimagetypeh);
    $reimagepath = $this->reimagesize[3];
    if ($reimagepath != "") { //如果存放浮水印地址不為空白
     if (!file_exists($reimagepath)) {
      mkdir($reimagepath);
     } else {
      $reimagename = basename($filename);
      $reimagename = $reimagepath . "r_" . $reimagename;
      if ($filetype == "gif")
       imagegif($reim, $reimagename);
      else
       if ($filetype == "jpg")
        imagejpeg($reim, $reimagename);
       else
        if ($filetype == "png")
         imagepng($reim, $reimagename);
      return true;
     }
    } else {
     $filename = basename($filename);
     if($this->indiapath == ""){
      $filename = $this->filepath."r_" . $filename;
     }else{
      $filename = $this->indiapath."r_" . $filename;
     }
     if ($filetype == "gif")
      imagegif($reim, $filename);
     else
      if ($filetype == "jpg")
       imagejpeg($reim, $filename);
      else
       if ($filetype == "png")
        imagepng($reim, $filename);
     return true;
    }

   }
  }
 }

}
if ($_post["submit"]) {
 $file = $_files['uploadfile'];
 $upfile = new upfile();
 echo $upfile->uploadfile($file);
}
?>

  <form action="" method="post" enctype="multipart/form-data">
  <input type="file" name="uploadfile"/><br/>
  <input type="submit" value="上傳" name="submit"/>
  </form>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.