PHP script to render a single transparent pixel (PNG or GIF)

后端 未结 5 1979
甜味超标
甜味超标 2020-12-22 22:56

I have to create a PHP that will return an image stream of one transparent dot (PNG or GIF)

Could you point me to an easy to use solution?

5条回答
  •  情歌与酒
    2020-12-22 23:46

    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 :)

    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.

    I had this code even without base64 decode, but this image has white color:

    header('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");
    

    Maybe someone knows how to convert any file to such data string?

提交回复
热议问题