0

I am trying to pull pictures from a table row that are base64 encoded. I need to decode them and display on a webpage. I would really like to put them into a slideshow but that is another topic!

Here is the query so far

<?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));
}

?>

The problem is decoding it. Here is what it shows so far

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

This is only for one picture (post) The table might have 50 pictures in it and each will need to be displayed (hence the desire for a slideshow). This is way over my head and I would really appreciate any help.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
JeffK
  • 247
  • 1
  • 5
  • 18
  • This may help you `http://stackoverflow.com/questions/2820249/base64-encoding-and-decoding-in-client-side-javascript` – RiggsFolly Jul 30 '13 at 22:25

1 Answers1

1

try this code, please justify according to your code:

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

You must decode only decoded data for the image, and don't forget to use header

  • Thanks for the reply. I added the code and changed the header to image/jpg to match the file type originally compressed. It now gives me errors. see http://www.photosfromfriends.com/webservice/gallery.php My config.inc.php file lists the header as header('Content-Type: text/html; charset=utf-8'); but I need the config.inc file to access other tables on this program. What a mess. – JeffK Jul 31 '13 at 00:13
  • I also changed $json on the first line to "posts" because it said a string was expected. I was still trying to figure that out when the header problem surfaced. – JeffK Jul 31 '13 at 00:19
  • you are printing something in somehwere, please do more research to find out' –  Jul 31 '13 at 00:22