Skip to content

Commit

Permalink
added watermark transform
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed Nov 8, 2017
1 parent 053f2e3 commit 8b5ee94
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 163 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/mort.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
listenAddr := flag.String("listen", ":8080", "Listen addr")
flag.Parse()
fmt.Println(*configPath, *listenAddr)
logger, _ := zap.NewDevelopment()
logger, _ := zap.NewProduction()
zap.ReplaceGlobals(logger)
log.RegisterLogger(logger.Sugar())
rp := mort.NewRequestProcessor(5)
Expand Down
8 changes: 7 additions & 1 deletion config/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "regexp"

type PresetsYaml struct {
Quality int `yaml:"quality"`
Format string `yaml:"format"`
Filters struct {
Thumbnail struct {
Size []int `yaml:"size"`
Expand All @@ -21,11 +22,16 @@ type PresetsYaml struct {
} `yaml:"entropy_crop"`
AutoRotate bool `yaml:"auto_rtate"`
Strip bool `yaml:"strip"`
Format string `yaml:"format"`
Blur struct {
Sigma float64 `yaml:"sigma"`
MinAmpl float64 `yaml:"minAmpl"`
} `yaml:"blur"`
Watermark struct {
Image string `yaml:"image"`
Position string `yaml:"position"`
Opacity float32 `yaml:"opacity"`

}
} `yaml:"filters"`
}

Expand Down
126 changes: 18 additions & 108 deletions configuration/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,129 +10,42 @@ headers:
"cache-control": "max-age=10, public"

buckets:
liip:
keys:
- accessKey: "acc"
secretAccessKey: "sec"
transform:
path: "\\/+(?:cache|resolve)\\/([a-z0-9_]+)\\/+[a-z]+\\/[a-zA-Z0-9]+\\/+(.*)"
kind: "presets"
order:
presetName: 0
parent: 1
presets:
blog_small:
quality: 75
filters:
thumbnail: { size: [100, 70], mode: outbound }
interlace: true
blog_medium:
quality: 75
filters:
thumbnail: { size: [903, 600], mode: outbound }
crop: { start: [0, 0], size: [900, 320] }

blog_home:
quality: 75
filters:
entropy_crop: { size: [756, 396], mode: outbound }
recent_gallery:
quality: 75
filters:
entropy_crop: { size: [85, 85], mode: outbound }

promoted:
quality: 75
filters:
entropy_crop: { size: [896, 465], mode: outbound }

relatedposts:
quality: 75
filters:
entropy_crop: { size: [504, 369], mode: outbound }

blog_big:
quality: 75
filters:
auto_rotate: ~
strip: ~
gallery_thumb:
quality: 75
filters:
thumbnail: { size: [200], mode: outbound }
auto_rotate: ~
strip: ~
photocategory_img:
quality: 75
filters:
entropy_crop: { size: [600, 450], mode: outbound }
auto_rotate: ~
strip: ~
interlace: true
storages:
basic:
kind: "local-meta"
rootPath: "/Users/aldor/workspace/mkaciubacom/web"
transform:
kind: "local-meta"
rootPath: "/Users/aldor/workspace/mkaciubacom/web"
media:
keys:
- accessKey: "acc"
secretAccessKey: "sec"
transform:
path: "\\/\\w+\\/\\w+\\/\\w+\\/[a-z]+_([a-z0-9-]+)_([a-z0-9_]+).*"
path: "\\/([a-z0-9_]+)\\/(.*)"
kind: "presets"
order:
parent: 0
presetName: 1
parentStorage: "api"
parent: 1
presetName: 0
parentBucket: "media"
presets:
default_small:
quality: 95
filters:
thumbnail: {size: [150]}
default_medium:
quality: 95
filters:
thumbnail: {size: [450]}
default_big:
quality: 95
filters:
thumbnail: {size: [700]}
blog_small:
small:
quality: 75
filters:
thumbnail: {size: [150]}
blog_big:
blur:
quality: 80
filters:
thumbnail: {size: [700]}
blog_big1000:
blur:
sigma: 5.0
webp:
quality: 100
format: webp
filters:
thumbnail: {size: [1000]}
blog_big1300:
quality: 100
filters:
thumbnail: {size: [1300]}
gallery_small:
quality: 75
filters:
thumbnail: {size: [150]}
gallery_big:
quality: 80
filters:
thumbnail: {size: [700]}
gallery_big1000:
quality: 100
filters:
thumbnail: {size: [1000]}
gallery_big1300:
watermark:
quality: 100
filters:
thumbnail: {size: [1300]}
watermark:
image: "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Imgur_logo.svg/150px-Imgur_logo.svg.png"
position: "center-center"
opacity: 0.5

gallery_big300:
quality: 100
filters:
Expand All @@ -142,15 +55,12 @@ buckets:
filters:
thumbnail: {size: [200]}
storages:
api:
basic:
kind: "http"
url: "https://mkaciuba.pl/download<item>"
url: "https://i.imgur.com/<item>"
headers:
"x-security-key": "sec"
"x--key": "sec"
transform:
kind: "local-meta"
rootPath: "/Users/aldor/workspace/mkaciubacom/web"
pathPrefix: "transforms"
basic:
kind: "local-meta"
rootPath: "/Users/aldor/workspace/mkaciubacom/web"
33 changes: 28 additions & 5 deletions engine/image_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package engine
import (
"time"
"strconv"
"encoding/binary"
"net/http"

"gopkg.in/h2non/bimg.v1"
"github.com/spaolacci/murmur3"
Expand All @@ -27,23 +29,44 @@ func (self *ImageEngine) Process(obj *object.FileObject, trans []transforms.Tran
return response.NewError(500, err), err
}

var transHash uint64
for _, tran := range trans {
image := bimg.NewImage(buf)
buf, err = image.Process(tran.BimgOptions())
meta, err := bimg.Metadata(buf)
if err != nil {
return response.NewError(500, err), err
}

opts, err := tran.BimgOptions(transforms.NewImageInfo(meta, bimg.DetermineImageTypeName(buf)))
if err != nil {
return response.NewError(500, err), err
}

buf, err = image.Process(opts)
if err != nil {
return response.NewError(500, err), err
}
transHash = transHash + tran.Hash().Sum64()
}

transHashB := make([]byte, 8)
binary.LittleEndian.PutUint64(transHashB, transHash)

hash := murmur3.New64()
hash.Write([]byte(obj.Key))
//hash.Write([]byte(len(trans)))
hash.Write(transHashB)

res := response.NewBuf(200, buf)
res.SetContentType("image/" + bimg.DetermineImageTypeName(buf))
res.Set("cache-control", "max-age=6000, public")
res.Set("Last-Modified", time.Now().Format(time.RFC1123))
res.Set("ETag", strconv.FormatInt(int64(hash.Sum64()), 16))
//res.Set("cache-control", "max-age=6000, public")
res.Set("Last-Modified", time.Now().Format(http.TimeFormat))
h := int64(hash.Sum64())

if h < 0 {
h = h * -1
}

res.Set("ETag", strconv.FormatInt(h, 16))
meta, err := bimg.Metadata(buf)
if err == nil {
res.Set("x-amz-public-width", strconv.Itoa(meta.Size.Width))
Expand Down
2 changes: 1 addition & 1 deletion log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package log

import "go.uber.org/zap"

var logger *zap.SugaredLogger
var logger *zap.SugaredLogger = zap.NewNop().Sugar()

func RegisterLogger(l *zap.SugaredLogger) {
logger = l
Expand Down
55 changes: 44 additions & 11 deletions object/file_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,69 @@ import (
)


func presetToTransform(preset config.PresetsYaml) transforms.Transforms {
func presetToTransform(preset config.PresetsYaml) (transforms.Transforms, error) {
var trans transforms.Transforms
filters := preset.Filters

if len(filters.Thumbnail.Size) > 0 {
trans.Resize(filters.Thumbnail.Size, filters.Thumbnail.Mode == "outbound")
err := trans.Resize(filters.Thumbnail.Size, filters.Thumbnail.Mode == "outbound")
if err != nil {
return trans, err
}
}

if len(filters.SmartCrop.Size) > 0 {
trans.Crop(filters.SmartCrop.Size, filters.SmartCrop.Mode == "outbound")
err := trans.Crop(filters.SmartCrop.Size, filters.SmartCrop.Mode == "outbound")
if err != nil {
return trans, err
}
}

if len(filters.Crop.Size) > 0 {
trans.Crop(filters.Crop.Size, filters.Crop.Mode == "outbound")
err := trans.Crop(filters.Crop.Size, filters.Crop.Mode == "outbound")
if err != nil {
return trans, err
}
}

trans.Quality(preset.Quality)

if filters.Interlace == true {
trans.Interlace()
err := trans.Interlace()
if err != nil {
return trans, err
}
}

if filters.Strip == true{
trans.StripMetadata()
err := trans.StripMetadata()
if err != nil {
return trans, err
}
}

if filters.Format != "" {
trans.Format(filters.Format)
if preset.Format != "" {
err := trans.Format(preset.Format)
if err != nil {
return trans, err
}
}

if filters.Blur.Sigma != 0 {
trans.Blur(filters.Blur.Sigma, filters.Blur.MinAmpl)
err := trans.Blur(filters.Blur.Sigma, filters.Blur.MinAmpl)
if err != nil {
return trans, err
}
}

return trans
if filters.Watermark.Image != "" {
err := trans.Watermark(filters.Watermark.Image, filters.Watermark.Position, filters.Watermark.Opacity)
if err != nil {
return trans, err
}
}

return trans, nil
}

type FileObject struct {
Expand Down Expand Up @@ -107,7 +135,12 @@ func (self *FileObject) decodeKey(bucket config.Bucket, mortConfig *config.Confi
presetName := string(matches[trans.Order.PresetName+1])
parent := "/" + string(matches[trans.Order.Parent+1])

self.Transforms = presetToTransform(bucket.Transform.Presets[presetName])
var err error
self.Transforms, err = presetToTransform(bucket.Transform.Presets[presetName])
if err != nil {
return err
}

if bucket.Transform.ParentBucket != "" {
parent = "/" + path.Join(bucket.Transform.ParentBucket, parent)
}
Expand Down
4 changes: 0 additions & 4 deletions processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ func BenchmarkNewRequestProcessor(b *testing.B) {
{"Process large image, small result", "http://mort/local/large.jpeg-small", "./tests/benchmark/local/large.jpeg", "./tests/benchmark/small.yml"},
}

logger, _ := zap.NewDevelopment()
zap.ReplaceGlobals(logger)
log.RegisterLogger(logger.Sugar())

for _, bm := range benchmarks {
data, err := ioutil.ReadFile(bm.filePath)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func prepareResponse(obj *object.FileObject, stream io.ReadCloser, item stow.Ite
}

res.Set("ETag", etag)
res.Set("Last-Modified", lastMod.String())
res.Set("Last-Modified", lastMod.Format(http.TimeFormat))

if contentType, ok := metadata["Content-Type"]; ok {
res.SetContentType(contentType.(string))
Expand Down
Loading

0 comments on commit 8b5ee94

Please sign in to comment.