Browse Source

Add rotating image step

feature/add-image-processing
Roman Zipp 3 years ago
parent
commit
c30062b2ea
  1. 7
      config/example.json
  2. 14
      pipelines/executable_step.go
  3. 45
      pipelines/pipeline_test.go
  4. 9
      pipelines/step.go

7
config/example.json

@ -12,6 +12,13 @@
"height": 720, "height": 720,
"upscale": false "upscale": false
} }
},
{
"name": "rotate image",
"type": 0,
"options": {
"angle": 90.0
}
} }
], ],
"output": { "output": {

14
pipelines/executable_step.go

@ -24,3 +24,17 @@ func (s ResizeImageStep) Execute(src image.Image) (image.Image, error) {
src = imaging.Resize(src, s.Options.Width, s.Options.Height, imaging.Linear) src = imaging.Resize(src, s.Options.Width, s.Options.Height, imaging.Linear)
return src, nil return src, nil
} }
// Rotate image
type RotateImageStep struct {
Step
Options struct {
Angle float64 `json:"angle"`
} `json:"options"`
}
func (s RotateImageStep) Execute(src image.Image) (image.Image, error) {
src = imaging.Rotate(src, s.Options.Angle, image.Black)
return src, nil
}

45
pipelines/pipeline_test.go

@ -9,6 +9,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// pipeline deserialization
func TestImagePipelineDeserialization(t *testing.T) { func TestImagePipelineDeserialization(t *testing.T) {
const Payload string = `{ const Payload string = `{
"name": "example pipeline", "name": "example pipeline",
@ -61,6 +63,8 @@ func TestVideoPipelineDeserialization(t *testing.T) {
}) })
} }
// image pipeline steps
func TestResizeImage(t *testing.T) { func TestResizeImage(t *testing.T) {
const Payload string = `{ const Payload string = `{
"name": "example pipeline", "name": "example pipeline",
@ -100,6 +104,45 @@ func TestResizeImage(t *testing.T) {
}) })
} }
func TestRotateImage(t *testing.T) {
const Payload string = `{
"name": "example pipeline",
"type": 1,
"removeMetadata": false,
"steps": [
{
"name": "rotate image",
"type": 1,
"options": {
"angle": 90.0
}
}
]
}`
const Bucket string = "pipeline_test_03"
t.Run("Image rotating is successful", func(t *testing.T) {
wd, _ := os.Getwd()
pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
storageProvider := storage.GetFileSystemStorageProvider("test", "..")
_, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
assert.Nil(t, err, "Test file should be readable")
assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
assert.Nil(t, err)
assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
// clean up
os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
os.Remove(storageProvider.GetPath(Bucket, dest))
})
}
// output options
func TestEncodeImageWithJpegQuality(t *testing.T) { func TestEncodeImageWithJpegQuality(t *testing.T) {
const Payload string = `{ const Payload string = `{
"name": "example pipeline", "name": "example pipeline",
@ -122,7 +165,7 @@ func TestEncodeImageWithJpegQuality(t *testing.T) {
}` }`
const Bucket string = "pipeline_test_02" const Bucket string = "pipeline_test_02"
t.Run("Image resizing is successful", func(t *testing.T) {
t.Run("Image encoding with jpeg quality is successful", func(t *testing.T) {
wd, _ := os.Getwd() wd, _ := os.Getwd()
pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0] pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]

9
pipelines/step.go

@ -9,6 +9,7 @@ type StepType int
const ( const (
TypeResizeImageStep StepType = iota TypeResizeImageStep StepType = iota
TypeRotateImageStep
) )
type Step struct { type Step struct {
@ -19,12 +20,20 @@ type Step struct {
func (s Step) GetExecutable() (IExecutableStep, error) { func (s Step) GetExecutable() (IExecutableStep, error) {
switch s.GetType() { switch s.GetType() {
case TypeResizeImageStep: case TypeResizeImageStep:
step := ResizeImageStep{} step := ResizeImageStep{}
if err := json.Unmarshal(s.Options, &step.Options); err != nil { if err := json.Unmarshal(s.Options, &step.Options); err != nil {
return nil, err return nil, err
} }
return step, nil return step, nil
case TypeRotateImageStep:
step := RotateImageStep{}
if err := json.Unmarshal(s.Options, &step.Options); err != nil {
return nil, err
}
return step, nil
} }
return nil, errors.New("invalid type") return nil, errors.New("invalid type")

Loading…
Cancel
Save