In theory creating an image object and calling imagedestroy in your destructor should be a good way of doing it; something like:
<?php
final class My_Image() {
private $img;
public function __construct() {
$this->img = imagecreatetruecolor();
}
public function __destruct() {
if(is_resource($this->img)) {
imagedestroy($this->img);
}
}
}
?>
I check that $this->img is a resource in case imagecreatetruecolor() fails or you null the value somewhere down the line from a method call meaning that $this->img is NOT a resource, in which case imagedestroy is an unecessary function call that just fails with a warning message.