php gd image do not displayed properly

fatpandahk99

New Member
Messages
5
Reaction score
0
Points
0
Hi! I am a newbie in here. I try to create a simple image using php gd (in a file called index.html in the public_html folder), but there is no image show up. I have a header in html code and change it afterwards. Here is the code:

<html>
<body>
<?php

$im=imagecreatetruecolor(120, 20) or die('Cannot Initialize new GD image stream');

$text_color=imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);

header("Content-type: image/png");

imagepng($im);
imagedestroy($im);
?>
</body>
</html>

And here is the output:
Warning: Cannot modify header information - headers already sent by (output started at /home/kimo/public_html/index.php:3) in /home/kimo/public_html/index.php on line 17
‰PNG IHDRxÉ0n¤æIDATX…í”Ià EiÕ5ÛŽóåpl{‚."YVüm‚:Hÿ-Á38.…B!äÜìQ¯ ”ò|mÐ`—j…^›§|Ê3Œrˆè:‡œ‡ ç+šGr:”Îù_«hZÑõ*&¸_´ïµé·Ù÷²jil{6￾Þè5ãÇóà²ÒO%Ž<_Vä• «œiFèºÍä¯ÕK¾åÄE?ì‘2h￾s:¡`ÜçGaFÙÓ￾9˧^=[Ù/;Ãm»Ãk 8mW&Ç+”ij%(s(ˆõ~+›E¬Ð„B!äßy￾¤Í"¥™íIEND®B`‚

Could anyone let me know what I did wrong?
 
Last edited:

dlukin

New Member
Messages
427
Reaction score
25
Points
0
1. Next time use "Go Advanced" and put your code either in PHP or CODE tags

2. Post all the code. It is complaining about line 17 and you don't have that many lines.
 

bhupendra2895

New Member
Messages
554
Reaction score
20
Points
0
Hi, Nothing should be sent before header header() even a white space is not allowed and use exit() after you sent header to user, so script will terminate.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Ask yourself: why are you outputting HTML elements in an image? Remove the <html> and <body> tags. That particular error has been covered many times before on these very forums.
 
Last edited:

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
For the sake of closing this issue, this is what Misson is suggesting:
PHP:
<?php
header("Content-type: image/png");
$im=imagecreatetruecolor(120, 20) or die('Cannot Initialize new GD image stream');
$text_color=imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
?>
 
Last edited:

fatpandahk99

New Member
Messages
5
Reaction score
0
Points
0
Thank you for the replies from Mission and Lemon-tree. I have been searching around for solution on that, but I found nothing. Thanks again.
 
Top