tags:

views:

299

answers:

9
  1. Save one image path in the database... and whenever a call is made to output a smaller size of that image, dynamically resize it , assign it to a variable and call that variable?

  2. Resize image during upload and create thumbnails of it according to your needs and have database columns for the original image path and the resized image paths?

A noob here, obviously

+2  A: 

Or 3rd.

Resize image. Store it in a location of some sorts (ie based on it's size) and call all resized images a certain name.

/images/thumb/200x100/200x100_picture.jpg

Then you do a check if there is a file called that. If so, serve it, if not resize and serve. No db of thumbs needed. Just a check if a file exists.

Ólafur Waage
How you can check file if you don't know extension of file? That will only work if you works with just one type of file...
sasa
+3  A: 

Dynamically resizing images each time they are loaded could put a lot of load on your webserver, depending on how busy your server will be.

Another approach is to only resize it when required. So when someone requests an image of a certain size, resize it, and store it. If another request comes for the same image/size, you would serve your already resized image.

Andre Miller
A: 

Have a look at Upload class

It has nice options to resize / crop images.

With this library it's dead simple to make some thumbnails of Your uploaded images.

In My opinion, the best way to store images is on the filesystem, not in the database. You can then later simply get all images via FTP for example from the server.

astropanic
+2  A: 
  1. Save original image to file and add path to database. Let's assume that you've saved it to ./images/image001.png.
  2. If a request is made for the resized version of that image, check if the file ./images/thumbnails/image001.png exists. If it does, great, we can output that image!
  3. If it doesn't, however, use the GD2 or ImageMagick libraries in PHP to manipulate the image on the fly. Save the new image to ./images/thumbnails/image001.png and then send that to the browser.

This method will help in several ways. Firstly, the image is only manipulated the first time it is requested, cutting out any processing time that is unnecessary. Secondly, it cuts out a column in your database for the path to the modified image, saving you disk space.

mynameiszanders
A: 

The GD image library in PHP is fast, but this comes down to the question of which is more of concern on your system: CPU or storage? Resizing from a single image is light on storage but heavy on CPU. If you don't have a vast number of images or traffic, it could make for a small backup archive. On the other hand if storage is not a problem (and it shouldn't be these days) you can easily do what Ólafur Waage or Andre Miller suggest and store them separately.

JYelton
A: 

I have used approach 1 on a reasonably sized website I have built and there were no performance issues. Magento handles images in this way as well. So save the image when it is uploaded, then when the php view scripts call for the image, generate it on the fly, saving to an appropriate folder (pretty much a cache).

One thing I found difficult when using the method you described in point two, wasn't from a performance or coding point of view. It is a bit of a pain re-creating images when the client or designer changes their mind on the size of the thumbs / images etc. You then need to regenerate all the thumbs. This is ok on smaller sites, but can be a pain on larger ones.

JonB
+1  A: 

If you store the original image in a 'original' folder, and then, when the need arises, resize it, you are saving on disc space upfront AND processing power. When you resize later, store the resized copy in another folder, for future use. This way you are also helping users take advantage of the browser's caching system. An image that is dynamically resized everytime is not cached and puts a LOT of strain on the server. Consider a page where you are loading several images, all of them resizing in real-time, there's a server killer!

Cristi Cotovan
A: 

If you are low on disk space, you will have to then compromise your server load and load thumbnails dynamically using Option 1

If your server is very busy or at slow speed, it would be good to use Option 2.

But to overall speed up your website, you can combine the 2 options.

Say we have an image file called ImgZA.jpg on your server. You can dynamically call a script call imgproc.php?id=ImgZA.jpg&h=50&w=50. The imgproc.php script will check whether the thumbnail of ImgZA.jpg with size of 50 by 50 pixels is already created. If the thumbnail is already there, just use the thumbnail. If not then create the thumbnail then store it.

This will optimize disk space, server load and allows you to prevent hotlinking as well (in your imgproc.php script).

thephpdeveloper