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

时间:2016-03-26 11:56:13

标签: php imagettftext

我要在屏幕上打印一些文本作为图像在页面中使用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();
    ?>

(对不起我的英文)

1 个答案:

答案 0 :(得分:0)

我已经解决了删除标题(header('Content-Type: image/png');)的问题。