Feed the image in Go just created
I am creating an image dynamically when it doesn't exist. IE example_t500.jpg upon request will be generated from example.jpg. The problem I'm running into is displaying the generated image when requested, before the 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 I am having problems displaying the image. Any help is appreciated.
source to share
There are several things you need to do.
First you need to remove this line from your function WebHandler
:
http.Handle("/", http.FileServer(http.Dir("example_t500.jpg")))
This is the default multiplayer setting for handling the root route - but you've already done that in your function main
here:
http.HandleFunc("/", WebHandler)
This way, every time you hit the root route, you are actually just telling servemux to process it again .. but in a different way.
What do you want to do .. the Content-Type
response header is set .. then copy the contents of the file into the response stream. Something like that:
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)
}
This is the manual version. There's also http.ServeFile .
EDIT:
In response to your comment - if you don't want to write a file at all and are just executing it dynamically, then all you have to do is pass it to the http.ResponseWriter
method Encode
. jpeg.Encode
takes io.Writer
.. just like io.Copy
it 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 io.Writer
in method, then code it. Then you can 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
}
source to share