简体   繁体   中英

Get a Image Blob from SQl via PHP and convert Json string

I'm new to posting here so bear with me.

Question : How do I pass an image stored as a blob through php as a json string?

Situation: I have a simple charity database with fields charity_id (int), charity_name (string), picture(blob) and a couple other fields.

I wanna grab the id, charity_name, and picture with php and pass the data as a json string to my android app where it will be parsed with volley library.

I have everything working and getting passed correctly, except my picture field is passing null instead of an string.

<?php 

if($_SERVER['REQUEST_METHOD']=='GET'){

    $id  = $_GET['id'];

    require_once('dbConnect.php');

    $sql = "SELECT charity_id, charity_name,picture FROM charity_table WHERE charity_id = '".$id."'";

    $r = mysqli_query($con,$sql);

    $res = mysqli_fetch_array($r);

    $result = array();


    array_push($result,array(
        "charity_id"=>$res['charity_id'],
        "charity_name"=>$res['charity_name'],
        "picture"=>$res['picture']
        )
    );

    echo json_encode(array("result"=>$result));

    mysqli_close($con);

}

json values passed {"result":[{"charity_id":"1","charity_name":"charity1","picture":null}]}

Because my understanding of the process (and it is probably wrong) is php gets a binary string from sql, this string is then put into a json array, then android reads the array and converts the binary string into a bitmap image.

Rather than store the image as a blob you can store a reference path to the image in your database and then

  1. Return a row(s) from your database

  2. Using the image reference url, you can download the image as a binary chunk with asynchronous call depending on your requirement.

JSON cannot handle raw binary data, you need to encode it somehow, and the 'how' is almost canonically base64.

Use this on the server side:

array_push($result,array(
    "charity_id"=>$res['charity_id'],
    "charity_name"=>$res['charity_name'],
    "picture"=>base64_encode($res['picture'])
    )
);

and implement the relevant base64 decode on the Android side.

That said, I agree with the other comments on this question that:

  1. Files should be stored in the filesystem and only a reference stored in the DB.
  2. You should probably serve the images via a separate, simple HTTP request rather than piping it into the JSON.
    • It's worth noting that encoding binary data in a text-safe manner will always inflate the size, and for base64 it's roughly 30% larger.
    • Separate requests can be handled in parallel.

If you're only storing and serving small icons or thumbnails this way it's not too bad, but anything over a few kB in size is going to be a pain in the booty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM