doufusi2013 2013-05-13 03:36 采纳率: 0%
浏览 197

PHP base64如何转换成图像以插入数据库

Okay so I've got an android app that an image to my php web service.

I have decoded my base64 string using base64_decode(). However when I execute an SQL query to insert my data into the database the script inserts this string \x5265736f75726365206964202333.

What I want to know is how I can modify my code to turn that string into an image and insert it into my PostGreSQL database which has UTF8 encoding and a bytea data column type to store the image.

PHP script

header('Content-Type: text/plain; charset=UTF-8');
$conn = pg_connect("database_string");

 /* GET DATA */
$name = $_POST['name'];
$s_name = pg_escape_string($name);
$description = $_POST['desc'];  
$s_desc = pg_escape_string($description);
$latitude = $_POST['lat'];
$longitude = $_POST['lng'];
$project = $_POST['project'];

$encoded_photo = $_POST['snap'];
$photo = base64_decode($encoded_photo);
header('Content-Type: image/jpeg; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $photo);
fclose($file);

 /* INSERT INTO DATABASE */
$res = pg_query("INSERT INTO records (photo, name, description, latitude, longitude, project) VALUES ('$file', '$s_name', '$s_desc', '$latitude', '$longitude', '$project')");

I know I'm close to my intended solution. I think I am inserting the decoded base64 string and inserting that but missing the final step to convert it to the original image taken by the android app. Apologies for my newness to android dev'ing.

  • 写回答

2条回答 默认 最新

  • dsf4354353452 2013-05-13 04:06
    关注

    I'd successfully write and recall base64 image using HTML + mySql.

    Talking the example in HTML5

    Below is my data on database

    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApQA

    You need to put data:image/< image format (png in my case) >

    So for the viewing in page you need to add <img src=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApQA />

    I'm hoping that this can be applied to your project.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料