• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

PHP的Base64格式图像存到本地的方法

武飞扬头像
Luke
帮助3567

<?php
//直接看代码吧,把BASE64的图像提交上来接受,然后在转为本地 
function index()
{
    //图片裁剪
    $base64_image_content = $_POST['img'];
    //匹配出图片的格式
    if (preg_match('/^(data:\s*image\/(\w );base64,)/', $base64_image_content, $result)) {
        $type = $result[2];
        $new_file = "Uploads/headportrait/" . date('Ymd', time()) . "/";
        if (!file_exists($new_file)) {
            //检查是否有该文件夹,如果没有就创建,并给予最高权限
            mkdir($new_file, 0700);
        }
        $new_file = $new_file . time() . ".{$type}";
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
            $_SESSION['home']['image']['data']['imgurl'] = '/' . $new_file;
            $res['status'] = 1;
            $res['str'] = '保存成功';
            $res['url'] = U('Person/register');
        } else {
            $res['status'] = 0;
            $res['str'] = '保存失败';
        } //*/
    } else {
        $res['status'] = 2;
        $res['str'] = '你的东西呢';
    }
    echo json_encode($res);
}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/taneckh
系列文章
更多 icon
同类精品
更多 icon
继续加载