Experts Round Table Network

Serverside Technology => PHP => Topic started by: gfreeman on January 25, 2008, 11:08:34 AM



Title: header()
Post by: gfreeman on January 25, 2008, 11:08:34 AM
Argh ...

Hi folks, having what appears to be the usual newbie header() problem.

Here's a sample of what's not working:

test.php:
Code:
header("Content-type: image/png");
header("Content-type: text/html", false);
echo "some text<p>";
include "drawshirt.php";
echo "<P>some more text<P>";

the drawshirt.php looks like this:
Code:
$im = @imagecreatefrompng('images/strip.png')
or die("Cannot open image file");

#SET TRANSPARENCY
     imagesavealpha($im, true);
     $trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
     imagefill($im, 0, 0, $trans_colour);

$shirt_r=255; $shirt_g=0; $shirt_b=0;
$short_r=0; $short_g=255; $short_b=0;
$socks_r=0; $socks_g=0; $socks_b=255;

$shirt=imagecolorallocate($im, $shirt_r, $shirt_g, $shirt_b);
$short=imagecolorallocate($im, $short_r, $short_g, $short_b);
$socks=imagecolorallocate($im, $socks_r, $socks_g, $socks_b);

#FILL SHIRT
imagefilltoborder($im, 11,  5, $black, $shirt);
imagefilltoborder($im,  9, 15, $black, $short);
imagefilltoborder($im,  9, 21, $black, $socks);
imagefilltoborder($im, 14, 21, $black, $socks);

imagepng($im);
imagedestroy($im);

What appears in the browser when displaying test.php is the two lines of text separated by a string of garbage ...

What am I doing wrong?


Title: Re: header()
Post by: mholt on January 25, 2008, 04:55:55 PM
You're trying to send text to a browser that thinks it is getting a PNG image instead... once you send headers, that's it. You're trying to send two types of headers, then text, then a picture, then more text.

If you want a PHP script to send an image file, but still want text and other things around it (in the same file), try using an <img> HTML tag in test.php and set its src to the location of the drawshirt.php file. If you do that, then you don't need those header() lines in test.php.

So test.php should look something like this then:
Code:
<?php
echo 'some text here...<br><br>';
echo '<img src="drawshirt.php">';
echo '<p>some more text</p>';
?>

Make sure you add this line:
Code:
header("Content-type: image/png");
to drawshirt.php before you send any data to the browser.


Title: Re: header()
Post by: gfreeman on January 25, 2008, 07:06:30 PM
echo '<img src="drawshirt.php">';

That's what I was missing!

Thank you for fixing this for me!!