If you care about speed, you probably already cache your generated images to a file. In that case, DON'T use "createimagefrompng" and "imagepng" to output the image. Use fpassthru instead. It is literally hundreds of times faster.
<?php
header("Content-Type: image/png");
if( !file_exists($cachefile) ) {
$im = generateimage(); imagepng($im, $cachefile); imagedestroy($im);
}
$fp = fopen($cachefile, 'rb'); fpassthru($fp);
exit;
?>
I've tested it with a 5120x5120 (1.2Mb) image, that was cached on the harddisk. Using imagepng, the transfer took 12 seconds. Using fpassthru, it took only 32ms!