php通過圖片資源上傳圖片

// $base64  圖片的base64資源  
// $path    上傳的路徑
function uploadimg($base64,$path)
{
 // 解決傳參時亂碼的問題
    $base64 = str_replace(' ','+',$base64); 
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)){
       //圖片的類型 jpg png等
        $type = $result[2];
        //重命名
        $new_file = $path.time().".{$type}";
        //資源寫入文件
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64)))){
            return '/'.$new_file;
        }else{
            return false;
        }
    }else{
        return false;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章