Added blur method
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Andreas Neue 2021-05-22 20:58:57 +02:00
parent 14ca478ecc
commit 1a62f6755d
1 changed files with 29 additions and 4 deletions

33
main.go
View File

@ -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)))