Home >Database >Mysql Tutorial >How to Convert a BLOB to an Image File in PHP?
Converting a Blob into an Image File in PHP
PHP provides various methods for converting BLOB data stored in a MySQL database into an image file. These methods rely on different image libraries that may already be installed on your system. Here are several options:
GD Library
<?php $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) . '" />'; ?>
ImageMagick (iMagick) Library
<?php $image = new Imagick(); $image->readimageblob($blob); echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />'; ?>
GraphicsMagick (gMagick) Library
<?php $image = new Gmagick(); $image->readimageblob($blob); echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />'; ?>
Notes:
The above is the detailed content of How to Convert a BLOB to an Image File in PHP?. For more information, please follow other related articles on the PHP Chinese website!