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
Release: 2024-11-29 07:51:14
Original
374 people have browsed it

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>";
Copy after login

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; ?>" />
Copy after login
<?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;
?>
Copy after login

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 of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template