| I'd break it down into two parts. The first is the image transformation. For this we use graphics magic, and based on the params in the querystring, we'd crop, resize, alter the quality, anchor the image to a focus point and so on. Not copy and pastable, but [1] should give you a rough idea. Also [2] is code we use to get the file type and size of the image (gm identify can be painfully slow). The second part was a bit more "fancy". There were two really slow parts to this (a) fetching the origin (from S3) and (b) applying lossless compression (for a large image, it can take 10+ seconds). Fetching from origin is easily solved by caching the origin to disk. So if you ask for goku.png?w=90001&h=9001 and then goku.png?w=2393&h=43433 it's only going to be 1 origin fetch. For the lossless compression, we just used the filesystem as a queue. We'll serve up the umcompressed image with a short cache header (maybe 10 minutes) and store it in /storage/uncompressed. The filesystem is monitored and when a file is added, we compress it and them move it to /storage/compressed. So, when you serve an image, the flow is: - check for the file in /storage/compressed/ and serve that with a long cache header (this is a fully transformed image (hash the querystring parameters)) - check for the file in /storage/uncompressed/ and serve that with a short cache header (this is a fully transformed image (hash the querystring parameters)) - Check if we at least have the original in /storage/original - if not, fetch the original, put it in /storage/original
- Transform the image, store it at /storage/uncompressed and serve it up- In the background, compress images and move them from /storage/uncompressed to /storage/compressed It might seem like overkill when you consider that, despite serving thousands of images per second, the CDN handles almost every request. The problem is with the lossless compression. We found it impossible to do it on-the-fly for too many of our images, so you absolutely need that available and ready to go for the 5% CDN miss. [1] https://gist.github.com/anonymous/8f328359f07f6c5d142e [2] http://openmymind.net/Getting-An-Images-Type-And-Size/ |
Do you handle the malicious case of someone supplying various widths and heights potentially DoSing the server?