Home >Backend Development >PHP Tutorial >How Can I Display Images Stored as BLOBs in MySQL?

How Can I Display Images Stored as BLOBs in MySQL?

Susan Sarandon
Susan SarandonOriginal
2024-11-29 07:51:14271browse

How Can I Display Images Stored as BLOBs in MySQL?

Displaying Images from a MySQL BLOB Column

In your quest to display images stored as BLOB data in a MySQL database, you've encountered the issue of presenting the binary data as an actual image. Let's delve into the solutions.

Solution 1: Inline Base64 Encoding

If you're dealing with a small number of images, you can encode them using base64. This approach converts the binary data into a compact string that can be embedded directly into your HTML:

echo '<dt><strong>Technician Image:</strong></dt><dd>' . '<img src="data:image/jpeg;base64,' . base64_encode($row2['image']) . '" width="290" height="290">' . "</dd>";

Solution 2: PHP Proxy File

A more efficient approach for numerous images is to create a PHP proxy file that retrieves the image from the database based on an ID parameter:

<img src="image.php?id=<?php echo $image_id; ?>" />
<?php
$id = (isset($_GET['id']) &amp;&amp; is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
$image = getImageFromDatabase($id);

header('Content-Type: image/jpeg');
echo $image;
?>

Note: To correctly implement these solutions, ensure that the MIME type of the image file is set to image/jpeg in your server configuration.

The above is the detailed content of How Can I Display Images Stored as BLOBs in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn