From 5a47f730205e53898dbe7335f653ca7c5c7b2361 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Tue, 20 Jun 2017 20:57:31 +0200 Subject: [PATCH] convert new images to PNG --- handlers.go | 9 ++++++--- imageOperations.go | 7 +++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/handlers.go b/handlers.go index bca91b9..8bf1173 100644 --- a/handlers.go +++ b/handlers.go @@ -5,6 +5,7 @@ import ( "image/jpeg" "io/ioutil" "net/http" + "strings" "github.com/gorilla/mux" ) @@ -36,6 +37,8 @@ func NewImage(w http.ResponseWriter, r *http.Request) { if err != nil { fmt.Println(err) } + imageName := strings.Split(handler.Filename, ".")[0] + fileName := imageName + ".png" data, err := ioutil.ReadAll(file) if err != nil { fmt.Println(err) @@ -46,14 +49,14 @@ func NewImage(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "error al processar la imatge") } img = Resize(img) - data, err = imageToData(img, handler.Filename) + data, err = imageToPNG(img) if err != nil { fmt.Fprintln(w, "error al processar la imatge") } - err = ioutil.WriteFile(handler.Filename, data, 0777) + err = ioutil.WriteFile(fileName, data, 0777) if err != nil { fmt.Println(err) } - fmt.Fprintln(w, "url:", handler.Filename) + fmt.Fprintln(w, "url:", fileName) } diff --git a/imageOperations.go b/imageOperations.go index 5456ab3..c409ad0 100644 --- a/imageOperations.go +++ b/imageOperations.go @@ -51,6 +51,13 @@ func imageToData(img image.Image, imageName string) ([]byte, error) { return buf.Bytes(), err } +func imageToPNG(img image.Image) ([]byte, error) { + buf := new(bytes.Buffer) + var err error + err = png.Encode(buf, img) + return buf.Bytes(), err +} + func Resize(img image.Image) image.Image { r := resize.Resize(160, 0, img, resize.Lanczos3) return r