What potential pitfalls should be considered when attempting to create an image from HTML using PHP?

One potential pitfall when creating an image from HTML using PHP is the risk of including malicious code in the HTML input, which could lead to security vulnerabilities. To mitigate this risk, it's important to sanitize and validate the HTML input before using it to generate the image.

// Sanitize and validate HTML input before creating image
$html = "<p>This is some HTML content</p>";
// Sanitize HTML input
$clean_html = strip_tags($html);
// Validate HTML input
if ($clean_html !== "") {
    // Create image from HTML
    $image = imagecreatefromstring($clean_html);
    // Output or save the image
    imagepng($image, 'output.png');
    imagedestroy($image);
} else {
    echo "Invalid HTML input";
}