中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

PHP 圖片上傳類并生成縮略圖

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
<?PHP
/**
* 上傳圖片
*/
class imgUpload{
        static protected $a;
        protected $formName;        //表單名稱
        protected $directory;        //文件上傳至目錄
        protected $maxSize;                //最大文件上傳大小
        protected $canUpload;        //是否可以上傳
        protected $doUpFile;        //上傳的文件名
        protected $sm_File;                //縮略圖名稱
         
        private function __construct($_formName='file', $_directory='./images/uploads/', $_maxSize=1048576){                        //1024*1024=1M
                //初始化參數(shù)
                $this->formName = $_formName;
                $this->directory = $_directory;
                $this->maxSize = $_maxSize;
                $this->canUpload = true;
                $this->doUpFile = '';
                $this->sm_File = '';
        }
         
        //判斷圖片是否屬于允許格式內(nèi)
        static public function Type($_formName='file'){
                $_type = $_FILES[$_formName]['type'];
                switch ($_type){
                        case 'image/gif':
                                if (self::$a==NULL)
                                        self::$a = new imgUpload($_formName);
                                break;
                        case 'image/pjpeg':
                                if (self::$a==NULL)
                                        self::$a = new imgUpload($_formName);
                                break;
                        case 'image/x-png':
                                if (self::$a==NULL)
                                        self::$a = new imgUpload($_formName);
                                break;
                        default:
                                self::$a = false;
                }
                return self::$a;
        }
         
        //獲取文件大小
        public function getSize($_format='K'){
                if ($this->canUpload) {
                        if (0 == $_FILES[$this->formName]['size']) {
                                $this->canUpload = false;
                                return $this->canUpload;
                                break;
                        }
                        switch ($_format){
                                case 'B':
                                        return $_FILES[$this->formName]['size'];
                                        break;
                                case 'K':
                                        return round($_FILES[$this->formName]['size'] / 1024);
                                        break;
                                case 'M':
                                        return round($_FILES[$this->formName]['size'] / (1024*1024),2);
                                        break;
                        }
                }       
        }
         
        //獲取文件類型
        public function getExt(){
                if ($this->canUpload) {
                        $_name = $_FILES[$this->formName]['name'];
                        $_nameArr = explode('.',$_name);
                        $_count = count($_nameArr)-1;
                }
                return $_nameArr[$_count];
        }
         
        //獲取文件名稱
        public function getName(){
                if ($this->canUpload) {
                        return $_FILES[$this->formName]['name'];
                }
        }
         
        //新建文件名
        public function newName(){
                return date('YmdHis').rand(0,9);
        }
         
        //上傳文件
        public function upload(){
                if ($this->canUpload)
                {
                        $_getSize = $this->getSize('B');
                        if (!$_getSize)
                        {
                                return $_getSize;
                                break;
                        }
                        else
                        {
                                $_newName = $this->newName();
                                $_ext = $this->getExt();
                                $_doUpload = move_uploaded_file($_FILES[$this->formName]['tmp_name'], $this->directory.$_newName.".".$_ext);
                                if ($_doUpload)
                                {
                                        $this->doUpFile = $_newName;
                                }
                                return $_doUpload;
                        }
                }
        }
         
        //創(chuàng)建縮略圖
        public function thumb($_dstChar='_m', $_max_len=320){                //$_dstChar:_m , _s
                if ($this->canUpload && $this->doUpFile != "") {
                        $_ext = $this->getExt();
                        $_srcImage = $this->directory.$this->doUpFile.".".$_ext;
                         
                        //得到圖片信息數(shù)組
                        $_date = getimagesize($_srcImage, &$info);
                         
                        $src_w = $_date[0];        //源圖片寬
                        $src_h = $_date[1];        //源圖片高
                        $src_max_len = max($src_w, $src_h);                //求得長(zhǎng)邊
                        $src_min_len = min($src_w, $src_h);                //求得短邊
                        $dst_w = '';                //目標(biāo)圖片寬
                        $dst_h = '';                //目標(biāo)圖片高
                         
                        //寬高按比例縮放,最長(zhǎng)邊不大于$_max_len
                        if ($src_max_len>$_max_len)
                        {
                                $percent = $src_min_len / $src_max_len;
                                if ($src_w == $src_max_len)
                                {
                                        $dst_w = $_max_len;
                                        $dst_h = $percent * $dst_w;
                                }
                                else
                                {
                                        $dst_h = $_max_len;
                                        $dst_w = $percent * $dst_h;
                                }
                        }
                        else
                        {
                                $dst_w = $src_w;
                                $dst_h = $src_h;
                        }
                         
                        //建立縮略圖時(shí),源圖片的位置
                        $src_x = '';
                        $src_y = '';
                         
                        //判斷如果縮略圖用于logo,將對(duì)其進(jìn)行裁減
                        if ('s_' == $_dstChar) {
                                $src_x = $src_w * 0.10;
                                $src_y = $src_h * 0.10;
                                $src_w *= 0.8;
                                $src_h *= 0.8;
                        }
                         
                        //判斷圖片類型并創(chuàng)建對(duì)應(yīng)新圖片
                        switch ($_date[2]){
                                case 1:
                                        $src_im = imagecreatefromgif($_srcImage);
                                        break;
                                case 2:
                                        $src_im = imagecreatefromjpeg($_srcImage);
                                        break;
                                case 3:
                                        $src_im = imagecreatefrompng($_srcImage);
                                        break;
                                case 8:
                                        $src_im = imagecreatefromwbmp($_srcImage);
                                        break;
                        }
                         
                        //創(chuàng)建一幅新圖像
                        if ($_date[2]==1) {                //gif無(wú)法應(yīng)用imagecreatetruecolor
                                $dst_im = imagecreate($dst_w, $dst_h);
                        }else {
                                $dst_im = imagecreatetruecolor($dst_w, $dst_h);
                        }
         
                        //對(duì)這副圖像進(jìn)行縮略圖copy
//                        $bg = imagecolorallocate($dst_im,255,255,0);
                        imagecopyresized($dst_im,$src_im, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
                        //對(duì)圖片進(jìn)行抗鋸齒操作
                        imageantialias($dst_im, true);
                         
                        switch ($_date[2]) {
                                case 1:
                                        $cr = imagegif($dst_im, $this->directory.$this->doUpFile.$_dstChar.".".$_ext, 100);
                                        break;
                                case 2:
                                        $cr = imagejpeg($dst_im, $this->directory.$this->doUpFile.$_dstChar.".".$_ext, 100);
                                        break;
                                case 3://imagepng有問(wèn)題,所以在這里用imagejpg代替
                                        $cr = imagejpeg($dst_im, $this->directory.$this->doUpFile.$_dstChar.".".$_ext, 100);
                                        break;
                        }
//                        $cr = imagejpeg($dst_im, $this->directory.$_dstChar.$this->doUpFile, 90);
                        if ($cr) {
                                $this->sm_File = $this->directory.$this->doUpFile.$_dstChar.".".$_ext;
 
                                return $this->sm_File;
                        }else {
                                return false;
                        }
                }
                imagedestroy($dst_im);
                imagedestroy($cr);
        }
         
        //得到上傳后的文件名
        public function getUpFile(){
                if ($this->doUpFile!='') {
                        $_ext = $this->getExt();
                        return $this->doUpFile.".".$_ext;
                }else {
                        return false;
                }
        }
         
        //得到上傳后的文件全路徑
        public function getFilePatch(){
                if ($this->doUpFile!='') {
                        $_ext = $this->getExt();
                        return $this->directory.$this->doUpFile.".".$_ext;
                }else {
                        return false;
                }
        }
         
        //得到縮略圖文件全路徑
        public function getThumb(){
                if ($this->sm_File!='') {
                        return $this->sm_File;
                }else {
                        return false;
                }
        }
        //得到上傳文件的路徑
        public function getDirectory(){
                if ($this->directory!='') {
                        return $this->directory;
                }else {
                        return false;
                }
        }
}
?>

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:使用 Arrays.copyOf 進(jìn)行數(shù)組復(fù)制

下一篇:PHP調(diào)用http接口