简体   繁体   中英

Convert binary string to Image in PHP?

Please help me with my code. I have to output an image (this is in PNG) that is converted into a binary file or BLOB by sql server. Please review the code below:

<?php
$myServer = "111.22.33.44";
$myUser = "username";
$myPass = "password";
$myDB = "database";


$connection = sqlsrv_connect($myServer, array('UID'=>$myUser, 'PWD'=>$myPass, 
                             'Database'=>$myDB));

if ($connection === false){
    print_r( sqlsrv_errors());
}

$qry_getCategory = "SELECT Sketch FROM Sample_Specs_Comment WHERE StyleID = '1'";

$stmt = sqlsrv_query($connection, $qry_getCategory);

if ($stmt) {
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {     
    $imgDes = $row["Sketch"];
    echo $imgDes;
} else{
    echo "Submission Unsuccessful!";
    die(print_r(sqlsrv_error(), true));
}
?>

the data of the text image file starts with 0x89504E470...... I am assuming this is a kinda binary code or BLOB. this is in the 'Sketch' column in the database. I want to output the BLOB into view-able format like 'PNG' or 'JPEG'.

This is the output below:

IHDR�Y�"l�sRGB���gAMA���a pHYs���o�d<�IDATx^�}x�vBHH 

I think I've solve the issue. I've encode the binary data to base64.

if ($stmt) {
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {     
    $imgDes = $row["Sketch"];
    $imageData = base64_encode($imgDes);
    }
}

and in HTML:

<img src="<?php  echo $imageData; ?>" alt="Image"/>

this should help. Thanks to the persons who gave answers to the problem.

I'm not sure if you want to return only an image for the request or if you would like to be able to load several images from database during one request and display them?

I'll presume the latter since you say "I have to output the data in a page ".

Looking at it quickly I see two routes you can go. If the binary data is a valid image and you know the type you could just save it into a file (perhaps in a temporary ramdisk to avoid writes on disk) and read from that file like normal. I would probably not go this route though since you already have the binary data in your database and saving it to another place indicates the design is wrong and a more correct scenario would have been to save the image on disk right away and store a reference to it in the database.

So, since the binary data is already in the database I would go with loading it directly into the image tag on your page using base64 encoding.

Pseudocode for this would be something like

$imageData = getBinaryImageDataFromDb();
$imageData = base64_encode($imageData);

// in html

<img src="data:image/png;base64,<?php echo $imageData?>">

Since you don't load the data from a file, normal functions likemime_content_type() etc will not work since except a file. And we don't want to save a temporary file for this.

So to check the file format of the file you could check the starting bytes and determine the image type using that.

JPEG: "\xFF\xD8\xFF"
PNG: "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"

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