generalize gifify function