将CDN与Image Resizer脚本一起使用

I'm using an image resizer to output images in my website. Is it possible to use a CDN in the current situation?

The image resizer takes the file path of the image and outputs the desired image. Now even if I use a CDN, the image resizer script is hosted on my server. So every image request is going through my server. Will using a CDN benefit me in any way?

If your sever and script is quicker enough then I would use your server code. This means you can play around with the script if you need to add custom functions. If you have any serious problems or want much more options which a CDN may provide then switch.

The cached object on CDNs are keyed on the request URI, so you should benefit from a CDN provided you application isn't generating any randomness in the URLs. If your image request looks like this

/resizer/200x100/thing.jpg
# ...or...
/resizer/thing.jpg?size=200x100

Then the CDN will cache it for all subsequent requests.

Short answer: No. If your server resizes images on-the-fly, then it still serves as a bottleneck, and the benefits of the CDN are essentially lost.

Three solutions you might be comfortable with:

  1. Have the image resizer script run once upon image upload, and create all necessary image sizes, then store everything in a logical manner on the CDN. (Reasonable solution. Downside: Very rigid, adding new image sizes requires considerable work).

  2. Have the image resizer script run (ie: resize a single image and upload to the CDN) upon request, but only if the image does not exist on the CDN already. (You can either store a list of created images in a database or, preferably, if at all possible use the object notation default image technique) (Cool solution. Downside: Old browsers don't like the object tag, non-standard, albeit valid, code).

  3. Switch CDNs, use a more mature CDN service that allows you to manipulate media files via API. ex: http://cloudinary.com/ (Smooth sailing solution. Downside: Not as cheap as the non-intelligent CDNs out there, but in most cases you should hardly feel it, and it will save you a ton of coding).

Hope this helps, I'd love to hear what solution you chose.