Retrieve an Image Stored as Blob on a MySQL Db

Display all images stored as BLOB data from MySQL database

First you need to remove the if(isset($_GET['id']) statement because you want to display all images.

Next, query all images by changing your query to query without an id

$query = mysql_query("select * from `blob`");

Next, store the image data to an array.

$images = array();
while ($row = mysql_fetch_assoc($query)) {
$images[] = $row['image'];
}

Finally, display all images. (Got the code to display multiple blobs from here)

/* header should be removed
header("content-type: image/jpeg"); */
foreach ($images as $image) {
echo '<img src="data:image/jpeg;base64,'. base64_encode($image) .'" />';
}

Retrieve an Image stored as BLOB on a MYSQL DB

On your ResultSet call:

Blob imageBlob = resultSet.getBlob(yourBlobColumnIndex);
InputStream binaryStream = imageBlob.getBinaryStream(0, imageBlob.length());

Alternatively, you can call:

byte[] imageBytes = imageBlob.getBytes(1, (int) imageBlob.length());

As BalusC noted in his comment, you'd better use:

InputStream binaryStream = resultSet.getBinaryStream(yourBlobColumnIndex);

And then the code depends on how you are going to read and embed the image.

How to get the image stored into my mysql database in blob format on my google sheet?

  • Get JdbcBlob from JdbcResultSet
  • Get AppsScriptBlob from JdbcBlob
  • Insert AppsScriptBlob to sheet as image
const jdbcBlob = results.getBlob(col + 1);
const appsScriptBlob = jdbcBlob.getAppsScriptBlob();
sheet.insertImage(appsScriptBlob,1,1);

//if image data is base64 encoded:
sheet.insertImage(Utilities.newBlob(Utilities.base64Decode(appsScriptBlob.getDataAsString())),1,1)

Another way would be to get byte array using getBytes():

const byteArr = results.getBytes(col + 1);
//if image data is base64 encoded:
sheet.insertImage(Utilities.newBlob(Utilities.base64Decode(Utilities.newBlob(byteArr).getDataAsString())),1,1)

How to display an BLOB image stored in MySql database?

This is what I used when I wanted to do something like that... a long time ago! =P

$sql = "SELECT image FROM table WHERE cond ORDER BY xxxx DESC LIMIT 5";
$result = mysqli_query($db,$sql);
while($arraySomething = mysqli_fetch_array($result))
{
echo "<img src='php/imgView.php?imgId=".$arraySomething."' />";
}

How to retrieve image blob from MySQL into C# application?

Not sure if this really counts as a "duplicate" question since it's the combination of a couple questions I found, but...

According to this you can read the BLOB as a byte[] array:

byte[] imageBytes = (byte[])dr["headshot"];

From there, you can convert that to an Image:

using (var ms = new MemoryStream(imageBytes))
{
headshot.Headshot = System.Drawing.Image.FromStream(ms);
}


Related Topics



Leave a reply



Submit