Leon Gaban
Leon Gaban

Reputation: 39028

PHP script throwing up garbage in browser when placed inside HTML

So I took the PHP code that creates this page here, and just put it inside of a new .php file, and uploaded that file here.

I just see a ton of garbage characters on the screen. Any ideas?

Here is the full PHP below:

<html>
<head>
    <title></title>
</head>
<body>

<?

// gif is more appropriate than jpg for this kind of image
header("Content-type: image/gif");

// both coupon.jpg and Helvetica.ttf must be located in the same directory as
// dynamic.php or these variables must be updated.
$imgname    = "./coupon.jpg";
$fontname   = "./Helvetica.ttf";

// read image from disk
$im = imagecreatefromjpeg( $imgname )
    or die("Source coupon image has been moved or renamed! Expected coupon.jpg");

// variable allocation
$size       = 11;
$textheight = 250;
$imwidth    = imagesx( $im );
$black      = imagecolorallocate( $im, 0,0,0 );

// create the string containing tomorrow's date
$text       = "Offer expires " .
    date('l\, F j\, Y', mktime(0, 0, 0, date("m")  , date("d")+1, date("Y")))."."; 

// learn the width of the newly allocated string
$bbox       = imagettfbbox( $size, 0, $fontname, $text );
$width      = $bbox[2] - $bbox[0];

// update, display, and clear from memory the newly modified image
imagettftext($im,$size,0,$imwidth/2 - $width/2,$textheight,$black,$fontname,$text);
imagegif($im);
imagedestroy($im);

?>

</body>
</html>

Update added: My goal is to have a Google analytics conversion script on the new page / PHP generated image, is that possible?

Upvotes: 1

Views: 309

Answers (2)

Bertrand
Bertrand

Reputation: 388

Your PHP code creates an image.

This line:

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

Allows to modify the HTTP response of the server to tell the browser that you are sending an image. So you do not need all of the HTML code.

You should just keep the PHP code.

Upvotes: 3

Danny Beckett
Danny Beckett

Reputation: 20768

What did you expect? You're outputting HTML, then after some Content-type: text/plain output (i.e. the HTML), saying Content-type: image/gif

Remove the HTML around the PHP, and just keep the PHP.

Upvotes: 3

Related Questions