渲染单个透明像素(PNG 或 GIF)的 PHP 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3203354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP script to render a single transparent pixel (PNG or GIF)
提问by Sjoerd
I have to create a PHP that will return an image stream of one transparent dot (PNG or GIF)
我必须创建一个 PHP,它将返回一个透明点(PNG 或 GIF)的图像流
Could you point me to an easy to use solution?
你能给我指出一个易于使用的解决方案吗?
回答by Sjoerd
Transparent 1x1 PNG:
透明 1x1 PNG:
header('Content-Type: image/png');
echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=');
Transparent 1x1 GIF:
透明 1x1 GIF:
header('Content-Type: image/gif');
echo base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==');
回答by Mikhail Bunkin
In PHP 5.4 and higher it is possible to use hex2bin, which is roughly two times faster than base64_decode (tested with the blank GIF file below). The code to output images would be:
在 PHP 5.4 及更高版本中,可以使用hex2bin,它大约比 base64_decode 快两倍(使用下面的空白 GIF 文件进行测试)。输出图像的代码是:
Transparent 1x1 PNG:
透明 1x1 PNG:
header('Content-Type: image/png');
die(hex2bin('89504e470d0a1a0a0000000d494844520000000100000001010300000025db56ca00000003504c5445000000a77a3dda0000000174524e530040e6d8660000000a4944415408d76360000000020001e221bc330000000049454e44ae426082'));
Transparent 1x1 GIF:
透明 1x1 GIF:
header('Content-Type: image/gif');
die(hex2bin('47494638396101000100900000ff000000000021f90405100000002c00000000010001000002020401003b'));
You can easily convert base64 encoded data into hexadecimal:
您可以轻松地将 base64 编码的数据转换为十六进制:
echo bin2hex(base64_decode($data));
Or a file:
或者一个文件:
echo bin2hex(base64_decode(file_get_contents($filename)));
However, using the native PHP escape method suggested by @Lukas Liesis is the fastest, about 12.5 times faster than base64_decode, according to my benchmark. And it would work with virtually any version of PHP. Here are the code snippets:
但是,根据我的基准测试,使用@Lukas Liesis 建议的原生 PHP 转义方法是最快的,大约比 base64_decode 快 12.5 倍。它几乎适用于任何版本的 PHP。以下是代码片段:
Transparent 1x1 PNG:
透明 1x1 PNG:
header('Content-Type: image/png');
die("\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x01\x00\x00\x00\x01\x01\x03\x00\x00\x00\x25\xdb\x56\xca\x00\x00\x00\x03\x50\x4c\x54\x45\x00\x00\x00\xa7\x7a\x3d\xda\x00\x00\x00\x01\x74\x52\x4e\x53\x00\x40\xe6\xd8\x66\x00\x00\x00\x0a\x49\x44\x41\x54\x08\xd7\x63\x60\x00\x00\x00\x02\x00\x01\xe2\x21\xbc\x33\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82");
Transparent 1x1 GIF:
透明 1x1 GIF:
header('Content-Type: image/gif');
die("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x90\x00\x00\xff\x00\x00\x00\x00\x00\x21\xf9\x04\x05\x10\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x04\x01\x00\x3b");
Creating such a string is easy with regular expressions (as it's done once, it doesn't have to work fast):
使用正则表达式创建这样的字符串很容易(因为它一次完成,它不必快速工作):
echo preg_replace('/../','\xecho preg_replace('/../','\xheader('Content-Type: image/gif');
// 1x1px white gif
die("\x47\x49\x46\x38\x37\x61\x1\x0\x1\x0\x80\x0\x0\xfc\x6a\x6c\x0\x0\x0\x2a\x0\x0\x0\x0\x1\x0\x1\x0\x0\x2\x2\x44\x1\x0\x3b");
',bin2hex(file_get_contents($filename)));
',bin2hex($data));
Or from a file:
或从文件:
hexdump -v -e '"\\x" 1/1 "%X"' image.gif; echo
回答by Lukas Liesis
To answer some people's questions:
回答一些人的问题:
You must echo the contents of an image. The Header only shows that are you are returning an image to the browser, instead of normal text. So if you only return a header, it's just a header - no data. However, print out data of an image and done, you just sent an image :)
您必须回显图像的内容。Header 仅显示您正在向浏览器返回图像,而不是普通文本。所以如果你只返回一个标题,它只是一个标题 - 没有数据。但是,打印出图像的数据并完成,您只是发送了一个图像:)
Reading a file is not good, it could work, but it's better to return raw data like shown in main answer by Sjoerd. This way, you save time and server load not having to process a read operation. Reading from disk is a slow task. Over time, reading from disk has increased in speed with SSD disks, but still, No I/O to disk is better.
读取文件不好,它可以工作,但最好返回原始数据,如 Sjoerd 的主要答案中所示。这样,您无需处理读取操作即可节省时间和服务器负载。从磁盘读取是一项缓慢的任务。随着时间的推移,使用 SSD 磁盘从磁盘读取的速度有所提高,但仍然没有 I/O 到磁盘更好。
I had this code even without base64 decode, but this image has white color:
即使没有base64解码,我也有这个代码,但是这个图像是白色的:
##代码##Maybe someone knows how to convert any file to such data string?
也许有人知道如何将任何文件转换为这样的数据字符串?
回答by Bob
In Linux you can convert it by typing in console:
在 Linux 中,您可以通过在控制台中键入来转换它:
##代码##
