Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

header()
Welcome, Guest. Please login or register.
August 20, 2008, 11:12:02 AM
11291 Posts in 1243 Topics by 833 Members
Latest Member: tkovalenko
Experts Round Table Network  |  Serverside Technology  |  PHP  |  header() « previous next »
Pages: [1]
Author Topic: header()  (Read 138 times)
gfreeman

Offline Offline

Posts: 2


« 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?
« Last Edit: January 25, 2008, 11:11:47 AM by gfreeman » Logged
mholt

Offline Offline

Posts: 1


« Reply #1 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.
« Last Edit: January 25, 2008, 05:03:11 PM by mholt » Logged
gfreeman

Offline Offline

Posts: 2


« Reply #2 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!!
Logged
Pages: [1]
« previous next »
    Jump to: