PHP上傳圖片類顯示縮略圖功能
更新時間:2016年06月30日 08:57:13 作者:塵夢
這篇文章主要介紹了PHP上傳圖片類顯示縮略圖功能的相關(guān)資料,重點(diǎn)強(qiáng)調(diào)下帶有縮鏍圖,但是感覺不全面,后續(xù)還會繼續(xù)修改,需要的朋友可以參考下
有縮略圖功能 但是 感覺不全面,而且有點(diǎn)問題,繼續(xù)學(xué)習(xí),將來以后修改下
<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post" ><input type="text" name="name" /><input type="file" name="file" /><input type="submit" name='submit' value="提交" ></form> <?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/6/28 * Time: 21:04 */ class upload{ protected $fileMine;//文件上傳類型 protected $filepath;//文件上傳路徑 protected $filemax;//文件上傳大小 protected $fileExt;//文件上傳格式 protected $filename;//文件名 protected $fileerror;//文件出錯設(shè)置 protected $fileflag;//文件檢測 protected $fileinfo; //FILES protected $ext; //文件擴(kuò)展 protected $path; //文件上傳 public function __construct($filename="file",$filemax=20000000,$filepath="./Uploads",$fileflag=true,$fileExt=array('jpg','exe'),$fileMine=array('image/jpeg')) { $this->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="上傳文件大小超過服務(wù)器允許上傳的最大值,php.ini中設(shè)置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文件上傳擴(kuò)展沒有打開 "; 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; } //文件名防止重復(fù) 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("<b>".$this->fileerror."</b>"); } } } <?php header("content-type:imagejpeg"); header("Content-type:text/html;charset=utf-8"); require 'list.php'; $u=new upload(); $a=$u->uploads(); $c=$u->Smallimg(); echo "<img src={$a} />"; echo "<img src={$c} />"; ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Examples</title> <meta name="description" content=""> <meta name="keywords" content=""> <link href="" rel="stylesheet"> </head> <body> <form action="ce.php" enctype="multipart/form-data" method="post" > <input type="text" name="name" /><input type="file" name="file" /> <input type="submit" name='submit' value="提交" > </form> </body> </html>
以上所述是小編給大家介紹的PHP上傳圖片類顯示縮略圖功能的相關(guān)知識,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Laravel模型間關(guān)系設(shè)置分表的方法示例
這篇文章主要給大家介紹了關(guān)于Laravel模型間關(guān)系設(shè)置分表的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04PHP生成迅雷、快車、旋風(fēng)等軟件的下載鏈接代碼實例
這篇文章主要介紹了PHP生成迅雷、快車、旋風(fēng)等軟件的下載鏈接實例,需要的朋友可以參考下2014-05-05PHPMailer發(fā)送HTML內(nèi)容、帶附件的郵件實例
這篇文章主要介紹了PHPMailer發(fā)送HTML內(nèi)容、帶附件的郵件實例,發(fā)送的內(nèi)容包含圖片和文字,附件則發(fā)送的一個EXCEL表,需要的朋友可以參考下2014-07-07

基于curl數(shù)據(jù)采集之單頁面并行采集函數(shù)get_htmls的使用
用第一篇的get_html()實現(xiàn)簡單的數(shù)據(jù)采集,由于是一個一個執(zhí)行才采集數(shù)據(jù)的傳輸時間就會是所有頁面下載的總時長,一個頁面假設(shè)1秒,那么10個頁面就是10秒了。所幸curl還提供了并行處理的功能
2013-04-04