簡體   English   中英

從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;

您必須僅解碼圖像的解碼數據,並且不要忘記使用標頭

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM