【问题标题】:Golang Iris Web - Serve 1x1 pixelGolang Iris Web - 服务 1x1 像素
【发布时间】:2016-09-17 07:01:36
【问题描述】:

我是 Golang 新手,正在尝试使用名为 iris 的框架。

我的问题是如何提供 1x1 gif 像素,而不是使用 c.HTML 上下文,而是浏览器标题变成 1x1 image gif 的方式。该图像将用于跟踪。

任何帮助将不胜感激。

【问题讨论】:

    标签: image go go-iris


    【解决方案1】:

    注意:我对 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")
    }
    

    更好理解的链接:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多