From 583bfe5f10d6d7d39e877d874db795cfc94f33a7 Mon Sep 17 00:00:00 2001 From: Marcin Kaciuba Date: Tue, 7 Nov 2017 23:38:49 +0100 Subject: [PATCH] added more image operations --- config/yaml.go | 8 +-- configuration/config-s3.yml | 6 +- configuration/config.yml | 6 +- object/file_object.go | 16 +++-- tests-int/config.yml | 3 +- transforms/transforms.go | 115 ++++++++++++++++++++++++------------ 6 files changed, 96 insertions(+), 58 deletions(-) diff --git a/config/yaml.go b/config/yaml.go index 589c4dd..b936de7 100644 --- a/config/yaml.go +++ b/config/yaml.go @@ -9,9 +9,7 @@ type PresetsYaml struct { Size []int `yaml:"size"` Mode string `yaml:"mode"` } `yaml:"thumbnail"` - Interlace struct { - Mode string `yaml:"mode"` - } `yaml:"interlace"` + Interlace bool `yaml:"interlace"` Crop struct { Size []int `yaml:"size"` Start []int `yaml:"start"` @@ -21,8 +19,8 @@ type PresetsYaml struct { Size []int `yaml:"size"` Mode string `yaml:"mode"` } `yaml:"entropy_crop"` - AutoRotate interface{} `yaml:"auto_rtate"` - Strip interface{} `yaml:"strip"` + AutoRotate bool `yaml:"auto_rtate"` + Strip bool `yaml:"strip"` } `yaml:"filters"` } diff --git a/configuration/config-s3.yml b/configuration/config-s3.yml index c4a3338..50a3e64 100644 --- a/configuration/config-s3.yml +++ b/configuration/config-s3.yml @@ -25,8 +25,7 @@ buckets: quality: 75 filters: thumbnail: { size: [100, 70], mode: outbound } - interlace: - mode: line + interlace: true blog_medium: quality: 75 filters: @@ -69,8 +68,7 @@ buckets: entropy_crop: { size: [600, 450], mode: outbound } auto_rotate: ~ strip: ~ - interlace: - mode: line + interlace: yes storages: basic: kind: "local" diff --git a/configuration/config.yml b/configuration/config.yml index 9b3bba4..6b3ee0e 100644 --- a/configuration/config.yml +++ b/configuration/config.yml @@ -25,8 +25,7 @@ buckets: quality: 75 filters: thumbnail: { size: [100, 70], mode: outbound } - interlace: - mode: line + interlace: true blog_medium: quality: 75 filters: @@ -69,8 +68,7 @@ buckets: entropy_crop: { size: [600, 450], mode: outbound } auto_rotate: ~ strip: ~ - interlace: - mode: line + interlace: true storages: basic: kind: "local-meta" diff --git a/object/file_object.go b/object/file_object.go index d429dec..b36a63a 100644 --- a/object/file_object.go +++ b/object/file_object.go @@ -16,18 +16,26 @@ func presetToTransform(preset config.PresetsYaml) transforms.Transforms { filters := preset.Filters if len(filters.Thumbnail.Size) > 0 { - trans.ResizeT(filters.Thumbnail.Size, filters.Thumbnail.Mode == "outbound") + trans.Resize(filters.Thumbnail.Size, filters.Thumbnail.Mode == "outbound") } if len(filters.SmartCrop.Size) > 0 { - trans.CropT(filters.SmartCrop.Size, filters.SmartCrop.Mode == "outbound") + trans.Crop(filters.SmartCrop.Size, filters.SmartCrop.Mode == "outbound") } if len(filters.Crop.Size) > 0 { - trans.CropT(filters.Crop.Size, filters.Crop.Mode == "outbound") + trans.Crop(filters.Crop.Size, filters.Crop.Mode == "outbound") } - trans.Quality = preset.Quality + trans.Quality(preset.Quality) + + if filters.Interlace == true { + trans.Interlace() + } + + if filters.Strip == true{ + trans.StripMetadata() + } return trans } diff --git a/tests-int/config.yml b/tests-int/config.yml index 6017cb6..d0ddc50 100644 --- a/tests-int/config.yml +++ b/tests-int/config.yml @@ -25,8 +25,7 @@ buckets: quality: 75 filters: thumbnail: { size: [100, 70], mode: outbound } - interlace: - mode: line + interlace: yes blog_medium: quality: 75 filters: diff --git a/transforms/transforms.go b/transforms/transforms.go index 6e64f64..6237606 100644 --- a/transforms/transforms.go +++ b/transforms/transforms.go @@ -1,56 +1,93 @@ package transforms -import "gopkg.in/h2non/bimg.v1" +import ( + "gopkg.in/h2non/bimg.v1" +) type Transforms struct { - Height int - Width int - AreaHeight int - AreaWidth int - Top int - Left int - Quality int - Compression int - Zoom int - Crop bool - Enlarge bool - Embed bool - Flip bool - Flop bool - Force bool - NoAutoRotate bool - NoProfile bool - Interlace bool - StripMetadata bool - Trim bool + height int + width int + areaHeight int + areaWidth int + top int + left int + quality int + compression int + zoom int + crop bool + enlarge bool + embed bool + flip bool + flop bool + force bool + noAutoRotate bool + noProfile bool + interlace bool + stripMetadata bool + trim bool + + sigma float64 + minAmpl float64 NotEmpty bool } -func (self *Transforms) ResizeT(size []int, enlarge bool) *Transforms { - self.Width = size[0] +func (t *Transforms) Resize(size []int, enlarge bool) *Transforms { + t.width = size[0] if len(size) == 2 { - self.Height = size[1] + t.height = size[1] } - self.Enlarge = enlarge - self.NotEmpty = true - return self + t.enlarge = enlarge + t.NotEmpty = true + return t +} + +func (t *Transforms) Crop(size []int, enlarge bool) *Transforms { + t.width = size[0] + t.height = size[1] + t.enlarge = enlarge + t.crop = true + t.NotEmpty = true + return t +} + +func (t *Transforms) Interlace() *Transforms{ + t.interlace = true + t.NotEmpty = true + return t +} + +func (t *Transforms) Quality(quality int) *Transforms { + t.quality = quality + t.NotEmpty = true + return t +} + +func (t *Transforms) StripMetadata() *Transforms { + t.stripMetadata = true + t.NotEmpty = true + return t } -func (self *Transforms) CropT(size []int, enlarge bool) *Transforms { - self.Width = size[0] - self.Height = size[1] - self.Enlarge = enlarge - self.Crop = true - self.NotEmpty = true - return self +func (t *Transforms) Blur(sigma, minAmpl float64) *Transforms { + t.NotEmpty = true + t.sigma = sigma + t.minAmpl = minAmpl + return t } -func (self *Transforms) BimgOptions() bimg.Options { +func (t *Transforms) BimgOptions() bimg.Options { return bimg.Options{ - Width: self.Width, - Height: self.Height, - Enlarge: self.Enlarge, - Crop: self.Crop, + Width: t.width, + Height: t.height, + Enlarge: t.enlarge, + Crop: t.crop, + Interlace: t.interlace, + Quality: t.quality, + StripMetadata: t.stripMetadata, + GaussianBlur: bimg.GaussianBlur{ + Sigma: t.sigma, + MinAmpl: t.minAmpl, + }, } }