Displaying an Image Stored in a MySQL Blob

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) .'" />';
}

How do i display images stored as blob data type in mysql with php?

I answered my own question, it's working now..

Below is the getpicture.php:

<?php

$db = new MySQLi('localhost', '', '', 'mydatabase');

if ($db->connect_errno) {
echo 'Connection to database failed: '. $db->connect_error;
exit();
}

if (isset($_GET['id'])) {

$id = $db->real_escape_string($_GET['id']);

$query = "SELECT `Picture` FROM member WHERE `Id` = '$id'";

$result = $db->query($query);

while($row = mysqli_fetch_array($result)) {
$imageData = $row['Picture'];
header("Content-type:image/jpeg");
echo $imageData;
}

}
?>

The php script which retrieve the getpicture.php above looks like this:

echo '<img src="getpicture.php?id=' . htmlspecialchars($_GET["id"]) . '"border ="0" height="250" width="250" />';

Thaank you all for the help

Displaying BLOB image from Mysql database into dynamic div in html


1) Base64 option

Work with a single line, image/png for a png image and image/jpeg for a jpg one :

echo '<img src="data:image/png;base64,'.base64_encode($blob).'"/>';

example :





<div style="background-color:black; text-align:center; padding: 5px;">

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAwBAMAAACh2TSJAAAALVBMVEUAAADtNTX////3n5/+9fX719f7zMz5tLTzfHzuQED//f31jY3ybGzxXV3wVFRaxp+rAAAAAXRSTlMAQObYZgAAALVJREFUOMut0rENAjEQRNHdC4kY0QBaAQUQX0QAFSAKIKQEKiAA6VqgIkriApuV1x7pQPz0aWwHljLMpZ0CRDBGoXmeghGYKFJsUo90giAImCgV5OJF+oOgKE48MlGgs2VLBIunWesw0a1ZHqF82c7GmmIfUSpgotOly29DFPFJFDEhkgIT/V5mZuvj6XofKrHU6vyI4u37IYi36aN4h5tL7PJyif1dvCgEpapzISbCTEj5R78BZq5A5Ldh2XYAAAAASUVORK5CYII">

</div>

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."' />";
}

PHP display image BLOB from MySQL

Try it like this.

For inserting into DB

$db = new mysqli("localhost", "root", "", "DbName");
$image = file_get_contents($_FILES['images']['tmp_name']);
$query = "INSERT INTO products (image) VALUES(?)";
$stmt = $db->prepare($query);
$stmt->bind_param('s', $image);
$stmt->execute();

For accessing image from Blob

$db = new mysqli("localhost", "root", "", "DbName");
$sql = "SELECT * FROM products WHERE id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array();
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';

How to display an Image from a mysql blob

Another option you might consider (assuming you are on Apache):

Create an .htaccess file with a mod_rewrite for all image extensions (png, jpg, gif).

Have it redirect to a php script that looks up the image requested in the DB. If it is there, it echos out the header and BLOG. If it isn't there, it returns a standard 404.

This way you can have:

<img src="adorablepuppy.jpg" />

Which then gets redirected ala:

RewriteEngine on
RewriteRule \.(gif|jpg|png)$ imagelookup.php

This script does a query for the image, which (obviously) assumes that the requested image has a unique key that matches the filename in the URL:

 $url = $_SERVER['REQUEST_URI'];
$url_parts = explode("/", $url);
$image_name = array_pop($url_parts);

Now you have just the image filename. Do the query (which I shall leave up to you, along with any validation methods and checks for real files at the address, etc.).

If it comes up with results:

 header('Content-type: image/jpeg');
header('Content-Disposition: inline; filename="adorablepuppy.jpg"');
print($image_blog);

otherwise:

 header("HTTP/1.0 404 Not Found");

FYI: I have no idea if this would be bad in terms of performance. But it would allow you to do what I think you want, which is output the image as though it were a flat image file on the server using a simple image element. I'm inclined to agree that BLOBs are not the best way to go, but this does avoid any cross-browser issues.



Related Topics



Leave a reply



Submit