Home > Database > Mysql Tutorial > body text

How Can I Convert a BLOB Field in MySQL to an Image File with PHP?

Mary-Kate Olsen
Release: 2024-11-10 06:18:02
Original
594 people have browsed it

How Can I Convert a BLOB Field in MySQL to an Image File with PHP?

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)  . '" />';
Copy after login

Using ImageMagick (iMagick)

For ImageMagick:

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

Using GraphicsMagick (gMagick)

Finally, for GraphicsMagick:

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

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!

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