douliang1891 2016-09-17 07:01
浏览 49
已采纳

Golang Iris Web-服务1x1像素

I'm new to Golang and trying to use a framework called iris.

My problem is how to serve 1x1 gif pixel, not using c.HTML context, but the way the browser title becomes 1x1 image gif. The image will be used for tracking.

Any help will be deeply appreciated.

  • 写回答

1条回答 默认 最新

  • douye2020 2016-09-17 08:00
    关注

    Attention: I'm not really familiar with iris so the solution maybe not idiomatic.

    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")
    }
    

    Links for better understanding:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端