Tp框架如何接受ios端post过来的base64编码的图片 并且把图片路径保存

挥手凝淡墨
  • 867

Tp框架如何接受ios端post过来的base64编码的图片 并且把图片路径保存

谢谢求大牛指点 最好有代码

评论
阅读 4.6k
4 个回答

直接把解码后存入指定目录的文件就行,

接收到的base64数据:

$data = "data:image/jpg;base64,balabalabala...";

类似这种格式, 先把','前面的东西去掉, 得到文件内容

$image_content = "balabalabala...";

然后执行

$image_content = base64_decode($image_content);

然后执行

file_input_contents($dir, $image_content)

$dir就是你要保存图片的地址

if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $_POST['image'], $result)){
        $type = $result[2];
        $new_file = RUNTIME_PATH . '/uploads/images/'.date('YmdHis', time()).'.'.$type;
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $request['image'])))){
          $image = $new_file;
        }
      }
<?php
$base = 'data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAEoAAAAoCAMAAABuMpwSAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAK5QTFRF/////+/IqJmZuN//qKjI7///78iomZmZ///fuJmZmbjf/9+4///vyKioyO//qMjvqLjfmZm43///majIyKi4yKiZ37iZqMjIqJmo37iombjIuJm4uLjfmZmoqJm43//v3//f/+/vmbi4uKi4uKiZ37i4yMioqMjfyO/fuLiZ1LiZqKiouKjIuMjv3+/IuMjfyLjI7//f79+4yMi4uKiouN/vqLi439+4yLiouLi45PPwawAAAAFiS0dEAIgFHUgAAAIlSURBVEjH7VVpc9MwEJWtWnJsUx+xFdqUtjSEUGjDUc7//8d4uyulIVbT8IUZZrKZcWzZ+/T27SGljna0qCWplv+TzFgxLOSTQqmykE+qF6dq687UxtYRJO9eMJZKGr/YdspDBXzYNOP3JoaDzXm5ZE8QDKSqvquG8KCcDqT6zmhlZhM7opW8PC1rVQ3YHZ54e0b8NaOc9x1YwZPp5nOmqfMLZ4VyNMJXogRJsAnksqsCVDWQdgzlGCVIOBK97YxgbUsAsa4ClGmvJ7wDPnudEbebCYu7wwgEgkCAMp5UvlhcL97IOm0WWOUbzKjuvO4KYrV8q31VrPpNgGfv5hsowyrhHgLsIrm2g/SO84RiMR5qiehCMQiKQNHWOkgWU8tKrpHyECBlPQZVDcBAfkHgJBvBkOOjdp7VDtRtyOx7DRcoUtpouR/t/zduh4Kujx1BM66kAsBA+UDt04QR9NRYEKg7mnm4wr+UMchQyT1+jcIYctyhXJamOICVQNXS+Fz0y/XHT5/vvswuAEVFW04ztxfpT1a11Dix4jZqHx5W/a20lozHg6GIIE1WOg/yrylcKcBvg10NJKUZT6p9ARaYy6zV5foe0ohWZa22j56DA0wa3Mo50a5TDGqC8ueG1fuhkJ+xVkmDDH6f/7DFX7FyRaQYUn7wAa5+puEQ2a+VdjMM82k2YhXqqqTyfZaVDGriFszw3szqfLC/qMLphHwe6mj/xH4DIIIqf4p44pEAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTYtMDYtMTNUMTU6MzM6MjArMDg6MDBOXtEYAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE2LTA2LTEzVDE1OjMzOjIwKzA4OjAwPwNppAAAAABJRU5ErkJggg==';

file_put_contents('1.jpg', file_get_contents($base));

看到楼主一直在问怎么接收

// 这就接收了base64的信息了啊
$base64 = I('data', '');

// ###文件处理
// 创建对应的目录
$pic_path = 'static/' . date("Y-m-d") . '/';

!file_exists($pic_path) && mkdir($pic_path, 0777);
// 图片名称
$pic_path .= md5(md5(time())) . '.jpg';
// 写入图片 返回的是字节数
$size   = file_put_contents($pic_path, base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64)));
宣传栏