Added image filesize validation and some refactoring

That was copied from the plugin documentation and could well be clumsy.