What best practices should be followed when handling resources like 'imagecreatefromjpeg' in PHP classes?

When handling resources like 'imagecreatefromjpeg' in PHP classes, it is important to properly manage memory usage and prevent resource leaks. One best practice is to always free up memory by using 'imagedestroy' to release the image resources when they are no longer needed.

class ImageHandler {
    private $imageResource;

    public function loadImage($imagePath) {
        $this->imageResource = imagecreatefromjpeg($imagePath);
    }

    public function processImage() {
        // Image processing code here
    }

    public function freeImage() {
        if ($this->imageResource) {
            imagedestroy($this->imageResource);
        }
    }
}