dongren1353 2017-05-01 16:59
浏览 29
已采纳

在Go中指定了多个URL参数的过程图像[关闭]

I would probably prefer to use pressly/chi, but I guess it makes no difference. I imagine given an input URL like this example.com/Jd8saD.jpg?resize=420x320&fit=crop&rotate=90, then it would go to the following GET function because of r.Get("/:image", ImageGET):

function ImageGET(w http.ResponseWriter, r *http.Request) {
  if r.URL.Query().Get("resize") != "" {
    // do something
  }
  if r.URL.Query().Get("crop") != "" {
    // do something
  }
  if r.URL.Query().Get("watermark") != "" {
    // do something
  }
  etc
}

Now, my question is, how should I design whatever function does the image processing so that it will process everything properly and efficiently? I don't expect you to write code that will handle the resizing, but how would these functions look like? Perhaps:

function Resize(size string) (imgfile?, error) {
  // the resize stuff
}

What would that returned imgfile even be? A struct containing some relevant img info?

  • 写回答

2条回答 默认 最新

  • douxiong4250 2017-05-01 17:18
    关注

    Likely,

    imgfile

    will satisfy the image.Image interface and not the data saved on disk (ie. the actual jpg file)

    Image is a finite rectangular grid of color.Color values taken from a color model.

    Lots of 3rd party golang image libraries use image.Image to manipulate images.


    I would use a standard image.Image interface retrieved (read to memory) by a filename in the imageGET function and modified according to the queries. You can see also the jpeg golang library from the standard lib.

    function ImageGET(w http.ResponseWriter, r *http.Request) {
      // GetJPEGImageFromFileSystem must decode os.File content and 
      // return a golang image.Image interface
      img, _ := GetJPEGImageFromFileSystem(r.URL.Query().Get("filename"))
      if r.URL.Query().Get("resize") != "" {
          // If resizing, write over the image.Image in Memory, 
          // but not the image File on disk
          img, _ = Resize(img, r.URL.Query().GET("resize"))
      }
      // etc...
    }
    
    function Resize(img image.Image, size string) (image.Image, error) {
      // the resize stuff -- or, alternatively just call a dependency in the original handler to resize
      return resizedImage, nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。