Caution should be observed with imagedestroy(); copying your reference variable over to another variable will cause imagedestroy to destroy both at once.
Eg.:
$a = imagecreate(...);
$b = $a;
imagedestroy($a);
While you'd think $b still contains your image, it doesn't. Both $a and $b are destroyed.