显示 1x1 GIF 的最有效方式(跟踪像素、网络信标)
我正在构建一个基本的分析服务,理论上基于 Google Analytics 的工作原理,但我不是请求实际图像,而是将图像请求路由到接受数据然后输出图像的脚本。由于浏览器在每次加载时都会请求该图像,因此每一毫秒都很重要。
我正在寻找从 PHP 脚本输出 gif 文件的最有效方法。到目前为止,我已经建立了 3 个主要方法。
是否有更有效的方法让我从 PHP 脚本中输出 1x1 GIF 文件?如果不是,哪一个是最有效和可扩展的?
三种确定的方法
PHP 图像构建库
$im = imagecreatetruecolor(1, 1);
imagefilledrectangle($im, 0, 0, 0, 0, 0xFb6b6F);
header('Content-Type: image/gif');
imagegif($im);
imagedestroy($im);
file_get_contents
将图像从服务器上输出
$im = file_get_contents('raw.gif');
header('Content-Type: image/gif');
echo $im;
base64_decode
(我的直觉
header('Content-Type: image/gif');
echo base64_decode("R0lGODdhAQABAIAAAPxqbAAAACwAAAAAAQABAAACAkQBADs=");
是,base64 是最快的,但我不知道该函数的资源密集程度如何;并且 file_get_contents 的扩展性可能不太好,因为它添加了另一个文件系统操作。)
作为参考,我的 GIF使用在这里: https://i.sstatic.net/LQ1CR.gif
编辑
所以,我服务的原因该图像是我的分析库构建了一个查询字符串并将其附加到该图像请求。我不是解析日志,而是将请求路由到 PHP 脚本,该脚本处理数据并以图像进行响应,以便最终用户的浏览器不会挂起或抛出错误。我的问题是,如何在脚本的范围内最好地呈现该图像?
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');
imagegif($im);
imagedestroy($im);
file_get_contents
the image off of the server and output it
$im = file_get_contents('raw.gif');
header('Content-Type: image/gif');
echo $im;
base64_decode
the image
header('Content-Type: image/gif');
echo base64_decode("R0lGODdhAQABAIAAAPxqbAAAACwAAAAAAQABAAACAkQBADs=");
(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: https://i.sstatic.net/LQ1CR.gif
EDIT
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许
这会输出一个与 1x1 透明 gif 的二进制文件内容相同的二进制字符串。我声称这是高效的,因为它不执行任何缓慢的 IO(例如读取文件),也不调用任何函数。
如果您想制作您自己的上述十六进制字符串版本,也许这样您就可以更改颜色,您可以使用它来生成 echo 语句的 php 代码。
maybe
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.
对于来自磁盘的图像来说可能是最快的,但是(特别是如果您使用字节码缓存)对于预先知道的小图像,base64 方式将是我认为最快的。发送
Content-Length
也可能是一个好主意,对于小图像,浏览器在大多数情况下在接收字节后不会等待任何内容,因此虽然您的服务器会花费同样多的时间,但使用体验会看起来好多了。另一种方法是让 Apache/lighttpd/nginx 提供图像、记录访问并离线解析它。
Probably would be fastest for image from disk, but (especially if you're using bytecode caching) for a small images known in advance the base64 way will be the fastest I think. Sending
Content-Length
might be a good idea too, for the small image the browser would in most cases not wait for anything after receiving the bytes so while your server would take as much time, use experience will be sightly better.Another way would be to let Apache/lighttpd/nginx serve the image, log the access and the parse it offline.
对于 Laravel:
如果有人出于某种原因想要这样做。
或者,如果您不喜欢代码中的长十六进制字符串:
With Laravel:
If anyone wants that for some reason.
Alternatively, if you don't like long(ish) hex strings in your code:
为什么不直接重定向到静态图像,而不是动态生成/输出图像呢?
Instead of dynamically generating/outputting an image, why not just redirect to a static image?
我发现这对于 1x1 gif 来说是最有效的。而且这个是透明的。
I found this is the most effective for 1x1 gif. and also this one is transparent .