图片上传类(ジKingべ提供)

#1 tealin

class uploadPic{
        private $FILE_NAME; //表单input type=file的name属性
        private $PIC_PATH;  //图片存放路径(不包括文件名)
        private $PIC_NAME;  //图片存放路径(包括文件名)
        private $PIC_SIZE;  //图片大小限制
        private $TYPE;      //允许的类型
        private $MAX_WIDTH; //允许的最大宽度
        private $MAX_HEIGHT;//允许的最大高度
        private $FLAG=false;//开关
        private $NOTICE_L;  //提示方式左定界
        private $NOTICE_R;  //提示方式右定界
        public function __construct($file,$path,$size,$max_width=null,$max_heigth=null,$left=null,$right=null,$type=array('gif','jpg','png')){
                $this->FILE_NAME=$file;
                $this->PIC_PATH=$path;
                $this->PIC_SIZE=$size;
                $this->TYPE=$type;
                $this->MAX_WIDTH=$max_width;
                $this->MAX_HEIGHT=$max_heigth;
                $this->NOTICE_L=$left;
                $this->NOTICE_R=$right;
                $this->check();
                $this->upload();
                $this->resize();
        }
        private function check(){
                if(preg_match('/^image/',$_FILES[$this->FILE_NAME]["type"])==false) echo $this->notice("非图片类型");
                elseif(is_array($this->TYPE)) {
                        $type=$this->get_type($_FILES[$this->FILE_NAME]["tmp_name"]);
                        $flag=false;
                        foreach($this->TYPE as $x){
                                if($x==$type) $flag=true;
                        }
                        if(!$flag) echo $this->notice("不支持的图片类型");
                        elseif($_FILES[$this->FILE_NAME]["size"]>$this->PIC_SIZE) echo $this->notice("超过最大限制");
                        elseif($_FILES[$this->FILE_NAME]["error"]>0) echo $this->notice("上传失败");
                        else $this->FLAG=true;
                }
                else echo $this->notice("指定允许的类型应为数组形式");
        }
        private function upload(){
                if($this->FLAG==true){
                        $this->__mkdirs($this->PIC_PATH);
                        $this->PIC_NAME=$this->PIC_PATH.'/'.$this->makename();
                        move_uploaded_file($_FILES[$this->FILE_NAME]["tmp_name"],$this->PIC_NAME);
                }
        }
        private function makename(){  //生成新图片名
                $type=$this->get_type($_FILES[$this->FILE_NAME]["tmp_name"]);
                $pic=date("YmdHis").mt_rand().'.'.$type;
                return $pic;
        }
        private function get_type($filename){  //获取真实类型
                $type=exif_imagetype($filename);
                switch($type){
                        case 1: $ext='gif';
                                break;
                        case 2: $ext='jpg';
                                break;
                        case 3: $ext='png';
                                break;
                }
                return $ext;
        }
        private function resize(){   //根据限制裁剪图片
                if($this->MAX_WIDTH!=null || $this->MAX_HEIGHT!=null){
                        list($width,$height) = getimagesize($this->PIC_NAME);
                        if($this->MAX_WIDTH==null && $height>$this->MAX_HEIGHT) $scale=$height/$this->MAX_HEIGHT; //只限高
                        elseif($this->MAX_HEIGHT==null && $width>$this->MAX_WIDTH) $scale=$width/$this->MAX_WIDTH;    //只限宽
                        elseif($width>$this->MAX_WIDTH || $height>$this->MAX_HEIGHT){
                                $scaleX=$width/$this->MAX_WIDTH;
                                $scaleY=$height/$this->MAX_HEIGHT;
                                $scale=($scaleX>$scaleY)?$scaleX:$scaleY;
                        }
                        if($scale!=null){
                                $new_width=$width/$scale;
                                $new_height=$height/$scale;
                                $new_image=imagecreatetruecolor($new_width,$new_height);
                                $type=$this->get_type($this->PIC_NAME);
                                if($type=='gif') $source=imagecreatefromgif($this->PIC_NAME);
                                if($type=='jpg') $source=imagecreatefromjpeg($this->PIC_NAME);
                                if($type=='png') $source=imagecreatefrompng($this->PIC_NAME);
                                imagecopyresampled($new_image,$source,0,0,0,0,$new_width,$new_height,$width,$height);
                                unlink($this->PIC_NAME);
                                if($type=='gif') imagegif($new_image,$this->PIC_NAME);
                                if($type=='jpg') imagejpeg($new_image,$this->PIC_NAME,90);
                                if($type=='png') imagepng($new_image,$this->PIC_NAME);
                        }
                }
        }
        private function notice($msg){
                if($this->NOTICE_L!=null && $this->NOTICE_R!=null) return $this->NOTICE_L.$msg.$this->NOTICE_R;
                else return $msg;
        }
        private function __mkdirs($dir, $mode = 0777){
                if (!is_dir($dir)) {
                        $this->__mkdirs(dirname($dir), $mode);
                        return @mkdir($dir,$mode);
                }
        return true;
        }
        public function __get($name){
                return $this->$name;
        }
}

/*
demo1:
$u=new uploadPic('pic','uploads/file',1024*1024);
demo2:
$uploadpic=new uploadPic('mypic',$path,1024*1024*2,500,null,'',array('jpg','gif'));
*/
?>

还是粘上来吧,以后用的时候方便来找,qq太不方便了!

2010-11-13 19:59:17

#2 ジKingべ

Thanks for your pasted.
PS: $PIC_PATH would be absolute path to avoid error.
For example:
With SpeedPHP framework
$path=APP_PATH.'/upload';
$u=new uploadPic('pic',$path,1024*1024);
$u->PIC_NAME; //Get the picture name(inlude path);
BTW,为了装B,用英语写起来真不顺手
:lol

2010-11-13 20:43:08

#3 prince

不用private前面那几个变量行不行?

2010-11-13 21:46:17