Home > Database > Mysql Tutorial > How to Display Images Stored as BLOBs in MySQL using PHP?

How to Display Images Stored as BLOBs in MySQL using PHP?

Barbara Streisand
Release: 2024-11-18 08:27:02
Original
753 people have browsed it

How to Display Images Stored as BLOBs in MySQL using PHP?

Retrieving and Displaying Images Stored as BLOBs in PHP

Question: Can a binary large object (BLOB) stored in a MySQL database be converted into an image file using PHP?

Answer: Yes, there are several methods to achieve this conversion using PHP, depending on the image library installed.

Using the GD Library:

$image = imagecreatefromstring($blob);

ob_start();
imagejpeg($image, null, 80);
$data = ob_get_contents();
ob_end_clean();

echo '<img src="data:image/jpg;base64,' . base64_encode($data) . '" />';
Copy after login

Using the ImageMagick (iMagick) Library:

$image = new Imagick();
$image->readimageblob($blob);

echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
Copy after login

Using the GraphicsMagick (gMagick) Library:

$image = new Gmagick();
$image->readimageblob($blob);

echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
Copy after login

Note: The echo is used to display multiple images from the same PHP script while looping through a MySQL result set. Alternatively, you can output the image using header().

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

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template