non ho provato ma sono sicuro che funzionerà.

ma non era più logico $img1 = NULL?