Php 从sql查询base64映像并对其进行解码
我正在尝试从表行中提取base64编码的图片。我需要解码它们并显示在网页上。我真的很想把他们放进幻灯片,但那是另一个主题 这是到目前为止的问题Php 从sql查询base64映像并对其进行解码,php,jquery,sql,base64,Php,Jquery,Sql,Base64,我正在尝试从表行中提取base64编码的图片。我需要解码它们并显示在网页上。我真的很想把他们放进幻灯片,但那是另一个主题 这是到目前为止的问题 <?php require("config.inc.php"); //initial query // table name is pictures and table row is picture $query = "Select * FROM pictures"; //execute query try { $stmt = $db-
<?php
require("config.inc.php");
//initial query
// table name is pictures and table row is picture
$query = "Select * FROM pictures";
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();
if ($rows) {
$response["success"] = 1;
$response["message"] = "Photos Available!";
$response["posts"] = array();
// only 3 rows in table - post_id, username, picture
foreach ($rows as $row) {
$post = array();
$post["post_id"] = $row["post_id"];
$post["username"] = $row["username"];
$post["picture"] = $row["picture"];
//update our repsonse JSON data
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No Photos Available!";
die(json_encode($response));
}
?>
问题在于解码。以下是目前为止的情况
这仅适用于一张图片(post)。表格中可能有50张图片,每个图片都需要显示(因此需要幻灯片)。这超出了我的理解范围,我非常感谢您的帮助。请尝试使用此代码,请根据您的代码证明:
$data = json_decode($json, true);
//print_r($data);
$picture = base64_decode($data['posts'][0]['picture']);
header("Content-type: image/png");
echo $picture;
您必须仅解码图像的解码数据,并且不要忘记使用标题这可能会对您有所帮助
http://stackoverflow.com/questions/2820249/base64-encoding-and-decoding-in-client-side-javascript
谢谢您的回复。我添加了代码并将标题更改为image/jpg,以匹配最初压缩的文件类型。现在它给了我错误。请参阅My config.inc.php文件,该文件将标题列为标题('Content-Type:text/html;charset=utf-8');但是我需要config.inc文件来访问这个程序中的其他表。真是一团糟。我还将第一行的$json改为“posts”,因为它说需要一个字符串。当标题问题出现时,我仍在试图找出答案。您正在某处打印某些内容,请进行更多研究以找到答案'