added error handling to image tag generation