optimize detecting avatar image logic

cc @eviltrout @coding-horror