Home >Database >Mysql Tutorial >How Can I Display MySQL BLOB Images Alongside Other Content?

How Can I Display MySQL BLOB Images Alongside Other Content?

Susan Sarandon
Susan SarandonOriginal
2024-11-27 08:51:09833browse

How Can I Display MySQL BLOB Images Alongside Other Content?

Displaying Images Stored as MySQL BLOBs with Additional Content

When working with MySQL BLOBs containing images, displaying the image while echoing other content can be problematic.

In the code snippet provided:

header("Content-type: image/jpeg");
echo $row['imageContent'];
echo '--------'; // If echoed before the image, the image will not display

Outputting text before the image data will cause the image to not display. This is because browsers interpret the text as part of the image.

To display both text and the image, convert the image data into base64 and embed it in an tag:

echo '<img src="data:image/jpeg;base64,' . base64_encode($row['imageContent']) . '" />';
echo 'Hello world.';

Note that this method is not ideal for performance, as the image data is not cached and is slower to load on mobile devices. Consider using a more efficient solution if performance is critical.

The above is the detailed content of How Can I Display MySQL BLOB Images Alongside Other Content?. 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