Parcourir la source

feat(plugin):Add function of resize thumbnail

EMqmyd il y a 4 ans
Parent
commit
c2cea2063f

+ 28 - 0
etc/functions/resize.json

@@ -0,0 +1,28 @@
+{
+	"about": {
+		"trial": false,
+		"author": {
+			"name": "EMQ",
+			"email": "contact@emqx.io",
+			"company": "EMQ Technologies Co., Ltd",
+			"website": "https://www.emqx.io"
+		},
+		"helpUrl": {
+			"en_US": "",
+			"zh_CN": ""
+		},
+		"description": {
+			"en_US": "",
+			"zh_CN": ""
+		}
+	},
+	"functions": [{
+		"name": "resize",
+		"example": "resize(image,width, height)",
+		"hint": {
+			"en_US": "Creates a scaled image with new dimensions (width, height) .If either width or height is set to 0, it will be set to an aspect ratio preserving value.",
+			"zh_CN": "创建具有新尺寸(宽度,高度)的缩放图像。如果width或height设置为0,则将其设置为长宽比保留值。"
+		}
+	}]
+}
+

+ 28 - 0
etc/functions/thumbnail.json

@@ -0,0 +1,28 @@
+{
+	"about": {
+		"trial": false,
+		"author": {
+			"name": "EMQ",
+			"email": "contact@emqx.io",
+			"company": "EMQ Technologies Co., Ltd",
+			"website": "https://www.emqx.io"
+		},
+		"helpUrl": {
+			"en_US": "",
+			"zh_CN": ""
+		},
+		"description": {
+			"en_US": "",
+			"zh_CN": ""
+		}
+	},
+	"functions": [{
+		"name": "thumbnail",
+		"example": "thumbnail(image,maxWidth, maxHeight)",
+		"hint": {
+			"en_US": "Downscales an image preserving its aspect ratio to the maximum dimensions (maxWidth, maxHeight).",
+			"zh_CN": "将保留宽高比的图像缩小到最大尺寸(maxWidth,maxHeight)。"
+		}
+	}]
+}
+

+ 1 - 1
plugins/funcMeta.go

@@ -30,7 +30,7 @@ type (
 )
 
 func isInternalFunc(fiName string) bool {
-	internal := []string{`accumulateWordCount.json`, `countPlusOne.json`, `echo.json`, `internal.json`, "windows.json"}
+	internal := []string{`accumulateWordCount.json`, `countPlusOne.json`, `echo.json`, `internal.json`, "windows.json", "thumbnail.json", "resize.json"}
 	for _, v := range internal {
 		if v == fiName {
 			return true

+ 62 - 0
plugins/functions/resize/resize.go

@@ -0,0 +1,62 @@
+package main
+
+import (
+	"bytes"
+	"fmt"
+	"github.com/emqx/kuiper/xstream/api"
+	"github.com/nfnt/resize"
+	"image"
+	"image/jpeg"
+	"image/png"
+)
+
+type imageResize struct {
+}
+
+func (f *imageResize) Validate(args []interface{}) error {
+	if len(args) != 3 {
+		return fmt.Errorf("The resize function supports 3 parameters, but got %d", len(args))
+	}
+	return nil
+}
+
+func (f *imageResize) Exec(args []interface{}, _ api.FunctionContext) (interface{}, bool) {
+	arg, ok := args[0].([]byte)
+	if !ok {
+		return fmt.Errorf("arg[0] is not a bytea, got %v", args[0]), false
+	}
+	width, ok := args[1].(int)
+	if !ok || 0 > width {
+		return fmt.Errorf("arg[1] is not a bigint, got %v", args[1]), false
+	}
+	height, ok := args[2].(int)
+	if !ok || 0 > height {
+		return fmt.Errorf("arg[2] is not a bigint, got %v", args[2]), false
+	}
+	img, format, err := image.Decode(bytes.NewReader(arg))
+	if nil != err {
+		return fmt.Errorf("image decode error:%v", err), false
+	}
+	img = resize.Resize(uint(width), uint(height), img, resize.Bilinear)
+
+	var b []byte
+	buf := bytes.NewBuffer(b)
+	switch format {
+	case "png":
+		err = png.Encode(buf, img)
+	case "jpeg":
+		err = jpeg.Encode(buf, img, nil)
+	default:
+		return fmt.Errorf("%s image type is not currently supported", format), false
+	}
+	if nil != err {
+		return fmt.Errorf("image encode error:%v", err), false
+	}
+	return buf.Bytes(), true
+}
+
+func (f *imageResize) IsAggregate() bool {
+	return false
+}
+
+var Resize imageResize

+ 62 - 0
plugins/functions/thumbnail/thumbnail.go

@@ -0,0 +1,62 @@
+package main
+
+import (
+	"bytes"
+	"fmt"
+	"github.com/emqx/kuiper/xstream/api"
+	"github.com/nfnt/resize"
+	"image"
+	"image/jpeg"
+	"image/png"
+)
+
+type thumbnail struct {
+}
+
+func (f *thumbnail) Validate(args []interface{}) error {
+	if len(args) != 3 {
+		return fmt.Errorf("The thumbnail function supports 3 parameters, but got %d", len(args))
+	}
+	return nil
+}
+
+func (f *thumbnail) Exec(args []interface{}, _ api.FunctionContext) (interface{}, bool) {
+	arg, ok := args[0].([]byte)
+	if !ok {
+		return fmt.Errorf("arg[0] is not a bytea, got %v", args[0]), false
+	}
+	maxWidth, ok := args[1].(int)
+	if !ok || 0 > maxWidth {
+		return fmt.Errorf("arg[1] is not a bigint, got %v", args[1]), false
+	}
+	maxHeight, ok := args[2].(int)
+	if !ok || 0 > maxHeight {
+		return fmt.Errorf("arg[2] is not a bigint, got %v", args[2]), false
+	}
+	img, format, err := image.Decode(bytes.NewReader(arg))
+	if nil != err {
+		return fmt.Errorf("image decode error:%v", err), false
+	}
+	img = resize.Thumbnail(uint(maxWidth), uint(maxHeight), img, resize.Bilinear)
+
+	var b []byte
+	buf := bytes.NewBuffer(b)
+	switch format {
+	case "png":
+		err = png.Encode(buf, img)
+	case "jpeg":
+		err = jpeg.Encode(buf, img, nil)
+	default:
+		return fmt.Errorf("%s image type is not currently supported", format), false
+	}
+	if nil != err {
+		return fmt.Errorf("image encode error:%v", err), false
+	}
+	return buf.Bytes(), true
+}
+
+func (f *thumbnail) IsAggregate() bool {
+	return false
+}
+
+var Thumbnail thumbnail