image - Golang Iris Web - 提供 1x1 像素

标签 image go go-iris

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

我的问题是如何提供 1x1 gif 像素,而不是使用 c.HTML 上下文,而是浏览器标题变成 1x1 图像 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")
}

更好理解的链接:

关于image - Golang Iris Web - 提供 1x1 像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39543845/

相关文章:

javascript - 如何在 WordPress 中使用 javascript/jquery 调整图像大小?

exception - 如何从外部包中的异步 panic 中恢复

oracle - 如何通过 xorm 从 Oracle DB 获取表值?

go - 获取登录用户信息以供显示

java - 如何访问不同文件夹中的图像?

php - Wordpress、PHP - 将图像插入主题主页

go - 申请表返回零

json - 如何解析json post请求

go - 使用 Iris-go 处理 body 支柱

Android 相机图像未上传到服务器。使用多部分数据 Http post