Rajat Pawar
Rajat Pawar

Reputation: 39

Output a PNG image from a PHP include file

I have a include file called sign.php which has some code -

header('Content-type: image/png');
.....
imagepng($image_file);
.....

A file where I want to display this image - index.php

......
<what comes here?> include('../sign.php');
.....

How would I display it? Directly echo-ing it doesn't work!

Thanks.

MORE CODE:

sign.php

    .....
   {
    imagettftext($im, 22, 0, 240, 43 , $text_color, $font, $text_god);
    imagettftext($im, 22, 0, 241, 44 , $text_color, $font, $text_god);
}
imagettftext($im, 22, 0, 60, 42 , $text_color, $fontname, $text_username);
     imagettftext($im, 16, 0, 230, 72 , $text_color, $font, $text_kills);
imagettftext($im, 16, 0, 230, 102 , $text_color, $font, $text_deaths);
imagettftext($im, 16, 0, 230, 132 , $text_color, $font, $text_rwon);

imagettftext($im, 16, 0, 365, 72 , $text_color, $font, $text_alevel);
imagettftext($im, 16, 0, 365, 102, $text_color, $font, $text_dlevel);
imagettftext($im, 16, 0, 365, 132 , $text_color, $font, $text_rlost);
imagettftext($im, 16, 0, 365, 162 , $text_color, $font, $text_money);   // Prints the money in the picture. 

imagecopymerge($im, $skinImg, 505, 56,0,0,55,99,100);

header('Content-Type: image/png');

imagepng($im); 

    imagedestroy($im); 

index.php

    .......
    <?php echo '<img src="../signature/sign.php" />'; ?>
    .......

Still doesn't work!

EDIT:

index.php

            include('../signature/sign.php');

            echo '<img src="data:image/png;base64,' + base64_encode($stringdata) + '">';

sign.php

http://pastebin.com/uz1U3V4R

Upvotes: 1

Views: 2242

Answers (3)

Ephi Gabay
Ephi Gabay

Reputation: 938

This happens because imagepng directly outputs the content of the image to the browser. You should save the content of the image into a variable like this in sign.php:

... 
imagecopymerge($im, $skinImg, 505, 56,0,0,55,99,100);  

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

imagedestroy($im);   
...

And then display it in your index.php like this:

<?php
    include('sign.php');
    echo '<img src="data:image/png;base64,' . base64_encode($stringdata) . '">';
?>

Upvotes: 3

Ferrakkem Bhuiyan
Ferrakkem Bhuiyan

Reputation: 2783

<img src='< ?php echo 'data:image/png;base64,' . base64_encode(file_get_contents('img.png')) ; ?> '>


<img src='<?php include("img.png"); ?>

This should work for you.

Upvotes: 0

Alireza Fallah
Alireza Fallah

Reputation: 4607

According that you said you tried <img src='sign.php'> and its not working, I guess something is wrong with set the header,Consider that

No output before sending headers

Functions that send/modify HTTP headers must be invoked before any output is made.

Some functions modifying the HTTP header are:

Output can be:

  • Unintentional:
  • Intentional:
    • print, echo and other functions producing output (like var_dump)
    • Raw <html> areas before <?php code.

Upvotes: 0

Related Questions