开发者

PHP to return 1x1 transparent GIF, how is this data encoded?

I have seen this web beacon image here

The code looks like this:

header( 'Content-type: image/gif' );
# The transparent, beacon image
echo chr(71).chr(73).chr(70).chr(56).chr(57).chr(97).
     chr(1).chr(0).chr(1).chr(0).chr(128).chr(0).
     chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).
     chr(33).chr(249).chr(4).chr(1).chr(0).chr(0).
     chr(0).chr(0).chr(44).chr(0).chr(0).chr(0).chr(0).
   开发者_开发技巧  chr(1).chr(0).chr(1).chr(0).chr(0).chr(2).chr(2).
     chr(68).chr(1).chr(0).chr(59);

And that made it very easy to let PHP return a transparent GIF-image. However, if I want to change the image, how can I do it? How do I transform any GIF that i create to this format?

Thanks.


Why don't you just have the image in a file and let PHP read and return it?

header('Content-type: image/gif');
readfile('file.gif');

If you really want to convert the image to PHP code, read it one byte at a time and create the PHP code like this:

<?php
$fh = fopen('file.gif', 'r');
$i = 0;
echo 'echo ';
while(!feof($fh)) {
  $byte = fread($fh, 1);
  $num = ord($byte);
  echo 'chr(', $num, ')';
  if(!feof($fh)) {
    echo '.'; //there's more bytes, echo dot                                    
    if(++$i % 6 == 0) {
      echo "\n     "; //do not be too wide                                      
    }
  }
}
echo ";\n";
fclose($fh);
?>


better than doing that, after you set a cookie or add a log entry, you can just redirect the call to an actual image (be it gif/jpeg/etc) and let the webserver worry about sending the content etc:

header("Location: http://webserver.com/images/beacon.gif");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜