I'm trying to display an image on an HTML page using a PHP script in the image/src tag, but the image is broken. When I hard-code the output of getimage.php into the HTML it shows up just fine so I know the base64 encoding is correct. The page that shows the image needs to be HTML.

PHP:

    <?php
        header("Content-type: image/png");
        $data = file_get_contents('arbitrary.png');
        $base64 = 'data:image/png;base64,'.base64_encode($data);
        echo $base64;
    ?>

HTML:

<html> <body> <img src="getimage.php" /> </body> </html>

Any ideas? Thanks in advance!

PHP interpreter doesn't check .html pages unless stated otherwise.
In Apache2@Ubuntu/Debian use this directive in .htaccess file.

AddType application/x-httpd-php .html .htm

or

AddType application/x-httpd-php5 .html .htm

If you are running PHP as CGI, you should write

AddHandler application/x-httpd-php .html .htm

or

AddHandler application/x-httpd-php5 .html .htm

Solution from here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.