How to display externally hosted images without being surprised by unexpected file sizes or other unforseen problems
So I run a webgame over at http://ninjawars.net
Displaying externally hosted images
I want to provide embeddable images that aren't avatars (I'm aware of and already using the great service of gravatar, but these images won't be tied to any email), this is because I've found hosting your own uploaded images to often be a pain, and other people are already doing it better, so to speak.
Preventing excesses for external file sizes
However, this somewhat takes the control over the image size away. Using a simple link, this would allow the user to upload some large file size, and though it would be displayed with a certain display size in the css, it could force some silly long load time.
One way: Constraining 开发者_开发知识库the url
So I expect to need to limit the url parameters somewhat once they're input by a user, limiting the options to certain image hosting services. My initial thought with this is to require fixed sections of the url, a form that displays:
[http://flickr.com/photos/] [ put in the rest of your photo url here ] [/sizes/sq/]
So it'd limit the image to being hosted on flickr, and to a certain url that presumably sets the size on flickr, possibly with some back-end PHP processing that would strip any duplicate instances of the first and last parts of the url so that people can just post in the full url and have it work.
Better alternatives?
So the question is, is this a silly way to do it? Are there better javascript or php solutions already out there? How should I externally host images while still exerting some semblance of control? Or is this amount of control paranoid and not really necessary?
Use HTTP "HEAD" request to get file size w/o downloading it. response field "Content-Length". Checking it in php or in js(ajax)... I would prefer js, cause it remove load from server to clients. Also, according to HTTP 1.1 no more connection should be used from one user-agent. And many users of your server could result in many simultaneous connections from your server to image hosting server.
Most people have absolutely no clues about file size so I think it's not paranoid to worry about possibly big images : they will try to use 4Gb pictures as avatars.
There will be no generic way to have any control on the image if you get an url.
You could get the image yourself, resize it, store the result and serve it but that's more or less the same than uploads...
Yes having to host uploaded images is painful but seems to be te best solution.
You have to watch out for some things :
- limit the size of the upload so that a user cannot make your serve choke
- deletes files with unexpectd formats
- use imagemagick or similar to resize the images ASAP to avoid having to much space eaten by the files
- do some cleanup to avoid keeping obsolete images filling your disk
requires some work but it's the better way to have some control...
And don't forget the problems with offensive and/or copyrighted images you'll have o cope with that whatever your technical solution)
精彩评论