by
0 6
0
5
5,576
29
Top 1% !
Popular
Famous
Pearl of Wisdom
Specified
Popularity: 1469th place

Published on:

No tags for this snippet yet.
LanguageGo
SourceGitHub

Golang HTTP Handler to Upload Image => Resize => Convert to JPEG => Save to Disk.

Golang HTTP Handler to Upload Image => Resize => Convert to JPEG => Save to Disk. : 
gistfile1.go
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/663162/Golang-HTTP-Handler-to-Upload-Image----R?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
func UploadHandler(w http.ResponseWriter, r *http.Request) { file, _, err := r.FormFile("file") if err != nil { log.Println(err) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return } img, _, err := image.Decode(file) if err != nil { log.Println(err) http.Error(w, http.StatusText(http.StatusUnsupportedMediaType), http.StatusUnsupportedMediaType) return } m := resize.Resize(1000, 0, img, resize.Lanczos3) out, err := os.Create("test_resized.jpg") if err != nil { log.Println(err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } defer out.Close() // Encode into jpeg http://blog.golang.org/go-image-package err = jpeg.Encode(out, m, nil) if err != nil { log.Println(err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus