5

There is a file in stored in the server, which for instance has dimension of 400 X 600 pixels. But in different part of the websites, we need the same picture of different dimension. For thumbnail, we usually need 50 X 50 pixels and if we choose to download the same picture with original dimension, it would take longer and the page load would slow down. If that picture is resized its size would go much more down (like, 500 KB to 50 KB). So, what I want is, before the browser downloads the picture, server should resize the picture to the dimensions as required in PHP script.

Dilip Raj Baral
  • 3,060
  • 6
  • 34
  • 62
  • 4
    The best way of doing that is to create the thumbnail of 50*50 while uploading original image at server. so that as per the requirement you can pull the relevant image. – Neeraj Jun 18 '12 at 11:15
  • we may have thousand of users. storing different versions of images of each of them would take a lot of storage space... – Dilip Raj Baral Jun 18 '12 at 11:18
  • 3
    doing it live and not storing means that you will do it thousands of times and at some point you'll decide to store it anyway. – Elzo Valugi Jun 18 '12 at 11:21
  • 1
    @RajBD You complain that caching the images ahead of time will take up space, but you don't realize the alternative is worse since you're redundantly allocating memory / storage space / processor by doing it on the fly. –  Jun 18 '12 at 11:26
  • possible duplicate of [Resize a picture to a fixed size](http://stackoverflow.com/questions/747101/resize-a-picture-to-a-fixed-size) – Emil Vikström Jun 18 '12 at 11:30
  • 1
    when you will have thousands of users accessing your site, your website will starting to slow down. For websites having large users accessing at the same time, best practice is to resize and store the image on server. At this point, performance is far more important than disk space. – FatalError Jun 18 '12 at 11:35

5 Answers5

1

You'll need PHP's GD library, then take a look at this and this and then link a php file which resizes instead of the image itself.

Also note the suggestion of neeraj, so you may resize it while uploading to get better performance.

In case if your host lacks PHP GD - which is highly unlikely - you should take a look at this or this.

Community
  • 1
  • 1
axiomer
  • 2,088
  • 1
  • 17
  • 26
1

Yes, with the PHP GD2 extension. Here's a quick tutorial

Blu
  • 4,036
  • 6
  • 38
  • 65
nyson
  • 1,055
  • 6
  • 20
1

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

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

You need change the "DSC01088.jpg" to echo $_GET['img']; after proper validation.

Community
  • 1
  • 1
FatalError
  • 922
  • 12
  • 31
0

Yes, of course!

Google for PHP image resize code samples

Then create a download script and call it with the parameters, like this:

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

The script will load the image, resize it to the provided width and height and optionally crop it, then echo with appropriate headers for download...

There are a lot of ways of doing this... Sometimes it is just better to use google prior to asking here at SE...

Blu
  • 4,036
  • 6
  • 38
  • 65
shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • Its not that I didn't google. I just wanted to reach to the specific answer in less time, like I reached to your answer. Thanks anyway.. – Dilip Raj Baral Jun 18 '12 at 11:32
0

Use GD Library to create thumbnails

firstly get list of all the images in 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  
         }
     } 
 }
Naveen Kumar
  • 4,543
  • 1
  • 18
  • 36