improved get_image_attachments function