From bd717bccc2a723cbcf5f90351d3359e0dc4bd1da Mon Sep 17 00:00:00 2001 From: arnaucode Date: Tue, 20 Jun 2017 18:12:15 +0200 Subject: [PATCH] implemented resizing of new images before saving --- README.md | 10 ++++++++ handlers.go | 35 ++++++++++------------------ imageOperations.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 23 deletions(-) create mode 100644 imageOperations.go diff --git a/README.md b/README.md index e21caeb..8580d11 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,12 @@ # goImgCDN CDN for images, written in Go lang + +upload example (with curl): +``` +curl -F file=@./image1.png http://127.0.0.1:3050/image +``` + +to get image: +``` +http://127.0.0.1:3050/images/image1.png +``` diff --git a/handlers.go b/handlers.go index 993e47c..bca91b9 100644 --- a/handlers.go +++ b/handlers.go @@ -2,13 +2,9 @@ package main import ( "fmt" - "image" "image/jpeg" - "image/png" "io/ioutil" "net/http" - "os" - "strings" "github.com/gorilla/mux" ) @@ -20,34 +16,17 @@ func Index(w http.ResponseWriter, r *http.Request) { func ImageShow(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) imageName := vars["imageName"] - //fmt.Fprintln(w, "Image show:", imageName) - var imageExtension = strings.Split(imageName, ".")[1] - file, err := os.Open(imageName) + file, err := ioutil.ReadFile(imageName) if err != nil { - //log.Fatal(err) - //fmt.Fprintln(w, "la imatge no existeix al server") fmt.Fprintln(w, err) } - var img image.Image - switch imageExtension { - case "png": - img, err = png.Decode(file) - case "jpg": - img, err = jpeg.Decode(file) - case "jpeg": - img, err = jpeg.Decode(file) - default: - img = nil - } + img, err := dataToImage(file, imageName) if err != nil { - //log.Fatal(err) fmt.Fprintln(w, "la imatge no existeix al server") } else { - file.Close() - jpeg.Encode(w, img, nil) // Write to the ResponseWriter } } @@ -61,6 +40,16 @@ func NewImage(w http.ResponseWriter, r *http.Request) { if err != nil { fmt.Println(err) } + + img, err := dataToImage(data, handler.Filename) + if err != nil { + fmt.Fprintln(w, "error al processar la imatge") + } + img = Resize(img) + data, err = imageToData(img, handler.Filename) + if err != nil { + fmt.Fprintln(w, "error al processar la imatge") + } err = ioutil.WriteFile(handler.Filename, data, 0777) if err != nil { fmt.Println(err) diff --git a/imageOperations.go b/imageOperations.go new file mode 100644 index 0000000..5456ab3 --- /dev/null +++ b/imageOperations.go @@ -0,0 +1,57 @@ +package main + +import ( + "bytes" + "image" + "image/jpeg" + "image/png" + "strings" + + "github.com/nfnt/resize" +) + +func dataToImage(data []byte, imageName string) (image.Image, error) { + reader := bytes.NewReader(data) + var imageExtension = strings.Split(imageName, ".")[1] + var img image.Image + var err error + switch imageExtension { + case "png": + img, err = png.Decode(reader) + case "jpg": + img, err = jpeg.Decode(reader) + case "jpeg": + img, err = jpeg.Decode(reader) + default: + img = nil + } + if err != nil { + return img, err + } + return img, err +} + +func imageToData(img image.Image, imageName string) ([]byte, error) { + buf := new(bytes.Buffer) + var imageExtension = strings.Split(imageName, ".")[1] + var err error + switch imageExtension { + case "png": + err = png.Encode(buf, img) + case "jpg": + err = jpeg.Encode(buf, img, nil) + case "jpeg": + err = jpeg.Encode(buf, img, nil) + default: + img = nil + } + if err != nil { + return buf.Bytes(), err + } + return buf.Bytes(), err +} + +func Resize(img image.Image) image.Image { + r := resize.Resize(160, 0, img, resize.Lanczos3) + return r +}