How can the code snippet provided be optimized for better performance and readability when using PHP to generate dynamic image content with text?

The code snippet can be optimized by using PHP's image functions to directly generate the image content instead of relying on an external image file. This will improve performance by reducing file I/O operations. Additionally, using a more structured approach to handle text alignment and styling will enhance readability.

<?php
// Create a blank image with specified dimensions
$image = imagecreatetruecolor(400, 200);

// Define colors for text and background
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);

// Fill the background with white color
imagefilledrectangle($image, 0, 0, 399, 199, $white);

// Add text to the image
$text = "Dynamic Text";
$font = "arial.ttf";
imagettftext($image, 20, 0, 10, 100, $black, $font, $text);

// Set the content type header
header('Content-Type: image/png');

// Output the image
imagepng($image);

// Free up memory
imagedestroy($image);
?>