Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-08-26 20:12:41 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-08-28 16:59:54 +0300
commit823f53c861bb49aecc6104e0add39fc3b0729025 (patch)
tree64a55d1c41de09b67305ad69a3600f3091d4f1fc /tpl
parentf9978ed16476ca6d233a89669c62c798cdf9db9d (diff)
Add a set of image filters
With this you can do variants of this: ``` {{ $img := resources.Get "images/misc/3-jenny.jpg" }} {{ $img := $img.Resize "300x" }} {{ $g1 := $img.Filter images.Grayscale }} {{ $g2 := $img | images.Filter (images.Saturate 30) (images.GaussianBlur 3) }} ``` Fixes #6255
Diffstat (limited to 'tpl')
-rw-r--r--tpl/images/images.go31
1 files changed, 27 insertions, 4 deletions
diff --git a/tpl/images/images.go b/tpl/images/images.go
index 4cb809df7..b64456876 100644
--- a/tpl/images/images.go
+++ b/tpl/images/images.go
@@ -1,4 +1,4 @@
-// Copyright 2017 The Hugo Authors. All rights reserved.
+// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -15,10 +15,16 @@
package images
import (
- "errors"
"image"
"sync"
+ "github.com/disintegration/gift"
+
+ "github.com/pkg/errors"
+
+ "github.com/gohugoio/hugo/resources/images"
+ "github.com/gohugoio/hugo/resources/resource"
+
// Importing image codecs for image.DecodeConfig
_ "image/gif"
_ "image/jpeg"
@@ -34,13 +40,15 @@ import (
// New returns a new instance of the images-namespaced template functions.
func New(deps *deps.Deps) *Namespace {
return &Namespace{
- cache: map[string]image.Config{},
- deps: deps,
+ Filters: &images.Filters{},
+ cache: map[string]image.Config{},
+ deps: deps,
}
}
// Namespace provides template functions for the "images" namespace.
type Namespace struct {
+ *images.Filters
cacheMu sync.RWMutex
cache map[string]image.Config
@@ -85,3 +93,18 @@ func (ns *Namespace) Config(path interface{}) (image.Config, error) {
return config, nil
}
+
+func (ns *Namespace) Filter(args ...interface{}) (resource.Image, error) {
+ if len(args) < 2 {
+ return nil, errors.New("must provide an image and one or more filters")
+ }
+
+ img := args[len(args)-1].(resource.Image)
+ filtersv := args[:len(args)-1]
+ filters := make([]gift.Filter, len(filtersv))
+ for i, f := range filtersv {
+ filters[i] = f.(gift.Filter)
+ }
+
+ return img.Filter(filters...)
+}