Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using XAMPP to serve php pages. All my PHP codes are running fine. But when I create image using the following php code :

XML
<!DOCTYPE html>
<html>

<head>

</head>

<body>

<?php
$myImage = imagecreate( 200, 100 );
$myGray = imagecolorallocate( $myImage, 204, 204, 204 );
$myBlack = imagecolorallocate( $myImage, 0, 0, 0 );
imageline( $myImage, 15, 35, 120, 60, $myBlack );
header( "Content-type: image/png" );
imagepng( $myImage );
imagedestroy( $myImage );
?>

</body>
</html>


and run the page the Google Chrome and Firefox show just an image place holder and IE8 shows the following binary code:

<!DOCTYPE html>
<html>

<head>

</head>

<body>

‰PNG


IHDRÈdùHíHPLTEÌÌÌÓ33dMIDATH‰c`£` Àx§”
Nf*k²Ã)Ãø§”•5}À)ŏS†ÚšpJUà”a§²¦œ2ÔŽw<špj‚q0>
†;
uTBúŸIEND®B`‚
</body>
</html>

I have checked php.ini file and have removed ';" from before "extension=php_gd2.dll". Still not working. Please help.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Aug-14 15:35pm    
Aren't you drawing black line on black background? Your gray color is not used anywhere.
And use imagecreatetruecolor() instead.
—SA
Mohibur Rashid 19-Aug-14 22:19pm    
The code is right. I already checked. draws black line on gray background. But the mistake is, he is also putting html data with image data. And sending header request after sending data output. I have already written, in detail, in answer section.
Sergey Alexandrovich Kryukov 19-Aug-14 22:55pm    
Ah, great! Some bugs don't even come to mind. Well-spotted.
—SA

1 solution

Here is few thing you need to understand about PHP and HTTP protocol.
According to HTTP protocol header must have to be sent by the server first.

php treats its header and data in two ways:
1. php gether all data and header seperately and store it memory. After the end of execution php will send the header and then data
2. PHP will start to send data right away from the beginning of the execution.

In most case the servers are configured as no 2.

So, what you did here is first you put html code. then in the middle you are sending header. Before php send the first byte out it sends out all its header. That is, before php sends out "" php already send out the header. So, no matter how many header you set afterward they will not be effected. Now your case what is happening is even worse. First you are sending out some html text and then telling the parser that its an image. Even if you do the right thing i.e. first sending the header and then sending out data, it will still suffer. The reason is, you are still sending invalid data, your html texts, for your image. The right code would be

PHP
<?php
$myImage = imagecreate( 200, 100 );
$myGray = imagecolorallocate( $myImage, 204, 204, 204 );
$myBlack = imagecolorallocate( $myImage, 0, 0, 0 );
imageline( $myImage, 15, 35, 120, 60, $myBlack );
header( "Content-type: image/png" );
imagepng( $myImage );
imagedestroy( $myImage );


That is it. Make sure there is no other data before <?php and no data after ?>. it would be way better if you do not use ?>
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Aug-14 22:57pm    
Well-spotted. Obviously deserves my 5. I would only advise to use imagecreatetruecolor() in all cases, not imagecreate().
—SA
Mohibur Rashid 19-Aug-14 23:05pm    
thanks
Madhukar Krishna 20-Aug-14 1:42am    
Thanks Rashid! This solved my problem. Kudos for your solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900