Home php Getting an image (BLOB) from MySQL PHP

Getting an image (BLOB) from MySQL PHP

Author

Date

Category

Can’t understand how to get an image from the database. I know that it is better not to store the image in the database, but still, how to get it and output ??

& lt;? php
$ HostName = "LocalHost";
$ username = "root";
$ password = "x";
$ Database = "Photo_Test";
$ usertable = "photo";
// Pocillion to database
$ Conn = Mysqli_Connect ($ hostname, $ username, $ password, $ database) or DIE ('Oops');
$ SQL = "SELECT * from $ USERTABLE WHERE ID = 2";


Answer 1

Already there is an answer to your question.

Original:
https://stackoverflow.com/a/6273415

You can use several different methods depending on which PHP image library is installed. Here are some examples.
Please note that ECHO is just a trick that I use to display multiple images from the same PHP script when cyclically viewing the MYSQL result resource. You can also display through the header (), as shown @naveed.

Gd:

$ image = imagecreatefromstring ($ blob);
OB_START (); // You Could Also Just Output The $ Image Via Header () And Bypass This Buffer Capture.
imagejpeg ($ image, null, 80);
$ Data = OB_GET_CONTENTS ();
OB_END_CLEAN ();
Echo '& lt; img src = "data: image / jpg; base64,'. Base64_ENCode ($ Data). '" / & gt;';

ImageMagick (Imagick):

$ image = new imagick ();
$ image- & gt; readimageblob ($ blob);
Echo '& lt; img src = "data: image / png; base64,'. Base64_encode ($ image- & gt; getimageblob ()). '" / & gt;';

GraphicsMagick (Gmagick):

$ image = new gmagick ();
$ image- & gt; readimageblob ($ blob);
Echo '& lt; img src = "data: image / png; base64,'. Base64_encode ($ image- & gt; getimageblob ()). '" / & gt;';

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions