php處理base64格式圖片上傳

$base64_image_content:要保存的Base64php

$path:要保存的路徑this

public function base64_image_content($base64_image_content,$path){
        //匹配出圖片的格式
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
            $type = $result[2];
            //$ff=date('Y-m-d',time());
            $new_file = $path."/lunbo/";
            if(!file_exists($new_file)){
                //檢查是否有該文件夾,若是沒有就建立,並給予最高權限
                mkdir($new_file, 0700);
            }
            $picname=mt_rand(0,99).time().".{$type}";
            $new_file = $new_file.$picname;
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
                return "/Uploads/lunbo/".$picname;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

調用url

$data['pic'] = $this->base64_image_content($_POST['thumb_url'],'./Uploads');
相關文章
相關標籤/搜索