Paul
Paul

Reputation: 11756

PHP image thumbnail performance?

I've recently started trying to increase my sites performance. One of the biggest obstacles I have are the number of thumbnails being displayed.

I currently use the full size image and scale it down by defining a height/width value in the img tag. This doesn't seem very efficient so my question is whats the recommended way to display thumbnails? Should I maintain a second table in the DB for thumbnails or is there a better solution?

Upvotes: 2

Views: 857

Answers (6)

encodes
encodes

Reputation: 751

you could use a program like imagemagick to make proper thumbnails.

If its your personal site you could batch resize (theres a powertool for xp that does this) and then upload to a 200 directory and change your code. Obviously this relies on you uploading the images.

imagemagick will need to be installed on the server but will resize and allow you to play with the size of the images

Upvotes: 0

Vagelis Ouranos
Vagelis Ouranos

Reputation: 482

I use phpTumb to create thumbnails on the fly. You could also use it while uploading a picture to change its dimesions. It has many other features. Check it out.

Upvotes: 0

James Williams
James Williams

Reputation: 4216

You are looking for something like what can be found here: http://dtbaker.com.au/random-bits/php-simple-image-resize-thumbnail-generator.html (this turns all images into jpegs)

Like @martincarlin87 stated - Just need to add a check to see if it exists in the thumbnail directory and either send the information or create and send it through. This can be turned into a function as well.

Upvotes: 0

Aurimas Ličkus
Aurimas Ličkus

Reputation: 10074

For example create seperate folder in images call it thumnails (images/thumbnails) add there put files prefixed with file size for ex: "original_file_name_200X200.jpg", store on database "original_file_name" and extension "jpg" to seperate fields (name, ext) then when you need to display it select name add size prefix and add extension you get /images/thumbnails/file_200X200.jpg this way you can add later more sizes leaving original untouched.

Upvotes: 1

Bjoern
Bjoern

Reputation: 16314

Processing images takes its (cpu) toll, you should better avoid it where possible.

My advice: Create the thumbnails while uploading the images into separate image files, this way you can determine when to create/resize them - and not during runtime.

If you want the links to the thumbnails in a separate database field or derive it from the original name, is entirely up to you - both ways work.

This makes additional performance boosters easier to implement too (p.e. caching).

I've implemented a similar process in a php based project, its a good way to scale out. In my case, I am creating the thumbnails nightly via cron, because system load is very low in that time.

Upvotes: 4

Ilians
Ilians

Reputation: 743

If using an uploader to get the images on your website, have PHP resize the original image and upload both the original and a smaller thumb with some kind of prefix in the name. This way you can easily get the images from your database and just use "filename.jpg" for your normal images and "thumb_filename.jpg" for your thumb. Same can be done without an uploader of course but you'd have to manually create/upload the thumbnails.

Upvotes: 3

Related Questions