How to output a JPG/JPEG image in html with low quality for faster browsing?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hervagello
    New Member
    • Dec 2011
    • 1

    How to output a JPG/JPEG image in html with low quality for faster browsing?

    I have like 50000 images on my server, and each image is about 7mb of size. I made a gallery script based on the famous html code for displaying image :
    <img src="image.jpg" width="200" height="150">
    Even after setting the width and the height, the image is till taking time to display, because it's always download the 7mb sized image.

    So, I'm here to ask you, Experts, if there is any way to display these images in a form of thumbnail with a lower quality, so it can be displayed quickly. I would like to do that without creating thumbnails of those 50000 images.

    I appreciate any help or idea from your behalf.
    I hope you all find my question interesting and i hope that we can find out a solution for this problem.
    Thank you very much.
  • drhowarddrfine
    Recognized Expert Expert
    • Sep 2006
    • 7434

    #2
    No. The only thing you can do is have images resized smaller.

    Comment

    • omerbutt
      Contributor
      • Nov 2006
      • 638

      #3
      you can try this simple script if you have the jnow how of GD library

      Using Imagecopyresize d ()
      Code:
      <?php 
       // The file you are resizing 
       $file = 'your.jpg'; 
       
       //This will set our output to 45% of the original size 
       $size = 0.45; 
       
       // This sets it to a .jpg, but you can change this to png or gif 
       header('Content-type: image/jpeg'); 
       
       // Setting the resize parameters
       list($width, $height) = getimagesize($file); 
       $modwidth = $width * $size; 
       $modheight = $height * $size; 
       
       // Creating the Canvas 
       $tn= imagecreatetruecolor($modwidth, $modheight); 
       $source = imagecreatefromjpeg($file); 
       
       // Resizing our image to fit the canvas 
       imagecopyresized($tn, $source, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); 
       
       // Outputs a jpg image, you could change this to gif or png if needed 
       imagejpeg($tn); 
       ?>
      Using Imagecopyresamp led ()
      Code:
      <?php 
       // The file you are resizing 
       $file = 'yourfile.jpg'; 
       
       //This will set our output to 45% of the original size 
       $size = 0.45; 
       
       // This sets it to a .jpg, but you can change this to png or gif 
       header('Content-type: image/jpeg'); 
       
       // Setting the resize parameters 
       list($width, $height) = getimagesize($file); 
       $modwidth = $width * $size; 
       $modheight = $height * $size; 
       
       // Resizing the Image 
       $tn = imagecreatetruecolor($modwidth, $modheight); 
       $image = imagecreatefromjpeg($file); 
       imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); 
       
       // Outputting a .jpg, you can make this gif or png if you want 
       //notice we set the quality (third value) to 100 
       imagejpeg($tn, null, 100); 
       ?>
      optionally you an use phpthumb also see here
      regards,
      Omer Aslam

      Comment

      • seoquake
        New Member
        • Dec 2011
        • 1

        #4
        I still think the most optimal way is to generate thumbnails right after they're uploaded. Why? Because if you generate thumbnails on the fly, all the 7mb still have to be loaded into memory and considerable amount of processing has to be done each time a user views an image.

        Comment

        • omerbutt
          Contributor
          • Nov 2006
          • 638

          #5
          yes seoquake i agree ,
          that is why i have added a link to phpthumb in the end , whatever h likes , choices he have.
          regards,
          Omer Aslam

          Comment

          • nilswap
            New Member
            • Oct 2013
            • 4

            #6
            Better way is that u can convert all these images in .png format.Because, Noone is going to use .jpg or .jpeg formats.

            Comment

            Working...