Browse Source

Add fit image step

feature/update-route-registration
Roman Zipp 3 years ago
committed by Fabian Vowie
parent
commit
2803021fef
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 16
      README.md
  2. 15
      pipelines/executable_step.go
  3. 50
      pipelines/pipeline_test.go
  4. 8
      pipelines/step.go

16
README.md

@ -232,6 +232,22 @@ Convert the colorspace of an image into grayscale.
} }
``` ```
#### Fit
Scales down the image to fit the specified maximum width and height.
**Step definition**:
```json
{
"type": 4,
"options": {
"height": 300,
"width": 200
}
}
```
## Authors ## Authors
- [Fabian Vowie](https://gogs.informatik.hs-fulda.de/FabianVowie) - [Fabian Vowie](https://gogs.informatik.hs-fulda.de/FabianVowie)

15
pipelines/executable_step.go

@ -74,3 +74,18 @@ func (s GrayscaleImageStep) Execute(src image.Image) (image.Image, error) {
src = imaging.Grayscale(src) src = imaging.Grayscale(src)
return src, nil return src, nil
} }
// Fit image
type FitImageStep struct {
Step
Options struct {
Height int `json:"height"`
Width int `json:"width"`
} `json:"options"`
}
func (s FitImageStep) Execute(src image.Image) (image.Image, error) {
src = imaging.Fit(src, s.Options.Width, s.Options.Height, imaging.Lanczos)
return src, nil
}

50
pipelines/pipeline_test.go

@ -239,7 +239,7 @@ func TestExecuteSteps(t *testing.T) {
}) })
t.Run("Image flip step direction validation is successful", func(t *testing.T) { t.Run("Image flip step direction validation is successful", func(t *testing.T) {
const Bucket string = "pipeline_test_06"
const Bucket string = "pipeline_test_07"
const Payload string = `{ const Payload string = `{
"name": "example pipeline", "name": "example pipeline",
"type": 1, "type": 1,
@ -316,6 +316,54 @@ func TestExecuteSteps(t *testing.T) {
os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
os.Remove(storageProvider.GetPath(Bucket, dest)) os.Remove(storageProvider.GetPath(Bucket, dest))
}) })
t.Run("Image fit step is successful", func(t *testing.T) {
const Bucket string = "pipeline_test_08"
const Payload string = `{
"name": "example pipeline",
"type": 1,
"removeMetadata": false,
"steps": [
{
"name": "fit",
"type": 4,
"options": {
"width": 300,
"height": 200
}
}
]
}`
wd, _ := os.Getwd()
pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
storageProvider := storage.GetFileSystemStorageProvider("test", "..")
// copy test file to storage bucket
_, 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"))
// run pipeline steps
dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
assert.Nil(t, err)
assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
// read image config
file, err := storageProvider.OpenFile(Bucket, dest)
assert.Nil(t, err)
imgConf, _, err := image.DecodeConfig(file)
assert.Nil(t, err)
assert.Equal(t, 200, imgConf.Width)
assert.Equal(t, 200, imgConf.Height)
// clean up
os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
os.Remove(storageProvider.GetPath(Bucket, dest))
})
} }
// output options // output options

8
pipelines/step.go

@ -12,6 +12,7 @@ const (
TypeRotateImageStep TypeRotateImageStep
TypeFlipImageStep TypeFlipImageStep
TypeGrayscaleImageStep TypeGrayscaleImageStep
TypeFitImageStep
) )
type Step struct { type Step struct {
@ -44,6 +45,13 @@ func (s Step) GetExecutable() (IExecutableStep, error) {
} }
return step, nil return step, nil
case TypeFitImageStep:
step := FitImageStep{}
if err := json.Unmarshal(s.Options, &step.Options); err != nil {
return nil, err
}
return step, nil
case TypeGrayscaleImageStep: case TypeGrayscaleImageStep:
return GrayscaleImageStep{}, nil return GrayscaleImageStep{}, nil
} }

Loading…
Cancel
Save