php上傳圖片base64上傳thinkphp5上傳upload圖片源碼下載

php上傳圖片base64上傳thinkphp5上傳upload圖片源碼下載


    function uploadbaseAction()
    {

        $img_path   = ROOT_PATH . 'test' . DS . 'media'. DS . 'bag'. DS . date('Ymd');
        $base64_image_content   = request()->post('file');
        $base64_image_content   = str_replace(" ", "+", $base64_image_content);

        //test
//        $img_dir = ROOT_PATH . 'public' . DS . 'media'. DS . 'bag'. DS . date('Ymd').DS.'b.jpg';
//        $base64_image_content = $this->imgToBase64($img_dir);
        //test end

        if (!preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result))
        {
            return json(['state'=>'failure','error'=>2,'msg'=>'base64圖片格式有誤']);
        }

        $type               = $result[2];//圖片後綴
        $image_name         = date('ymdHis').rand(1000,9999).".".$type;
        $imge_real_url      = $img_path.'/'.$image_name;
        if (!file_exists($img_path))
        {
            mkdir($img_path, 0700);
            fopen($img_path.'\\'.$image_name, "w");
        }

        $decode             = base64_decode(str_replace($result[1], '', $base64_image_content));

        if (!file_put_contents($imge_real_url, $decode))
        {
            return json(['state'=>'failure','error'=>2,'msg'=>'圖片保存失敗']);
        }

        $data   = [
            'Extension' => $type,
            'saveName' => $image_name,
            'file_path' => '/media/bag/'.date('Ymd').'/'.str_replace("\\","/", $image_name),
            'Filename' => $image_name,
        ];
        $returnData =  ['state'=>'success','error'=>1, 'msg'=>'保存成功','data'=>$data];
        return json($returnData);
    }


    /**
     * 獲取圖片的Base64編碼(不支持url)
     * @date 2017-02-20 19:41:22
     *
     * @param $img_file 傳入本地圖片地址
     *
     * @return string
     */
    function imgToBase64($img_file) {

        $img_base64 = '';
        if (file_exists($img_file)) {
            $app_img_file = $img_file; // 圖片路徑
            $img_info = getimagesize($app_img_file); // 取得圖片的大小,類型等

            //echo '<pre>' . print_r($img_info, true) . '</pre><br>';
            $fp = fopen($app_img_file, "r"); // 圖片是否可讀權限

            if ($fp) {
                $filesize = filesize($app_img_file);
                $content = fread($fp, $filesize);
                $file_content = chunk_split(base64_encode($content)); // base64編碼
                switch ($img_info[2]) {           //判讀圖片類型
                    case 1: $img_type = "gif";
                        break;
                    case 2: $img_type = "jpg";
                        break;
                    case 3: $img_type = "png";
                        break;
                }

                $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成圖片的base64編碼

            }
            fclose($fp);
        }

        return $img_base64; //返回圖片的base64
    }

    function testAction()
    {
        $img_dir = ROOT_PATH . 'test' . DS . 'media'. DS . 'bag'. DS . date('Ymd').DS.'b.jpg';


        $img_base64 = $this->imgToBase64($img_dir);
        echo '<img src="' . $img_base64 . '">';
        echo $img_base64;exit;

    }

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章