Nov 172011
 

In developing one of my task management projects I was playing around with creating a tracking pixel that could be used to measure performance. When this pixel was placed on the page, I noticed in Safari and IE the browser would ask me if I’d like to open or save the file.

Safari said:

Do you want to open or save this file?

Name: pixel.php

Type: PHP File

Internet Explorer asked:

Do you want to open or save pixel.php from taskshot.com

Both had the buttons to Open / Save / Cancel

 

As you can tell, this is not what I wanted to happen, in fact, it would probably scare people away. So I looked to fix it. Here’s the code I was using:

//spit out pixel just in case browsers want it
$im=imagecreate(1,1);
$white=imagecolorallocate($im,255,255,255);
imagesetpixel($im,1,1,$white);
header("Content-type: image/jpg");
imagejpeg($im);
imagedestroy($im);

I thought something had to be wrong with the header, and after trying another file type with success, I tried changing the content-type from jpg to jpeg.

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

Surprisingly enough, that was all it took to fix it. I don’t remember where I got this sample code from, but it was wrong. Content type for JPEGs should be image/jpeg, not image/jpg.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)