从sql中查询base64映像并对其进行解码


Query a base64 image from sql and decode it

我正试图从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->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));
}
?>

问题是解码。以下是迄今为止显示的内容

http://www.photosfromfriends.com/webservice/gallery.php

这只是一张图片(帖子)表格中可能有50张图片,每张图片都需要显示(因此需要幻灯片放映)。这让我不知所措,我真的很感激任何帮助。

尝试此代码,请根据您的代码进行验证:

$data = json_decode($json, true);
//print_r($data);
$picture = base64_decode($data['posts'][0]['picture']);
header("Content-type: image/png");
echo $picture;

您必须只解码图像的解码数据,不要忘记使用标题