這個php圖片上傳類別功能非常完善,完全可以滿足各種圖片上傳需求
-
/********************************
- 蹺蹺板同事| http://seesaw.net
-
- 用戶端:
- 文件:
- 說明:
-
- 版權所有(C) 2008 Matt Kenefick(.com)
- ***** ******** ***************************/
-
- class mk_imageUpload{
-
- var $max_size; var $thumb_height;
- var $dest_dir;
- var $extensions;
- var $max_height;
- var $max_main_height;
-
-
- ; var $last_pid;
- var $last_uid;
-
- var $image_file;
- var $image_field;
-
- function __construct( $maxHeightMain, $maxHeightMain, $maxHeight $this->max_size = (1024/2)*1000; // 750kb
- $this->allowed_types = array( 'jpeg', 'jpg', 'pjpeg', 'gif', 'png' );
- $this->extensions = array(
- ' image/jpeg' => '.jpg',
- 'image/gif' => '.gif',
- 'image/png' => '.png',
- ' image/x-png ' => '.png',
- 'image/pjpeg' => '.jpg'
- );
- $this->dest_dir = $destDir;
- $this->max_height = $maxHeightThumb ;
- $this->max_main_height = $maxHeightMain;
- }
-
- function putImage( $formname, $newName ){
- $this-> ;image_field = $formname;
- if ( $this->getImage() ){
-
- // 檢查錯誤
- if ( !$this->checkType() )
- return $this->throwError(2);
-
- if ( !$this->checkFileSize() )
- return $this->throwError(1);
-
-
- // 取得圖片
- $img = $this-> image_file;
-
- // 檢查acquire
- if ( !$this->checkImageSize() )
- return $this -> throwError(3);
-
- // 取得圖片尺寸
- $size = $this->getImageSize();
- $size['width'] = $size[0] ;
- $size['height'] = $size[1];
- $ratio = $this->max_height/$size['height'];
-
- $maxheight = $this ->max_height;
- $maxwidth = $size['width'] * $ratio;
-
- // 建立
- $s_t = $this->resizeImage( $size, $img , $maxwidth, $maxheight,$newName,'s' );
-
- if ( $ $ size['height'] > $this->max_main_height ){
- $ratio = $this->max_main_height/$size['height'];
- $maxheight = $this->max_main_height;
- $ $ maxwidth = $size ['寬度'] * $ratio;
- }else{
- $maxheight = $size['height'];
- $maxwidth = $size['width'];
- }
-
- // 建立大的重新縮放
- $s_l = $this->resizeImage( $size, $img, $maxwidth, $maxheight,$newName,'l' );
-
- // 刪除暫存檔案
- unlink($img['tmp_name']);
-
- if( $s_t && $s_l ){
- $nm = split('_', $newName );
- $this->last_ext = $this->extensions[$size['mime']];
- $this->last_pid = $nm[0];
- $this-> last_uid = $ nm[1];
- return 'ok';
- }else{
- return $this->throwError( 4 );
- }
- }else{
- return $this->throwError( 2 );
- }
- }
-
- function resizeImage($size,$img, $maxwid,$img, $ maxheight,$newName,$ext){
- // 建立大概
- if($size['mime'] == "image/pjpeg" || $size['mime'] == "image / jpeg "){
- $new_img = imagecreatefromjpeg($img['tmp_name']);
- }elseif($size['mime'] == "image/x-png" || $size[' mime ' ] == "image/png"){
- $new_img = imagecreatefrompng($img['tmp_name']);
- }elseif($size['mime'] == "image/gif") {
- $new_img = imagecreatefromgif($img['tmp_name']);
- }
-
- if (function_exists('imagecreatetruecolor')){
- $resized_img = color')){
- $resized_img = 著色;
- }else{
- return("錯誤:請確保您的伺服器有GD 函式庫版本2 ");
- }
-
- imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $maxwidth, $maxheight, $size['width'], $size['height']);
- if(> if(> if(> if(> if(>) $size['mime'] == "image/pjpeg" || $size['mime'] == "image/jpeg"){
- $success = ImageJpeg ($resized_img,$this->dest_dir.$ newName.'_'.$ext.$this->extensions[$size['mime']]);
- }elseif($size['mime'] == "image/x-png" | $newName .'_'.$ext.$this->extensions[$size['mime']]);
- | $size['mime'] == "image/png"){
- $success = ImagePNG ($resized_img,$this->dest_dir.$newName.'_'.$ext.$this-> 擴充[$ size['mime']]);
- }elseif($size['mime'] == "image/gif"){
- $success = ImageGIF ($resized_img,$this->; dest_dir.$ newName.'_'.$ext.$this->extensions[$size['mime']]);
- }
-
- // 刪除臨時映像
- ImageDestroy ($ resized_img);
- ImageDestroy ($new_img);
-
- return $success;
- }
-
- function getImage(){
- if( isset($_FILES[$this-image" && is_uploaded_file($_FILES[$this->image_field]['tmp_name']) ){
- $this->image_file = $_FILES[$this->image_field];
- return true;
- }
- }
- return false;
- }
-
- function returnImg(){
- return $this->image_file;
- }
-
- function getImageSize((MgetImage) $returnImg();
-
- return getimagesize($img['tmp_name']);
- }
-
- function checkImageSize( ){
- $size = $s ->getImageSize();
-
- if( $size[1] max_height )
- return false;
- return true;
- }
-
- 函數checkFileSize(){
- $img = $this->returnImg();
-
- if( $img['size'] > $this->max_size )
- return false;
- return true;
- }
-
- function checkType(){
- $img = $this->returnImg();
-
- $type = split('/',$img[' type']);
- if( !in_array( $type[ 1], $this->allowed_types ) )
- 回傳false;
- 回傳true;
- }
-
- functionthrow> function throwor ($val){
- switch($val){
- case 1: return '錯誤:檔案大小專業';
- break;
- case 2: return '錯誤:檔案格式不正確' ;
- break;
- 情況3:返回'錯誤:您的圖片太小';
- break;
- 情況4:返回'錯誤:創建圖片時出錯';
- break;
- }
- }
-
- }
-
?>
複製程式碼
|