我是 Golang 的新手,正在尝试使用一个名为iris的框架。
我的问题是如何提供 1x1 gif 像素,而不是使用 c.HTML 上下文,而是浏览器标题变成1x1 image gif的方式。该图像将用于跟踪。
任何帮助将不胜感激。
注意:我对 iris 不是很熟悉,所以解决方案可能不是惯用的。
package main
import (
"github.com/kataras/iris"
"image"
"image/color"
"image/gif"
)
func main() {
iris.Get("/", func(ctx *iris.Context) {
img := image.NewRGBA(image.Rect(0, 0, 1, 1)) //We create a new image of size 1x1 pixels
img.Set(0, 0, color.RGBA{255, 0, 0, 255}) //set the first and only pixel to some color, in this case red
err := gif.Encode(ctx.Response.BodyWriter(), img, nil) //encode the rgba image to gif, using gif.encode and write it to the response
if err != nil {
panic(err) //if we encounter some problems panic
}
ctx.SetContentType("image/gif") //set the content type so the browser can identify that our response is actually an gif image
})
iris.Listen(":8080")
}
更好理解的链接: