用PHP输出文本图像时出现问题

用PHP输出文本图像时出现问题,php,imagettftext,Php,Imagettftext,我必须在屏幕上打印一些文本作为一个页面中的PHP图像,但它只显示一个白色正方形 以下是主页的代码: <?php SESSION_START(); if ($_SESSION["log"]!=true){ //die("<h1><center>Non sei loggato come amministratore</center></h1>"); header('location:index.php');

我必须在屏幕上打印一些文本作为一个页面中的PHP图像,但它只显示一个白色正方形

以下是主页的代码:

    <?php
    SESSION_START();
    if ($_SESSION["log"]!=true){
    //die("<h1><center>Non sei loggato come amministratore</center></h1>");
    header('location:index.php'); }

   include('ase.php');
    echo ('
                <!DOCTYPE html>
                <html>
                <body>
                <a href="./logout.php">Logout</a>
                <img src="data:image/png;base64,' . base64_encode($stringdata) . '">
                </body></html>');
      ?>

这里是应该输出图像的页面代码:

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

    // Create the image
    $im = imagecreatetruecolor(800, 2000);

    // Create some colors
    $white = imagecolorallocate($im, 255, 255, 255);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 800, 2000, $white);

    // The text to draw
    $text = "Read the text and the questions below. For each question mark 
    the letter next to the correct answer - A,B,C or D.";

    // Replace path by your own font path
    $font = 'arial.ttf';


    // Add the text
    imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

        ob_start();
        imagepng($im);
        $stringdata = ob_get_contents();
        ob_end_clean();
    ?>


(对不起,我的英文版)

我已经解决了删除标题(
标题(
)的问题。

而不是(可能是您正在做的)包含脚本并干扰输出缓冲,将php脚本设置为图像源url,然后正常输出图像,而无需输出缓冲。使用
imagedestroy($im)在
imagepng
之后销毁GD对象
。如果你不想做Charlotte Dunois建议的事情(你应该这么做),只需删除
标题('Content-Type:image/png')。多亏了@Federico,它才起作用!