How can one optimize the code provided to ensure smooth execution and proper display of the generated image in PHP?

The issue can be optimized by using the imagepng() function to output the generated image directly to the browser. This will ensure smooth execution and proper display of the image without any interference from other output.

<?php
$width = 400;
$height = 200;
$image = imagecreatetruecolor($width, $height);

$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);

imagefilledrectangle($image, 0, 0, $width, $height, $white);
imagettftext($image, 20, 0, 10, 50, $black, 'arial.ttf', 'Hello, World!');

header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>