中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php圖片上傳類(支持縮放、裁剪、圖片縮略功能)

發布時間:2020-08-07 16:53:08 來源:網絡 閱讀:1356 作者:Lee_吉 欄目:web開發
  1. 代碼:
    <?php
    namespace dollarphp;
    /**
    * @desc:圖片上傳類
    * @author [Lee] <[<complet@163.com>]>
    * 1、自動驗證文件是表單提交的文件還是base64流提交的文件
    * 2、驗證圖片類型是否合法
    * 3、驗證圖片尺寸是否合法
    * 4、驗證圖片大小是否合法
    * 5、支持獲取圖片信息功能
    * 6、支持縮放功能
    * 7、支持裁剪功能
    * 8、支持縮略圖功能
    */
    class image{
    private $file;
    private $type;
    public $suffix = array();
    public $measure = array();
    public $size = array();
    public $scale = array(
            'is_scale'=>0,
            'ratio'=>1
        );
    public $crop = array(
            'is_crop'=>0,
            'width'=>0,
            'height'=>0
        );
    public $thumb = array(
            'is_thumb'=>0,
            'width'=>0,
            'height'=>0
        );
    public function __construct($file){
        $this->file = $file;
        $this->getType();
    }
    /*
     內部方法:獲取圖片類型
     1、多文件
     2、單文件
     3、base64流文件
     */
    private function getType(){
        $file = $this->file;
        if(is_array($file)){
            // 多文件
            if(is_array($file['name'])){
                $type = 1;
            }else{  // 單文件
                $type = 2;
            }
        }else{
            $type = 3;
        }
        $this->type = $type;
    }
    /*
     內部方法:獲取圖片后綴
     @param path 圖片路徑
     @return suffix 后綴名 如:jpg
     */
    private function getSuffix($path) {
        $file = fopen($path, "rb");
        $bin = fread($file, 2); // 只讀2字節
        fclose($file);
        $strInfo = @unpack("C2chars", $bin);
        $typeCode = intval($strInfo['chars1'] . $strInfo['chars2']);
        $suffix = "unknow";
        if($typeCode == 255216){
            $suffix = "jpg";
        }elseif($typeCode == 7173){
            $suffix = "gif";
        }elseif($typeCode == 13780){
            $suffix = "png";
        }elseif($typeCode == 6677){
            $suffix = "bmp";
        }elseif($typeCode == 7798){
            $suffix = "exe";
        }elseif($typeCode == 7784){
            $suffix = "midi";
        }elseif($typeCode == 8297){
            $suffix = "rar";
        }elseif($typeCode == 7368){
            $suffix = "mp3";
        }elseif($typeCode == 0){
            $suffix = "mp4";
        }elseif($typeCode == 8273){
            $suffix = "wav";
        }
        return $suffix;
    }
    /*
     內部方法:保存流文件
     @param stream 文件流
     @param suffix 后綴
     @param dir 保存文件夾
     @return path 文件路徑
     */
    private function uploadBase64($stream,$suffix,$dir) {
        if (empty($stream)) return false;
        if (preg_match('/^(data:(\s)?(image|img)\/(\w+);base64,)/', $stream, $str)) {
            $path = $dir . md5(rand(100000, 999999)) . ".{$suffix}";
            if (file_put_contents($path, base64_decode(str_replace($str[1], '', $stream)))) {
                return $path;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    /*
     校驗文件類型是否合法
     @return ret true:合法 false:非法
     */
    public function checkType(){
        $file = $this->file;
        $type = $this->type;
        $validSuffix = $this->suffix;
        $ret = true;
        if($type==1){   // 多文件
            foreach($file['tmp_name'] as $v){
                $suffix = $this->getSuffix($v);
                if(!in_array($suffix,$validSuffix)){
                    $ret = false;
                    break;
                }
            }
        }elseif($type==2){  // 單文件
            $suffix = $this->getSuffix($file['tmp_name']);
            if(!in_array($suffix,$validSuffix)){
                $ret = false;
            }
        }else{  // base64文件
            $suffix = $this->getSuffix($file);
            if(!in_array($suffix,$validSuffix)){
                $ret = false;
            }
        }
        return $ret;
    }
    /*
     校驗文件尺寸是否合法
     @return ret true:合法 false:非法
     */
    public function checkMeasure(){
        $file = $this->file;
        $type = $this->type;
        $validMeasure = $this->measure;
        $ret = true;
        if($type==1){   // 多文件
            foreach($file['tmp_name'] as $v){
                $measure = getimagesize($v);
                $width = $measure[0];
                $height = $measure[1];
                if(($width < $validMeasure['width'][0] || $width > $validMeasure['width'][1]) || ($height < $validMeasure['height'][0] || $height > $validMeasure['height'][1])){
                    $ret = false;
                    break;
                }
            }
        }elseif($type==2){  // 單文件
            $measure = getimagesize($file['tmp_name']);
            $width = $measure[0];
            $height = $measure[1];
            if(($width < $validMeasure['width'][0] || $width > $validMeasure['width'][1]) || ($height < $validMeasure['height'][0] || $height > $validMeasure['height'][1])){
                $ret = false;
            }
        }else{  // base64文件
            $measure = getimagesize($file);
            $width = $measure[0];
            $height = $measure[1];
            if(($width < $validMeasure['width'][0] || $width > $validMeasure['width'][1]) || ($height < $validMeasure['height'][0] || $height > $validMeasure['height'][1])){
                $ret = false;
            }
        }
        return $ret;
    }
    /*
     校驗文件大小是否合法
     @return ret true:合法 false:非法
     */
    public function checkSize(){
        $file = $this->file;
        $type = $this->type;
        $validSize = $this->size;
        $ret = true;
        if($type==1){   // 多文件
            foreach($file['tmp_name'] as $v){
                $size = filesize($v);
                if(($size < $validSize['min']*1024*1024) || ($size > $validSize['max']*1024*1024)){
                    $ret = false;
                    break;
                }
            }
        }elseif($type==2){  // 單文件
            $size = filesize($file['tmp_name']);
            if(($size < $validSize['min']*1024*1024) || ($size > $validSize['max']*1024*1024)){
                $ret = false;
            }
        }else{  // base64文件
            $info = file_get_contents($file);
            $size = strlen($info);
            if(($size < $validSize['min']*1024*1024) || ($size > $validSize['max']*1024*1024)){
                $ret = false;
            }
        }
        return $ret;
    }
    /*
     @desc:獲取圖片信息
     */
    public function info(){
        $file = $this->file;
        $type = $this->type;
        $ret = array();
        if($type==1){   // 多文件
            foreach($file['tmp_name'] as $k=>$v){
                $measure = getimagesize($v);
                $width = $measure[0];
                $height = $measure[1];
                $size = filesize($v)/1024/1024;
                $ret[$k] = array(
                        'width'=>$width,
                        'height'=>$height,
                        'size'=>number_format($size,2)
                    );
            }
        }elseif($type==2){  // 單文件
            $measure = getimagesize($file['tmp_name']);
            $width = $measure[0];
            $height = $measure[1];
            $size = filesize($file['tmp_name'])/1024/1024;
            $ret = array(
                    'width'=>$width,
                    'height'=>$height,
                    'size'=>number_format($size,2)
                );
        }else{  // base64文件
            $measure = getimagesize($file);
            $width = $measure[0];
            $height = $measure[1];
            $info = file_get_contents($file);
            $size = strlen($info)/1024/1024;
            $ret = array(
                    'width'=>$width,
                    'height'=>$height,
                    'size'=>number_format($size,2)
                );
        }
        return $ret;
    }
    /*
     內部方法:縮放圖片
     */
    private function scale($path,$suffix,$ratio){
        list($width, $height) = getimagesize($path);  
        $new_w = $ratio * $width;
        $new_h = $ratio * $height;
        $new_s = imagecreatetruecolor($new_w, $new_h);
        if(in_array($suffix,array('jpg','jpeg'))){
            $img = imagecreatefromjpeg($path);
        }elseif($suffix == 'png'){
            $img = imagecreatefrompng($path);
        }elseif($suffix == 'gif'){
            $img = imagecreatefromgif($path);
        }else{
            return false;
        }
        $ret1 = imagecopyresized($new_s, $img, 0, 0, 0, 0, $new_w, $new_h, $width, $height);  
        if(in_array($suffix,array('jpg','jpeg'))){
            $ret2 = imagejpeg($new_s, $path);
        }elseif($suffix == 'png'){
            $ret2 = imagepng($new_s, $path);
        }elseif($suffix == 'gif'){
            $ret2 = imagegif($new_s, $path);
        }else{
            return false;
        }
        imagedestroy($new_s);  
        imagedestroy($img);
        if($ret1 && $ret2){
            return $path;
        }else{
            return false;
        }
    }
    /*
     內部方法:裁剪圖片
     */
    private function crop($path,$suffix,$cut_width,$cut_height){
        $cut_x;
        $cut_y;
        $min;
        $size = getimagesize($path);
        $width = $size[0];
        $height = $size[1];
        $min = min($width,$height);
        $cut_width = ($cut_width > $min)?$min:$cut_width;
        $cut_height = ($cut_height > $min)?$min:$cut_height;
        $cut_x = ($width - $cut_width) / 2;
        $cut_y = ($height - $cut_height) / 2;
        if(in_array($suffix,array('jpg','jpeg'))){
            $img = imagecreatefromjpeg($path);
        }elseif($suffix == 'png'){
            $img = imagecreatefrompng($path);
        }elseif($suffix == 'gif'){
            $img = imagecreatefromgif($path);
        }else{
            return false;
        }
        $new_s = imagecreatetruecolor($cut_width, $cut_height);
        $ret1 = imagecopyresampled($new_s, $img, 0, 0, $cut_x, $cut_y, $cut_width, $cut_height, $cut_width, $cut_height);  
        if(in_array($suffix,array('jpg','jpeg'))){
            $ret2 = imagejpeg($new_s, $path);
        }elseif($suffix == 'png'){
            $ret2 = imagepng($new_s, $path);
        }elseif($suffix == 'gif'){
            $ret2 = imagegif($new_s, $path);
        }else{
            return false;
        }
        imagedestroy($new_s);
        imagedestroy($img);
        if($ret1 && $ret2){
            return $path;
        }else{
            return false;
        }
    }
    /*
     內部方法:生成縮略圖
     */
    private function thumb($path,$suffix,$cut_width,$cut_height){
        $cut_x;
        $cut_y;
        $ratio = 1;
        $size = getimagesize($path);
        $width = $size[0];
        $height = $size[1];
        $cw;
        $ch;
        if($width/$height >= $cut_width/$cut_height){
            $ratio = $cut_height / $height;
        }else{
            $ratio = $cut_width / $width;
        }
        $path = $this->scale($path,$suffix,$ratio,$path);
        $width *= $ratio;
        $height *= $ratio;
        $cut_x = abs($cut_width - $width) / 2;
        $cut_y = abs($cut_height - $height) / 2;
        if(in_array($suffix,array('jpg','jpeg'))){
            $img = imagecreatefromjpeg($path);
        }elseif($suffix == 'png'){
            $img = imagecreatefrompng($path);
        }elseif($suffix == 'gif'){
            $img = imagecreatefromgif($path);
        }else{
            return false;
        }
        $new_s = imagecreatetruecolor($cut_width, $cut_height);
        $ret1 = imagecopyresampled($new_s, $img, 0, 0, $cut_x, $cut_y, $cut_width, $cut_height, $cut_width, $cut_height);  
        if(in_array($suffix,array('jpg','jpeg'))){
            $ret2 = imagejpeg($new_s, $path);
        }elseif($suffix == 'png'){
            $ret2 = imagepng($new_s, $path);
        }elseif($suffix == 'gif'){
            $ret2 = imagegif($new_s, $path);
        }else{
            return false;
        }
        imagedestroy($new_s);
        imagedestroy($img);
        if($ret1 && $ret2){
            return $path;
        }else{
            return false;
        }
    }
    /*
     保存
     @param dir 存儲的文件夾 如:'./'
     @return ret 存儲的文件路徑 如:'./test.jpg'
     */
    public function save($dir){
        $file = $this->file;
        $type = $this->type;
        $scale = $this->scale;
        $crop = $this->crop;
        $thumb = $this->thumb;
        $is_scale = $scale['is_scale'];
        $is_crop = $crop['is_crop'];
        $is_thumb = $thumb['is_thumb'];
        $ratio = $scale['ratio'];
        $crop_width = $crop['width'];
        $crop_height = $crop['height'];
        $thumb_width = $thumb['width'];
        $thumb_height = $thumb['height'];
        if($type==1){   // 多文件
            foreach($file['tmp_name'] as $k=>$v){
                $suffix = $this->getSuffix($v);
                $name = $dir.md5(rand(100000,999999)).'.'.$suffix;
                $flag = file_put_contents($name,file_get_contents($v));
                if(!$flag){
                    $ret = false;
                }else{
                    if($is_scale){
                        $name = $this->scale($name,$suffix,$ratio);
                    }
                    if($is_crop){
                        $name = $this->crop($name,$suffix,$crop_width,$crop_height);
                    }
                    if($is_thumb){
                        $name = $this->thumb($name,$suffix,$thumb_width,$thumb_height);
                    }
                    $ret[$k] = $name;
                }
            }
        }elseif($type==2){  // 單文件
            $suffix = $this->getSuffix($file['tmp_name']);
            $name = $dir.md5(rand(100000,999999)).'.'.$suffix;
            $flag = file_put_contents($name,file_get_contents($file['tmp_name']));
            if(!$flag){
                $ret = false;
            }else{
                if($is_scale){
                    $name = $this->scale($name,$suffix,$ratio);
                }
                if($is_crop){
                    $name = $this->crop($name,$suffix,$crop_width,$crop_height);
                }
                if($is_thumb){
                    $name = $this->thumb($name,$suffix,$thumb_width,$thumb_height);
                }
                $ret = $name;
            }
        }else{  // base64文件
            $suffix = $this->getSuffix($file);
            $name = $dir.md5(rand(100000,999999)).'.'.$suffix;
            $flag = file_put_contents($name,file_get_contents($file));
            if(!$flag){
                $ret = false;
            }else{
                if($is_scale){
                    $name = $this->scale($name,$suffix,$ratio);
                }
                if($is_crop){
                    $name = $this->crop($name,$suffix,$crop_width,$crop_height);
                }
                if($is_thumb){
                    $name = $this->thumb($name,$suffix,$thumb_width,$thumb_height);
                }
                $ret = $name;
            }
        }
        return $ret;
    }
    }
  2. 示例用法:
    $file = $_FILES['file'];
    $image = new image($file);
    $image->thumb = array(
        'is_thumb'=>1,
        'width'=>200,
        'height'=>500
    );
    $ret = $image->save('./');
    var_dump($ret);
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

吉木乃县| 白玉县| 车险| 宁河县| 涪陵区| 南通市| 靖远县| 丹寨县| 新宁县| 新沂市| 江口县| 镶黄旗| 昆山市| 西峡县| 卫辉市| 麻城市| 阳西县| 康定县| 涿州市| 宣化县| 沙雅县| 平凉市| 苍梧县| 随州市| 海门市| 鸡泽县| 丰台区| 德昌县| 济阳县| 黑龙江省| 邢台市| 布尔津县| 吉首市| 盖州市| 阿克陶县| 重庆市| 镇江市| 宜宾市| 长治市| 武城县| 陆丰市|