I am retriving image from mysql database
and want to hyperlink this processed image
but not able to find solution

for ($i = 0; $i < $rows; $i++) {
                      $description = mysql_fetch_array($result);
                          $img_str= base64_encode($description["nImage"]);
                    echo '<a id="zoom" href="data:image/jpeg;base64,'.$img_str.'"><img src="data:image/jpeg;base64,'.$img_str.'" width="150" height="150" /></a>';
                        if($i%3 == 2) echo "</br>";


Recommended Answers

All 6 Replies

Create a script, for example display.php:

$f = $_GET['image'];
header("Content-type: image/jpeg");
echo base64_decode($f);

Now your link becomes:

<a href="display.php?image=<?php echo $img_str; ?>">

    <img src="data:image/jpeg;base64,<?php echo $img_str; ?>" width=150 height=150 />

Member Avatar for diafol

Like he said ^^ !

Are you sure that you need to store image data in the db? It can be considerably slower than serving flat files. Also, 'data' images can't be cached. I'm assuming that you have a specific requirement for blob storage?

It looks as though there are many images to retrieve. Depending on the usage/context, you have a number of options open to you: db blob, filename storage, css sprites

Hi, I have already solved this scenario using Cross Database Engine. It has an encode_image function which can make thumbnails and it does a checksum on the image from the database to see whether it should output the file again.
Here is an example:

  //before starting, please have sqlite3, gd and exif extensions enabled otherwise this won't work!

  require_once "cdesimple.php";

   //get the image of a duck
   $imagedata = file_get_contents ("http://upload.wikimedia.org/wikipedia/commons/5/51/Mandarin.duck.arp.jpg");

  //make a table with CDE and store it

  $CDE = new CDESimple ("images.db", "", "", "sqlite3", false, "YYYY-mm-dd");

  $CDE->exec ("create table if not exists tblmyimages (
                 id integer primary key autoincrement,
                 description varchar (200) default '',
                 imagedata blob       

   //make an insert, notice the use of params which sqlite doesn't support natively 

   $sqlinsert = "insert into tblmyimages (description, imagedata) 
                                  values (?          , ?)";  

   $CDE->exec ($sqlinsert, "A Duck", $imagedata);

   //now to display all the ducks - CDE automatically knows how to fetch blobs from the different database engines!!!

   $sql = "select * from tblmyimages";

   //get_row( $sql = "", $rowtype = 0, $fetchblob = true, $calculatedfields = array( ) ) {
   //rowtype can be one of the following
   //define( "CDE_OBJECT", 0 );
   //define( "CDE_ARRAY", 1 );
   //define( "CDE_ASSOC", 2 );

   $myimages = $CDE->get_row ($sql);

   //it may seem strange but CDE makes all fieldnames uppercase and by default returns an object, you can tell it to return an array if you want 

   //make a folder to store the outputtted images
   mkdir ("myimagestore");  

   //variable for outputting things  
   $html = "";   
   foreach ($myimages as $mid => $myimage ) {
      //introducing the encode_image function 
      //function encode_image( $imagedata, $imagestore = "images", $size = "", $noimage = "/images/noimage.jpg" )
      //$imagedata is the blob field, 
      //$imagestore is a folder you have created on the server,
      //$size is in the form XxY where X = width and Y = Height - 200x200 for example
      //$noimage is when you have a blank in the data and you want it to display that image
      $html .= "<img alt=\"{$myimage->DESCRIPTION}\" src= \"".$CDE->encode_image ($myimage->IMAGEDATA, "myimagestore", "50X50")."\"><br>";


   echo $html;


while passing $img_str in query string, it gives error

Request-URI Too Large

The requested URL's length exceeds the capacity limit for this server.

That happens if the GET request exceeds the LimitRequestLine directive (if you're using Apache), this limit cannot be higher than 8190 bytes, to change it you should modify the source and re-compile Apache:

Reconsider diafol suggestion. Otherwise use the id of the image instead of the encoded string and then retrieve it from the database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.