PHP - resizing the image on the server before loading the browser

The server stores a file, which, for example, has a size of 400 X 600 pixels. But in different parts of websites we need the same picture of a different dimension. Thumbnails usually require 50 x 50 pixels, and if we want to load the same image with the original size, it will take longer and the page loading will slow down. If this image is resized, its size will be much lower (for example, from 500 KB to 50 KB). So, I want, before the browser loads the image, the server must resize the image to the size required by the PHP script.

+5
source share
5 answers

You need to put the code in the sketch code in a new file, say image.php, and then use this script as the src of the img tag with the parameters for the image file name, width and height.

<img src="http://path-to-directory/image.php?img=abc.jpg&width=50&height=50" />

You need to change the "DSC01088.jpg" for the echo $ _GET ['img']; after the correct check.

+1
source

You will need the PHP GD library, and then look at this and then link the php file, which resizes instead of the image itself.

Also pay attention to the neeraj suggestion, so you can resize it at boot time to improve performance.

If your host lacks PHP GD, which is unlikely, you should take a look at this or this .

+1
source
+1

, !

Google PHP

script , :

http:///www.mysite.com/image.php?image=path/to/my/image.png&width=50&height=50&crop=1

script , , , , ...

... google, , SE...

0

Using the GD Library to Sketch

first get a list of all the images in the folder

 function thumb()
 {
     $img_dir_path='images/';
     $thumb_dir_path='images/thumbs/';

     $img_array=GLOB($img_dit_path.'*.{jpg,jpeg}',GLOB_BRACE);

     foreach($img_array as $img)
     {
       list($path,$file)=explode('/', $img);
       list($fileName, $extension)=explode('.',$file);

        $thumb-file=$thumb_di_path . $fileName. '-thumb.jpg';

         if(file_exists($thumb-file))
         {
               //do nothing 
         }
         else 
         {
            //create file using GD and set width and height proportionally
            // Example $width=$orig_width * 0.1; $height =$orig_height * 0.1  
         }
     } 
 }
0
source

All Articles