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) . '" />';
Using the ImageMagick (iMagick) Library:
$image = new Imagick(); $image->readimageblob($blob); echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
Using the GraphicsMagick (gMagick) Library:
$image = new Gmagick(); $image->readimageblob($blob); echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
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!