Search code examples
phpimagettftext

Problems outputting text image with PHP


I've to print some text on the screen as an image with PHP in a page, but it only shows a white square.

Here is the code of the main page:

    <?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>');
      ?>

And here the code of the page that should output the image:

 <?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();
    ?>

(sorry for my english)


Solution

  • i've resolved the problem removing the header ( header('Content-Type: image/png'); ).