从sql查询base64映像并解码它

时间:2013-07-30 22:09:38

标签: 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->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张图片,每张图片都需要显示(因此需要幻灯片放映)。这是我的头脑,我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:1)

尝试此代码,请根据您的代码证明:

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

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