【问题标题】:Query a base64 image from sql and decode it从sql中查询一张base64图片并解码
【发布时间】:2013-07-30 22:09:38
【问题描述】:

我正在尝试从 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 张图片,每张都需要显示(因此需要幻灯片)。这超出了我的想象,我非常感谢任何帮助。

【问题讨论】:

  • 这可能会帮助你http://stackoverflow.com/questions/2820249/base64-encoding-and-decoding-in-client-side-javascript

标签: php jquery sql base64


【解决方案1】:

试试这个代码,请根据你的代码证明:

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

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

【讨论】:

  • 感谢您的回复。我添加了代码并将标题更改为 image/jpg 以匹配最初压缩的文件类型。它现在给了我错误。请参阅photosfromfriends.com/webservice/gallery.php 我的 config.inc.php 文件将标题列为 header('Content-Type: text/html; charset=utf-8');但我需要 config.inc 文件来访问该程序上的其他表。真是一团糟。
  • 我还将第一行的 $json 更改为“posts”,因为它表示需要一个字符串。当标题问题浮出水面时,我仍在试图弄清楚这一点。
  • 你在某个地方打印了一些东西,请做更多研究以找出答案'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多