简体   繁体   English

PHP base64如何变成用于插入数据库的图像

[英]PHP base64 how to turn into image for insertion into database

Okay so I've got an android app that an image to my php web service. 好的,所以我有一个Android应用程序,该程序将图像发送到我的php Web服务。

I have decoded my base64 string using base64_decode() . 我已经使用base64_decode()解码了base64字符串。 However when I execute an SQL query to insert my data into the database the script inserts this string \\x5265736f75726365206964202333 . 但是,当我执行SQL查询以将我的数据插入数据库时​​,脚本会插入此字符串\\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. 我想知道的是如何修改代码以将该字符串转换为图像并将其插入到我的PostGreSQL数据库中,该数据库具有UTF8编码和bytea数据列类型来存储图像。

PHP script PHP脚本

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. 我想我正在插入解码的base64字符串并将其插入,但缺少将其转换为android应用程序拍摄的原始图像的最后一步。 Apologies for my newness to android dev'ing. 对我对android dev'ing的新颖性表示歉意。

This part: 这部分:

$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')");

makes no sense. 没有意义。 You're inserting a PHP filehandle. 您正在插入一个PHP文件句柄。 It's being converted to a string like Resource id #3 by PHP then converted to bytea . PHP将其转换为类似Resource id #3的字符串,然后转换为bytea Not what you want. 不是你想要的。

It's totally unnecessary to write the data to a file in the first place, since you already have it in memory. 首先,完全没有必要将数据写入文件,因为您已经将其存储在内存中。 Just use parameterised queries (you should always use parameterised queries) and let the DB driver take care of it via pg_query_params : 只需使用参数化查询(您应该始终使用参数化查询),然后让数据库驱动程序通过pg_query_params来处理它:

$res = pg_query_params(
       'INSERT INTO records (photo, name, description, latitude, longitude, project) VALUES ($1, $2, $3, $4, %6, $6)',
       array( pg_escape_bytea($photo), $s_name, $s_desc, $latitude, $longitude, $project)
);

See the docs on pg_query_params . 请参阅pg_query_params上的文档

Regarding bytea escapes see pg_escape_bytea and pg_unescape_bytea . 关于bytea转义,请参见pg_unescape_byteapg_escape_bytea Note that these functions are pretty behind the times and may not recognise hex escapes correctly on output; 请注意,这些功能远远落后于时代,可能无法正确识别输出的hex转义。 if you have issues, issue the SQL command SET bytea_output = 'escape'; 如果遇到问题,请发出SQL命令SET bytea_output = 'escape'; before trying to fetch values to pg_unescape_bytea . 在尝试获取pg_unescape_bytea值之前。

What PHP should really be doing is using the fact that the data type of the query's first parameter is bytea to infer that it must insert $photo as raw bytes rather than text. PHP 真正应该做的是利用以下事实:查询的第一个参数的数据类型是bytea以推断它必须将$photo作为原始字节而不是文本插入。 From your comment it sounds like it is not smart enough to do this. 从您的评论看来,这样做还不够聪明。

BTW, you really need to read this website to understand on reason why parameterized statements should always be used. 顺便说一句,您确实需要阅读本网站,以了解为什么应始终使用参数化语句的原因。 See SQL Injection and the PHP manual on SQL injection . 请参阅SQL Injection和有关SQL Injection 的PHP手册 In addition to being more secure, parameterised statements are easier to get right and often faster. 除了更安全之外,参数化语句更容易正确,而且通常更快。

I'd successfully write and recall base64 image using HTML + mySql. 我会使用HTML + mySql成功编写和调用base64图像。

Talking the example in HTML5 谈论HTML5中的示例

Below is my data on database 下面是我的数据库数据

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApQA 数据:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAApQA

You need to put data:image/< image format (png in my case) > 您需要输入data:image / <图片格式(在我的情况下为png)>

So for the viewing in page you need to add <img src=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApQA /> 因此,要在页面中进行查看,您需要添加<img src=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApQA />

I'm hoping that this can be applied to your project. 我希望这可以应用于您的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM