Go图像上传

gev0vcfq  于 2023-09-28  发布在  Go
关注(0)|答案(2)|浏览(171)

我正在上传一个图像到我的服务器,执行以下操作:

  1. func (base *GuildController) GuildLogo(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  2. ...
  3. logo, _, err := req.FormFile("logo")
  4. defer logo.Close()
  5. logoGif, format, err := image.Decode(logo)
  6. if err != nil {
  7. base.Error = "Error while decoding your guild logo"
  8. return
  9. }
  10. logoImage, err := os.Create(pigo.Config.String("template")+"/public/guilds/"+ps.ByName("name")+".gif")
  11. if err != nil {
  12. base.Error = "Error while trying to open guild logo image"
  13. return
  14. }
  15. defer logoImage.Close()
  16. //resizedLogo := resize.Resize(64, 64, logoGif, resize.Lanczos3)
  17. err = gif.Encode(logoImage, logoGif, &gif.Options{
  18. 256,
  19. nil,
  20. nil,
  21. })
  22. if err != nil {
  23. base.Error = "Error while encoding your guild logo"
  24. return
  25. }
  26. ...
  27. }

所以一切都很好。但是gif失去了动画效果。
例如,这里有一个我想上传的gif

这是被救的那个

不知道我做错了什么

33qvvth1

33qvvth11#

正如注解中所暗示的,您只使用一个框架:
func Decode(r io.Reader) (image.Image, error) Decode从r读取GIF图像,并将第一个嵌入的图像作为image.Image返回。
但你需要
func DecodeAll(r io.Reader)(*GIF,error)DecodeAll从r读取GIF图像并返回连续帧和定时信息。

func EncodeAll(w io.Writer, g *GIF) error EncodeAll以GIF格式将g中的图像写入到w中,并具有给定的循环计数和帧间延迟。
查看this post了解详细信息。
下面是一个将图像速度降低到每帧0.5s的示例:

  1. package main
  2. import (
  3. "image/gif"
  4. "os"
  5. )
  6. func main() {
  7. logo, err := os.Open("yay.gif")
  8. if err != nil {
  9. panic(err)
  10. }
  11. defer logo.Close()
  12. inGif, err := gif.DecodeAll(logo)
  13. if err != nil {
  14. panic(err)
  15. }
  16. outGif, err := os.Create("done.gif")
  17. if err != nil {
  18. panic(err)
  19. }
  20. defer outGif.Close()
  21. for i := range inGif.Delay {
  22. inGif.Delay[i] = 50
  23. }
  24. if err := gif.EncodeAll(outGif, inGif); err != nil {
  25. panic(err)
  26. }
  27. }

结果如下:

旁注

即使在我的浏览器(Firefox)中,我看到了输出图像的动画,我可以看到GIMP中的帧,我不能在我的桌面查看器(gifview,comix)上看到它的动画。我不知道这是什么原因。

展开查看全部
xzlaal3s

xzlaal3s2#

使用gin框架上传图片。
首先创建项目目录并运行命令“go get -u github.com/gin-gonic/gin“
然后在根目录中创建assets/upload目录。
在main.go文件中添加以下代码。

  1. func main() {
  2. r := gin.Default()
  3. r.Static("/assets", "./assets")
  4. r.LoadHTMLGlob("templates/*")
  5. r.MaxMultipartMemory = 8 << 20 // 8 MiB
  6. r.GET("/", func(c *gin.Context) {
  7. c.HTML(http.StatusOK, "index.html", gin.H{})
  8. })
  9. r.POST("/upload", func(c *gin.Context) {
  10. // Get the file
  11. file, err := c.FormFile("image")
  12. if err != nil {
  13. c.HTML(http.StatusBadRequest, "index.html", gin.H{
  14. "error": "Failed to upload image",
  15. })
  16. return
  17. }
  18. filePath := "assets/uploads/" + file.Filename
  19. // Upload the file to specific folder.
  20. err = c.SaveUploadedFile(file, filePath)
  21. if err != nil {
  22. c.HTML(http.StatusBadRequest, "index.html", gin.H{
  23. "error": "Failed to upload image",
  24. })
  25. return
  26. }
  27. c.HTML(http.StatusOK, "index.html", gin.H{
  28. "image": "/" + filePath,
  29. })
  30. })
  31. r.Run() // listen and serve on 0.0.0.0:8080
  32. }

然后创建一个templates目录,并在该目录中创建一个index.html文件,然后粘贴以下代码...

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Golang File Upload</title>
  6. </head>
  7. <body>
  8. {{ if .image }}
  9. <img src="{{ .image }}" alt="">
  10. {{ end }}
  11. <form action="/upload" method="post" enctype="multipart/form-data">
  12. <input type="file" name="image" id="">
  13. <input type="submit" value="Submit">
  14. </form>
  15. {{ if .error }}
  16. <p>{{ .error }}</p>
  17. {{ end }}
  18. </body>
  19. </html>

最后使用“go run main.go”运行应用程序并进行测试。

展开查看全部

相关问题