What is the best way to store images? Currently when an image is uploaded I resize it to 3 different sizes (a thumbnail, a normal size, and a large size). I save in a database a description of the image, the format, and use the id number from the database as the image name. Each size image has its own directory.
Should I be storing the images in the开发者_如何学Python database? Should I only be storing the larger size and generate the thumbnail as needed? Or any other ideas you have?
What you described sounds reasonable to me. You could store the images in your database but you wouldn't notice any major performance boosts and, if your webhost limits the size of your MySQL database, you might find you're overpaying for database space.
As for resizing the images, doing it once and storing the three copies is way less computationally expensive than generating smaller images once per request; however, you may want to consider having the browser do your thumbnail generation for you (just specify an img tag with the src set to the full size image plus width/height attributes), which would allow you to save CPU power and disk space at the expensive of having not-so-pretty thumbnails (browsers generally scale images for speed at the expensive of quality).
You can choose a mix of both worlds, saving the big image on the SQL (or HDD). You generate the thumbnails on the fly when requested, but then cache them on the server for a certain ammount of time.
This means at any time you will have on the disk the big images and the thumbnails for the most recent / frequently used ones.
If you had a drupal powered site, this functionality is built in with http://drupal.org/project/imagecache
精彩评论