Yahel Yahel - 1 year ago 156
Javascript Question

Most efficient way to display a 1x1 GIF (tracking pixel, web beacon)

I'm building a basic analytics service, based in theory off of how Google Analytics works, but instead of requesting an actual image, I'm routing the image request to a script that accepts the data and then outputs an image. Since browsers will be requesting this image on every load, every millisecond counts.

I'm looking for the most efficient way for a file to output a gif file from a PHP script. So far, I've established 3 main methods.

Is there a more efficient way for me output a 1x1 GIF file from within a PHP script? If not, which of these is the most efficient and scalable?

Three Identified Methods

PHP image building libraries

$im = imagecreatetruecolor(1, 1);
imagefilledrectangle($im, 0, 0, 0, 0, 0xFb6b6F);
header('Content-Type: image/gif');

the image off of the server and output it

$im = file_get_contents('raw.gif');
header('Content-Type: image/gif');
echo $im;

the image

header('Content-Type: image/gif');

(My gut was that base64 would be fastest, but I have no idea how resource intensive that function is; and that file_get_contents would likely scale less well, since it adds another file-system action.)

For reference, the GIF I'm using is here: http://i.stack.imgur.com/LQ1CR.gif


So, the reason I'm serving this image is that my analytics library builds a query string and attaches it to this image request. Rather than parse logs, I'm routing the request to a PHP script which processes the data and responds with an image,so that the end user's browser doesn't hang or throw an error. My question is, how do I best serve that image within the confines of a script?

Answer Source


header('Content-Type: image/gif');
//equivalent to readfile('pixel.gif')
echo "\x47\x49\x46\x38\x37\x61\x1\x0\x1\x0\x80\x0\x0\xfc\x6a\x6c\x0\x0\x0\x2c\x0\x0\x0\x0\x1\x0\x1\x0\x0\x2\x2\x44\x1\x0\x3b";

That will output a binary string identical to the binary file contents of a 1x1 transparent gif. I'm claiming this as efficient based on the grounds that it doesn't do any slow IO such as reading a file, nor do I call any functions.

If you want to make your own version of the above hex string, perhaps so that you can change the color, you can use this to generate the php code for the echo statement.

printf('echo "%s";', join('', array_map(function ($char) {
    return '\x' . dechex(ord($char));
}, str_split(file_get_contents('https://upload.wikimedia.org/wikipedia/commons/2/23/1x1.GIF')))));