douting0585 2014-11-04 20:53
浏览 9
已采纳

为刚创建的Go服务图像

I'm creating an image dynamically when one doesn't exist. IE example_t500.jpg when requested would be created from example.jpg. The problem I'm having is displaying the created image when it's requested before a missing image is shown.

Code:

package main

import (
        "image/jpeg"
        "net/http"
        "log"
        "os"
        "strings"
        "fmt"
        "strconv"
        resizer "github.com/nfnt/resize"
    )

func WebHandler (w http.ResponseWriter, r *http.Request) {
    var Path = "../../static/img/photos/2014/11/4/test.jpg"
    ResizeImage(Path, 500)
    http.Handle("/", http.FileServer(http.Dir("example_t500.jpg")))
}

func ResizeImage (Path string, Width uint) {
    var ImageExtension = strings.Split(Path, ".jpg")
    var ImageNum       = strings.Split(ImageExtension[0], "/")
    var ImageName      = ImageNum[len(ImageNum)-1]
    fmt.Println(ImageName)
    file, err := os.Open(Path)
    if err != nil {
        log.Fatal(err)
    }

    img, err := jpeg.Decode(file)
    if err != nil {
        log.Fatal(err)
    }
    file.Close()

    m := resizer.Resize(Width, 0, img, resizer.Lanczos3)

    out, err := os.Create(ImageName + "_t" + strconv.Itoa(int(Width)) + ".jpg")
    if err != nil {
        log.Fatal(err)
    }
    defer out.Close()

    jpeg.Encode(out, m, nil)
}

func main() {
    http.HandleFunc("/", WebHandler)
    http.ListenAndServe(":8080", nil)
}

This is my first time trying to use Go and am having trouble rendering the image. Any help is appreciated.

  • 写回答

2条回答 默认 最新

  • duandi4238 2014-11-04 21:23
    关注

    There are a few things you need to do.

    First, you need to remove this line from your WebHandler function:

    http.Handle("/", http.FileServer(http.Dir("example_t500.jpg")))
    

    That's setting up the default serve mux to handle the root route - but you've already done that in your main function here:

    http.HandleFunc("/", WebHandler)
    

    So every time you hit the root route, you're effectively just telling the servemux to handle it again .. but differently.

    What you want to do.. is set the Content-Type header of the response.. then copy the contents of the file to the response stream. Something like this:

    func WebHandler (w http.ResponseWriter, r *http.Request) {
        var Path = "../../static/img/photos/2014/11/4/test.jpg"
        ResizeImage(Path, 500)
    
        img, err := os.Open("example_t500.jpg")
        if err != nil {
            log.Fatal(err) // perhaps handle this nicer
        }
        defer img.Close()
        w.Header().Set("Content-Type", "image/jpeg") // <-- set the content-type header
        io.Copy(w, img)
    }
    

    Thats the manual version. There's also http.ServeFile.

    EDIT:

    In response to your comment - if you don't want to write the file at all and just serve it dynamically, then all you need to do is pass the http.ResponseWriter to the Encode method. jpeg.Encode takes an io.Writer .. just as io.Copy does in my original example:

    // Pass in the ResponseWriter
    func ResizeImage (w io.Writer, Path string, Width uint) {
        var ImageExtension = strings.Split(Path, ".jpg")
        var ImageNum       = strings.Split(ImageExtension[0], "/")
        var ImageName      = ImageNum[len(ImageNum)-1]
        fmt.Println(ImageName)
        file, err := os.Open(Path)
        if err != nil {
            log.Fatal(err)
        }
    
        img, err := jpeg.Decode(file)
        if err != nil {
            log.Fatal(err)
        }
        file.Close()
    
        m := resizer.Resize(Width, 0, img, resizer.Lanczos3)
    
        // Don't write the file..
        /*out, err := os.Create(ImageName + "_t" + strconv.Itoa(int(Width)) + ".jpg")
        if err != nil {
            log.Fatal(err)
        }
        defer out.Close()*/
    
        jpeg.Encode(w, m, nil) // Write to the ResponseWriter
    }
    

    Accept an io.Writer in the method .. then encode it to that. You can then call your method like this:

    func WebHandler (w http.ResponseWriter, r *http.Request) {
        var Path = "../../static/img/photos/2014/11/4/test.jpg"
        ResizeImage(w, Path, 500) // pass the ResponseWriter in
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗