php gd image didn't show up

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 before and change it afterwards. Here is the code:

HTML:
<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 9
‰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? Thank you!
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
You've already posted this question and got the correct response in your other thread.
 

fatpandahk99

New Member
Messages
5
Reaction score
0
Points
0
I tried exit(), and it doesn't work. And I put no space in front of header, that doesn't work either. Therefore, I posted again. Could anyone give me solution? Thank you.

---------- Post added at 05:07 PM ---------- Previous post was at 04:58 PM ----------

lemon-tree: Thank you so much for your reply. I didn't get that before I reposted it. I got what Mission means and make it works. Thanks again. Sorry if the repost bothered you.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
As long as you're dealing with the same problem, stick to the thread. If you don't understand a solution, you should ask for clarification in the same thread. If none of the offered solutions work, post this information in the same thread, along with how you applied the solutions and what the result was. If you haven't gotten a response that resolves your solution, and no-one has responded in a few days (a few hours isn't enough), bump the thread. Starting a duplicate thread is just spamming.

If you're working on a new problem, then start a new thread.
 
Last edited:

froger

New Member
Messages
49
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 before and change it afterwards. Here is the code:

HTML:
<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 9
‰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? Thank you!

You are trying to send a header after the headers have already been sent. You see header("Content-Type: image/png") needs to be set before the start <html> tagg.

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

?>
should be lets say, image.php.

By itself it will view the image because the pages content-type is image/png.

If you want to place it into html though, you will have to create an <img> tag for image.php.

Get it?
 
Last edited:
Top