php上傳檔案並產生縮圖的函數

來源:互聯網
上載者:User
php 上傳檔案並產生縮圖的代碼,分為單檔案與多檔案上傳,並可以產生縮圖,確實不錯,建議大家參考學習下。

完整代碼如下。

* ******************************************************/function UpLoadFileAll($input='UpPic', $path='upload', $ftype='jpg,gif,png', $fsize=2){$fileInfo = $_FILES[$input];                           //檔案資訊if(strrpos($path, '/') < strlen($path)-1) $path .= '/';   //上傳檔案夾.$myfile = CreatMyFile($path);                             //建立檔案夾if($myfile==false) return false;$Atype = explode(',', $ftype);                            //檔案類型.$fsize = $fsize*1048576;                                  //(1024*1024==1048576=1M)限檔案大小,按位元組.    $js = "以下檔案上傳成功:\\n\\n";if(is_array($fileInfo["error"])){   foreach ($fileInfo["error"] as $key => $error){     if ($error == 0) {           $name = $fileInfo["name"][$key];              //用戶端機器檔案的原名稱.     $size = $fileInfo["size"][$key];              //上傳檔案的大小,單位為位元組.     $type = $fileInfo["type"][$key];              //上傳檔案類型.     $tmp_name = $fileInfo["tmp_name"][$key];      //檔案被上傳後在服務端儲存的臨時檔案名稱.     $type = MyFileType($type);                    //檢測上傳檔案類型.     $path = $myfile.MakeFname($type);             //檔案路徑包括檔案名稱.          if(in_array($type, $Atype) && $size<=$fsize){     if(@move_uploaded_file($tmp_name, $path)){      $array[] = $path;      $js .= " ".$name." 上傳成功 !\\n";        }     }     }   }}echo "";return $array;}/****************************************************************************** 重設圖片尺寸大小:ResizeImage(原圖片路徑,縮圖(最大)寬度,縮圖(最大)高度)* 傳回值: *     失敗返回: FLASH.*     成功返回:縮圖路徑.*****************************************************************************/function ResizeImage($path, $maxwidth, $maxheight){$picS = substr($path, -3);$name = substr($path, 0, strrpos($path, '.')).'_S';switch($picS){case 'jpg':    $im = @imagecreatefromjpeg($path);     break;case 'gif':    $im = @imagecreatefromgif($path);     break;default:    $im = @imagecreatefrompng($path);}$width = imagesx($im); $height = imagesy($im); if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){   if($maxwidth && $width > $maxwidth){    $widthratio = $maxwidth/$width;     $RESIZEWIDTH=true;     }//end if    if($maxheight && $height > $maxheight){     $heightratio = $maxheight/$height;      $RESIZEHEIGHT=true;     }//end if    if($RESIZEWIDTH && $RESIZEHEIGHT){     if($widthratio < $heightratio){      $ratio = $widthratio;      }else{      $ratio = $heightratio;      }     }elseif($RESIZEWIDTH){     $ratio = $widthratio;     }elseif($RESIZEHEIGHT){     $ratio = $heightratio;     }//end if    $newwidth = $width * $ratio;     $newheight = $height * $ratio;     if(function_exists("imagecopyresampled")){     $newim = imagecreatetruecolor($newwidth, $newheight);      imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);     }else{     $newim = imagecreate($newwidth, $newheight);      imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);     }//end if}else{   $newim = $im;}//end if       switch($picS){     case 'jpg':        $PicPath = $name.".jpg";     if(imagejpeg($newim, $PicPath)){       imagedestroy($newim);     return str_replace(array('../','./'), '', $PicPath);     }else{     return false;     }     break;     case 'gif':        $PicPath = $name.".gif";     if(imagegif($newim, $PicPath)){       imagedestroy($newim);     return str_replace(array('../','./'), '', $PicPath);     }else{     return false;     }     break;     default:        $PicPath = $name.".png";        if(imagepng($newim, $PicPath)){       imagedestroy($newim);     return str_replace(array('../','./'), '', $PicPath);     }else{     return false;     }    }//end switch}//end function/*************************** 檔案屬性 $type = 檔案屬性***************************/function MyFileType($type) {    $type = strtolower($type);switch($type) {      //OFFICE   case 'application/msword' :    $type = 'doc';   break;   case 'application/vnd.ms-excel':   $type = 'xls';   break;   case 'application/vnd.ms-powerpoint':    $type = 'ppt';   break;   //壓縮   case 'application/octet-stream':    $type = 'rar';   break;   //文本   case 'text/plain':    $type = 'txt';   break;   //圖片   case 'image/pjpeg':    $type = 'jpg';   break;   case 'image/gif':    $type = 'gif';   break;   case 'image/x-png':    $type = 'png';   break;   case 'image/bmp':    $type = 'bmp';   break;   default :    $type = 'err';}return $type; //返迴文件類型.}/******************* 建立檔案夾(路徑)*******************/function CreatMyFile($fname=''){switch($fname){   case '':    break;   default:    if(strrpos($fname, '/') < strlen($fname)-1) $fname .= '/';}$fname .= date("Y-m");if(is_dir($fname)) return $fname.'/';   if(mkdir($fname, 0755)==false) return false;//if(chmod($fname, 0777)==false) return false;return $fname.'/';}/****************************** 組建檔案名* $fname ==> 檔案名稱* $ftype   ==> 檔案類型*****************************/function MakeFname($ftype) {  $fname = date("mdHis").'_'.rand(100000, 999999);return $fname.'.'.$ftype;}?>
  • 聯繫我們

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