PHP base64多圖片上傳


    // 多圖片上傳,base64
    public function upload_multi() {
        $pic = $_POST['pic'];
        if (!$pic) {
            $this->json->setErr('10001',lang('tips_param_error'));
            $this->json->Send();
        }

        $err = 0;
        $out_data = [];
        if (is_array($pic)) {
            foreach ($pic as $k=>$v) { // 處理base64圖片數據
                $img =  $this->base64_to_img($v);
                if (!$img) {
                    $err ++;
                }
                $out_data[] = $img;
            }
        } else {
            // 處理base64圖片數據
            $img =  $this->base64_to_img($pic);
            if (!$img) {
                $err ++;
            }
            $out_data[] = $img;
        }

        if($err == 0){
            // 處理憑證綁定
            $this->json->setErr(0,lang('tips_deal_success'));
            $this->json->setAttr('data',$out_data);
            $this->json->Send();
        }else{
            $this->json->setErr(10099, lang('tips_deal_fail'));
            $this->json->Send();
        }
    }

    // base64=>img
    public function base64_to_img($base64_str,$us = 'img') {
        //  $base_img是獲取到前端傳遞的src里面的值,也就是我們的數據流文件
        if (strstr($base64_str,",")){
            $base64_str_arr = explode(',',$base64_str);
            $base64_str = $base64_str_arr[1];
        }
        //  設置文件路徑和文件前綴名稱
        $folders = date('Ymd',time());
        $path = "site_upload/".$us.'/'.$folders.'/';
        if (!is_dir($path)){
            @mkdir('./'.$path, 0777,true);
        }
        $prefix='rf_';
        $output_file = $prefix.time().rand(100,999).'.jpg';
        $img_path = $path.$output_file;
        //  創建將數據流文件寫入我們創建的文件內容中
        $ifp = fopen( $img_path, "wb" );
        fwrite( $ifp, base64_decode( $base64_str) );
        fclose( $ifp );
        // 輸出文件
        $qiniu = new QiniuImg();

        $img =  config('user.sf_host'). $img_path;
        $ext = pathinfo($img, PATHINFO_EXTENSION);
        $name = time() . mt_rand() . '.' . $ext;
        $s = $qiniu->up($img, $name, config('user.qiniu.bucket'));

        @unlink('./' .$img_path);

        if($s){
            return config('user.cdn_host') . $name;
        } else {
            return false;
        }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM