產生縮圖的PHP上傳圖片類

來源:互聯網
上載者:User

標籤:tag   nbsp   壓縮   type   err   lis   rto   通過   his   

做圖片類網站的朋友可能都有自己上傳圖片的方式方法。這裡給大家介紹一個php上傳圖片類,它可以驗證上傳檔案是否為圖片,圖片的格式是否是網站所接受的;還可以產生指定大小的縮圖。具體方法如下所示:


<?php
class UploadComponent {

 var $imageData = array();
 var $destinationDir = NULL;
 var $thumbDestinationDir = NULL;
 var $imageSize = array(500,500);
 var $cropImageSize = array(90,90);
 var $tag = ‘width‘;
 var $imageExt = array(‘jpg‘,‘gif‘,‘png‘,‘bmp‘,‘jpeg‘);

 function initialize($arr)
 {
  $this->imageData = $arr;
 }

 function isImageFile()
 {
  $ext = $this->imageExtension();

  if(in_array(strtolower($ext),$this->imageExt))
  {
   return true;
  }
  else
  {
   return false;
  }
 }

 function isValidImage()
 {
  if($this->imageData[‘error‘] == 0 && $this->imageData[‘name‘] <> ‘‘)
  {
   if($this->isImageFile())
   {
    return true;
   }
   else
   {
    return false;
   }
  }
  else
  {
   return false;
  }
 }

 function imageExtension()
 {
  return trim(substr($this->imageData[‘name‘],strpos($this->imageData[‘name‘],‘.‘)+1,strlen($this->imageData[‘name‘])));
 }

 function upload($thumb=false)
 {
  $this->__upload($this->destinationDir,$this->imageSize[0],$this->imageSize[1]);
  if($thumb)
  {
   $this->__upload($this->thumbDestinationDir,$this->cropImageSize[0],$this->cropImageSize[1]);
  }
 }

 function __upload($filethumb,$Twidth,$Theight)
 {
  list($width,$height,$type,$attr)=getimagesize($this->imageData[‘tmp_name‘]);
  switch($type)
  {
   case 1:
    $img = imagecreatefromgif($this->imageData[‘tmp_name‘]);
   break;
   case 2:
    $img=imagecreatefromjpeg($this->imageData[‘tmp_name‘]);
   break;
   case 3:
    $img=imagecreatefrompng($this->imageData[‘tmp_name‘]);
   break;
  }

  if($this->tag == "width") //根據寬度來壓縮圖片
  {
   $Theight=round(($height/$width)*$Twidth);
  }
  elseif($this->tag == "height") //根據高度來壓縮圖片
  {
   $Twidth=round(($width/$height)*$Theight);
  }
  else
  {
   if($width > $height)
    $Theight=round(($height/$width)*$Twidth);
   else
    $Twidth=round(($width/$height)*$Theight);
  }
  $thumb=imagecreatetruecolor($Twidth,$Theight);

  if(imagecopyresampled($thumb,$img,0,0,0,0,$Twidth,$Theight,$width,$height))
  {
   switch($type)
   {
    case 1:
     imagegif($thumb,$filethumb);
    break;
    case 2:
     imagejpeg($thumb,$filethumb,100);
    break;
    case 3:
     imagepng($thumb,$filethumb);
    break;
   }
   chmod($filethumb,0666);
   return true;
  }
 }
}
?>

這個類的使用方法很簡單,將上傳檔案$_FILES[‘html表單中上傳項的name‘]這個變數作為執行個體化這個類的變數傳遞給建構函式。之後就可以用其中的方法來處理圖片了。當使用upload($thumb=false)函數的時候,$thumb=false為不產生縮圖,反之則產生縮圖;圖片以及縮圖的大小可以通過 $imageSize、$cropImageSize這兩個變數來指定,預設是500*500和90*90,朋友們可以根據自己的需求來改變他們。

產生縮圖的PHP上傳圖片類

聯繫我們

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