2016-09-17 50 views
1

我是Golang的新手,嘗試使用名爲iris的框架。Golang Iris Web - 服務1x1像素

我的問題是如何提供1x1 gif像素,不使用c.HTML上下文,但瀏覽器標題變爲1x1圖像gif。圖像將用於跟蹤。

任何幫助將深表謝意。

回答

1

注意:我對虹膜不是很熟悉,所以解決方案可能不是慣用的。

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

鏈接以便更好地理解: