/**
 * 将base64图片数据保存成图片
 * @param $image        base64字符串
 * @param string $path  保存路径
 * @return bool|string  保存成功返回路径否则返回flase
 */
function base64ToImgFile($image,$path='/apply/upload/'){
    $imageName = "b64_".date("His",time())."_".rand(1111,9999).'.png';
    if (strstr($image,",")){
        $image = explode(',',$image);
        $image = $image[1];
    }
    $path = $path.date("Ym",time());
    if (!is_dir($_SERVER['DOCUMENT_ROOT'].$path)){ //判断目录是否存在 不存在就创建
        mkdir($path,0777,true);
    }
    $imageSrc=  $_SERVER['DOCUMENT_ROOT'].$path."/". $imageName;  //图片名字
    $r = file_put_contents($imageSrc, base64_decode($image));//返回的是字节数
    if (!$r) {
        return false;
    }else{
        //return true;
        return $path."/". $imageName;
    }
}

 

posted on 2018-08-15 15:34  kerryk  阅读(359)  评论(0编辑  收藏  举报