PHP之圖片上傳類(加了縮圖),
有縮圖功能 但是 感覺不全面,而且有點問題,繼續學習,將來以後修改下
filename=$filename; $this->fileinfo=$_FILES[$this->filename]; $this->filemax=$filemax; $this->filepath=$filepath; $this->fileflag=$fileflag; $this->fileExt=$fileExt; $this->fileMine=$fileMine; //var_dump($this->filename); } //錯誤判斷 public function UpError(){ if($this->fileinfo['error']>0){ switch($this->fileinfo['error']) { case 1: $this->fileerror="上傳檔案大小超過伺服器允許上傳的最大值,php.ini中設定upload_max_filesize選項限制的值 "; break; case 2: $this->fileerror="上傳檔案大小超過HTML表單中隱藏欄位MAX_FILE_SIZE選項指定的值"; break; case 3: $this->fileerror="檔案部分被上傳"; break; case 4: $this->fileerror="沒有選擇上傳檔案"; break; case 5: $this->fileerror="未找到臨時目錄"; break; case 6: $this->fileerror="檔案寫入失敗"; break; case 7: $this->fileerror="php檔案上傳擴充沒有開啟 "; break; case 8: $this->fileerror=""; break; } return false; } return true; } //檢測檔案類型 public function UpMine(){ if(!in_array($this->fileinfo['type'],$this->fileMine)) { $this->error="檔案上傳類型不對"; return false; } return true; } //檢測檔案格式 public function UpExt(){ $this->ext=pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION); //var_dump($ext); if(!in_array($this->ext,$this->fileExt)){ $this->fileerror="檔案格式不對"; return false; } return true; } //檢測檔案路徑 public function UpPath(){ if(!file_exists($this->filepath)){ mkdir($this->filepath,0777,true); } } //檢測檔案大小 public function UpSize(){ $max=$this->fileinfo['size']; if($max>$this->filemax){ $this->fileerror="檔案過大"; return false; } return true; } //檢測檔案是否HTTP public function UpPost(){ if(!is_uploaded_file($this->fileinfo['tmp_name'])){ $this->fileerror="惡意上償還"; return false; } return true; } //檔案名稱防止重複 public function Upname(){ return md5(uniqid(microtime(true),true)); } //圖片縮圖 public function Smallimg($x=100,$y=100){ $imgAtt=getimagesize($this->path); //映像寬,高,類型 $imgWidth=$imgAtt[0]; $imgHeight=$imgAtt[1]; $imgext=$imgAtt[2]; //等比列縮放 if(($x/$imgWidth)>($y/$imgHeight)){ $bl=$y/$imgHeight; }else{ $bl=$x/$imgWidth; } $x=floor($imgWidth*$bl); //縮放後 $y=floor($imgHeight*$bl); $images=imagecreatetruecolor($x,$y); $big=imagecreatefromjpeg($this->path); imagecopyresized($images,$big,0,0,0,0,$x,$y,$imgWidth,$imgWidth); switch($imgext){ case 1: $imageout=imagecreatefromgif($this->path); break; case 2: $imageout=imagecreatefromjpeg($this->path); break; case 3: $imageout=imagecreatefromgif($this->path); break; } $im=imagejpeg($images,$this->path); } //檔案雙傳 public function uploads() { if($this->UpError()&&$this->UpMine()&&$this->UpExt()&&$this->UpSize()&&$this->UpPost()){ $this->UpPath(); $names=$this->Upname(); $this->path=$this->filepath.'/'. $names.'.'.$this->ext; if(move_uploaded_file($this->fileinfo['tmp_name'], $this->path)){ return $this->path; }else{ $this->fileerror="上傳失敗"; } }else{ exit("".$this->fileerror.""); } }}?>
uploads(); $c=$u->Smallimg();echo "";echo "";?>
Examples
http://www.bkjia.com/PHPjc/1138633.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1138633.htmlTechArticlePHP之圖片上傳類(加了縮圖), 有縮圖功能 但是 感覺不全面,而且有點問題,繼續學習,將來以後修改下 form action="?php $_SERVER['PHP_...