Files
x_admin/server/util/img_util/img_util.go
2024-11-07 19:03:35 +08:00

39 lines
758 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package img_util
import (
"bytes"
"image"
"image/color"
"image/gif"
)
func EmptyGif() []byte {
img := image.NewRGBA(image.Rect(0, 0, 1, 1))
// 设置像素颜色为黑色
img.Set(0, 0, color.Black)
// 创建GIF动画
g := &gif.GIF{
Image: []*image.Paletted{imageToPaletted(img)},
Delay: []int{0}, // 延迟时间单位是10毫秒
}
var buffer bytes.Buffer
// 编码GIF到缓冲区
gif.EncodeAll(&buffer, g)
return buffer.Bytes()
}
// 将image.Image转换为*image.Paletted
func imageToPaletted(img image.Image) *image.Paletted {
b := img.Bounds()
pm := image.NewPaletted(b, color.Palette{color.Black})
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
pm.Set(x, y, img.At(x, y))
}
}
return pm
}