0

I have Images stored in my DB, I want to retrieve them all and display them in the browser. It works just fine, but I want to give the user the ability to click on the picture so that they can have a better view of the picture (so display it with its original size). I tried many things but didn't work. second question: if I want my code to support multiple images type (such gif, jpg, etc..), if there a way to do it without having to save the image type (when I insert the images) and play with a whole bunch of if/else (when I retrieve them)?
This is my code

  $count = 0;
       echo " <div class=\"row\">"; 
   while($row = $result->fetch_assoc()) {

       $imagename = base64_encode( $row['Image'] );
       if(($count%3) ==0){
        echo "</div>";
         echo " <div class=\"row\">"; 

            echo "  <div class=\"col-sm-2\">";
            echo " <a href=\"$imagename.jpeg\" class=\"thumbnail\">";
            echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'" style=\"width:130px;height:130px\"/>';
            echo"</a></div>";      


        ++$count;

       }else{
                echo "  <div class=\"col-sm-2\">";
            echo " <a href=\"$imagename.jpeg\" class=\"thumbnail\">";
            echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'" style=\"width:130px;height:130px\"/>';
            echo"</a></div>";
            ++$count;

       }

   }
 echo "</div>" ;
Bobby
  • 496
  • 5
  • 18
  • 7
    Don't save files in a database. http://stackoverflow.com/a/38829952/267540 – e4c5 Nov 22 '16 at 14:54
  • Save only the path in the db and the file in a folder in your server. What happens when you click your href link? – Rafael Shkembi Nov 22 '16 at 14:56
  • @e4c5 small files (ca. < 100k) is ok – Strawberry Nov 22 '16 at 14:56
  • 1
    @Strawberry we are going to have our first disagreement. – e4c5 Nov 22 '16 at 14:57
  • 1
    First of many no doubt :-) – Strawberry Nov 22 '16 at 14:57
  • @e4c5 According to some sources, Databases are posweful enough nowadays to store images – Bobby Nov 22 '16 at 14:57
  • see that linked question. that has a direct quote from the mysql manual advising against storing files in the database. – e4c5 Nov 22 '16 at 14:58
  • Look at all the nasty stuff you have had to do because you have stored files in a database. Your code would be a lot simpler and faster if you hadn't done so – e4c5 Nov 22 '16 at 14:58
  • But in practice, storing tiny files (tiny thumbnails say) can actually perform really well. I avoid the practice myself, but only because it's not widely supported. – Strawberry Nov 22 '16 at 14:59
  • You need to store the type. You won't need _"a whole bunch of if/else"_, though. Save the type and just: `data:image/= $row['type'] ?>;base64,`. Simple enough. – M. Eriksson Nov 22 '16 at 15:03
  • Btw, storing images in databases has major drawbacks. Every time you want to show an image, you need the overhead of connecting to the database and fetch the data, which he otherwise wouldn't need to do to see the larger version. Images won't be cached by the client either, when he shows them in his list. – M. Eriksson Nov 22 '16 at 15:08
  • Assuming I do store them in the DB like I did, do you guys have a solution to my question? – Bobby Nov 22 '16 at 15:28

1 Answers1

0

The answer to your first question is "You have to use light-box jQuery plugin", in order to display larger (original size) of a thumbnail you need to use any lightbox plugin, I recommend using this one http://www.jqueryscript.net/zoom/Responsive-jQuery-Lightbox-Like-Image-Zooming-Plugin-Lighter.html Or you can search for any other plugin, there are thousands of them.

Regarding your second question, If you will use data as an images source, you have to specify a type, no way you can do that without mentioning types.

Doaa Magdy
  • 518
  • 5
  • 20