From 1a62f6755db996363819004ee56ad3544f614d24 Mon Sep 17 00:00:00 2001 From: Andreas Neue Date: Sat, 22 May 2021 20:58:57 +0200 Subject: [PATCH] Added blur method --- main.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 03b40d0..f6df336 100644 --- a/main.go +++ b/main.go @@ -53,20 +53,23 @@ func transform(w http.ResponseWriter, r *http.Request) { buf := []byte{} errMsg := "" - errCode := 200 + errCode := 0 switch method { case "pass": break - + case "resize": buf, errMsg, errCode = doResize(resp.Body, args) if buf == nil { writeError(w, imgPath, method, errMsg, errCode) } - case "thumbnail": - break + case "blur": + buf, errMsg, errCode = doBlur(resp.Body, args) + if buf == nil { + writeError(w, imgPath, method, errMsg, errCode) + } default: writeError(w, imgPath, method, "unknown method", http.StatusBadRequest) @@ -133,6 +136,28 @@ func doResize(body io.ReadCloser, args string) ([]byte, string, int) { return buf, "", 0 } +func doBlur(body io.ReadCloser, args string) ([]byte, string, int) { + // Convert width and heigh to integers + sigma, err := strconv.ParseFloat(args, 64) + if err != nil { + return nil, "invalid sigma", http.StatusInternalServerError + } + img, err := vips.NewImageFromReader(body) + if err != nil { + return nil, fmt.Sprintf("%v", err), http.StatusInternalServerError + } + err = img.GaussianBlur(sigma) + if err != nil { + return nil, fmt.Sprintf("%v", err), http.StatusInternalServerError + } + ep := vips.NewDefaultExportParams() // default export params are sufficient + buf, _, _ := img.Export(ep) + if err != nil { + return nil, fmt.Sprintf("%v", err), http.StatusInternalServerError + } + return buf, "", 0 +} + func writeError(w http.ResponseWriter, imgPath, method, err string, status int) { w.WriteHeader(status) w.Write([]byte(fmt.Sprintf("Error [%s %s]: %s", method, imgPath, err)))