Converting BLOB Data to Image Files with PHP and MySQL
Can PHP and MySQL convert a BLOB field into an image file?
Yes, you can convert a BLOB field in a MySQL database into an image file using PHP. This is possible using various image manipulation libraries available in PHP.
Using GD Library
To convert a BLOB to an image 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 ImageMagick (iMagick)
For ImageMagick:
$image = new Imagick(); $image->readimageblob($blob); echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
Using GraphicsMagick (gMagick)
Finally, for GraphicsMagick:
$image = new Gmagick(); $image->readimageblob($blob); echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
The above is the detailed content of How Can I Convert a BLOB Field in MySQL to an Image File with PHP?. For more information, please follow other related articles on the PHP Chinese website!