Skip to content

Instantly share code, notes, and snippets.

@jan4984
Created March 6, 2017 10:11
Show Gist options
  • Save jan4984/c8148575e057319136cbf887d701e8d1 to your computer and use it in GitHub Desktop.
Save jan4984/c8148575e057319136cbf887d701e8d1 to your computer and use it in GitHub Desktop.
a server converting raw RGBA to bmp
package main
import(
"golang.org/x/image/bmp"
"image"
"io/ioutil"
"fmt"
"strconv"
"net/http"
"log"
//"mime/multipart"
)
func main(){
http.HandleFunc("/up",func(w http.ResponseWriter, r*http.Request){
if r.Method == "GET"{
w.Header().Add("content-type","text/html")
fmt.Fprint(w, `<form enctype="multipart/form-data" action="/up" method="post">
<input type="file" name="uploadfile"/>
<input type="text" name="width"/>
<input type="submit" value="submit"/>
</form>
`)
return
}
err := r.ParseMultipartForm(25 * 1024 * 1024)//25M
if err != nil{
http.Error(w, err.Error(), 500);
return
}
defer r.Body.Close()
file, _, err := r.FormFile("uploadfile")
if err != nil {
http.Error(w, err.Error(), 500);
return
}
defer file.Close()
widthStr := r.FormValue("width")
if widthStr == ""{
widthStr = "1080"
}
width64,err := strconv.ParseInt(widthStr, 10, 32)
if err!=nil{
http.Error(w, err.Error(), 500);
return
}
width := int(width64)
raw, err := ioutil.ReadAll(file)
if err != nil{
http.Error(w, err.Error(), 500);
return
}
rect := image.Rect(0,0,int(width),len(raw)/4/int(width))
rgba := image.RGBA{
raw,
4*int(width),
rect,
}
img := rgba.SubImage(rect)
w.Header().Add("content-type", "image/bmp")
err = bmp.Encode(w, img);
if err!=nil{
http.Error(w, err.Error(), 500);
}
})
log.Fatal(http.ListenAndServe(":3333", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment