1

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?

4
  • 1
    might not be related, but shouldn't the php tags be <?php ?> ? It's safer this way in case your php.ini disables the shorthand tags. Commented Mar 6, 2013 at 2:27
  • You can't mix a HTML page and binary output (image) in one php script. Commented Mar 6, 2013 at 2:29
  • 1
    Why did you paste the file inside another file? Wouldn’t it make more sense to simply save the file that works and upload it to the new location? Commented Mar 6, 2013 at 2:29
  • @LeonGaban You should consider using Piwik as your tracking tool. It can track calls via PHP, too. All other calls from "normal" visitors are tracked as simple as GA. But additionally it inserts an image to track non javascript users, too. It is in any way better than GA, in my opinion. It is also open source. piwik.org Commented Mar 10, 2014 at 8:19

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Will I be able to still add Google analytics to the page then? Can it be included inside of the PHP tags?
No, there is no way to add GA, since this requires JS to be output to the browser. You can either display Content-type: text/plain OR Content-type: image/gif, but not both.
Thank you! After hearing what was going on.. it's and image! Made sense, so I just now created a new .php page and used and iframe to pull in the graphic. Now the coupon graphic is sitting on a page that I can install the GA script on :) I'm sure there's a better way, but this fixes my problem.
3

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.

3 Comments

Can I include Google Analytics javascript inside the PHP?
No, you can not (from my knowledge). What kind of measure are your looking for ? B.
Oh it was a conversion script, was helping a friend actually. He had a link on his site go to this coupon link. The coupon PHP then generated an image. The GA script needed to be on the page that was clicked too. I solved the issue by creating another page which pulled in the coupon PHP generated image in an iframe, so was able to put the script there :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.