Allow slices in the image Filter funcs, not just varargs

[ci skip]

See #6255
This commit is contained in:
Bjørn Erik Pedersen
2019-09-04 14:07:10 +02:00
parent 529c7f1090
commit bb894ceaf8
8 changed files with 90 additions and 21 deletions

View File

@@ -28,6 +28,8 @@ import (
"strings"
"sync"
"github.com/disintegration/gift"
"github.com/gohugoio/hugo/cache/filecache"
"github.com/gohugoio/hugo/resources/images/exif"
@@ -37,7 +39,6 @@ import (
_errors "github.com/pkg/errors"
"github.com/disintegration/gift"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/resources/images"
@@ -209,12 +210,19 @@ func (i *imageResource) Fill(spec string) (resource.Image, error) {
})
}
func (i *imageResource) Filter(filters ...gift.Filter) (resource.Image, error) {
func (i *imageResource) Filter(filters ...interface{}) (resource.Image, error) {
conf := i.Proc.GetDefaultImageConfig("filter")
conf.Key = internal.HashString(filters)
var gfilters []gift.Filter
for _, f := range filters {
gfilters = append(gfilters, images.ToFilters(f)...)
}
conf.Key = internal.HashString(gfilters)
return i.doWithImageConfig(conf, func(src image.Image) (image.Image, error) {
return i.Proc.Filter(src, filters...)
return i.Proc.Filter(src, gfilters...)
})
}
@@ -331,9 +339,6 @@ func (i *imageResource) getImageMetaCacheTargetPath() string {
func (i *imageResource) relTargetPathFromConfig(conf images.ImageConfig) dirFile {
p1, p2 := helpers.FileAndExt(i.getResourcePaths().relTargetDirFile.file)
if conf.Action == "trace" {
p2 = ".svg"
}
h, _ := i.hash()
idStr := fmt.Sprintf("_hu%s_%d", h, i.size())

View File

@@ -512,7 +512,7 @@ func TestImageOperationsGolden(t *testing.T) {
c.Assert(rel, qt.Not(qt.Equals), "")
}
resized, err = resized.Filter(filters[0:4]...)
resized, err = resized.Filter(filters[0:4])
c.Assert(err, qt.IsNil)
rel := resized.RelPermalink()
c.Log("filter all", rel)

View File

@@ -0,0 +1,34 @@
// 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.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package images
import (
"testing"
"github.com/gohugoio/hugo/resources/internal"
qt "github.com/frankban/quicktest"
)
func TestFilterHash(t *testing.T) {
c := qt.New(t)
f := &Filters{}
c.Assert(internal.HashString(f.Grayscale()), qt.Equals, internal.HashString(f.Grayscale()))
c.Assert(internal.HashString(f.Grayscale()), qt.Not(qt.Equals), internal.HashString(f.Invert()))
c.Assert(internal.HashString(f.Gamma(32)), qt.Not(qt.Equals), internal.HashString(f.Gamma(33)))
c.Assert(internal.HashString(f.Gamma(32)), qt.Equals, internal.HashString(f.Gamma(32)))
}

View File

@@ -14,6 +14,7 @@
package images
import (
"fmt"
"image"
"image/color"
"image/gif"
@@ -259,3 +260,20 @@ func imageConfigFromImage(img image.Image) image.Config {
b := img.Bounds()
return image.Config{Width: b.Max.X, Height: b.Max.Y}
}
func ToFilters(in interface{}) []gift.Filter {
switch v := in.(type) {
case []gift.Filter:
return v
case []filter:
vv := make([]gift.Filter, len(v))
for i, f := range v {
vv[i] = f
}
return vv
case gift.Filter:
return []gift.Filter{v}
default:
panic(fmt.Sprintf("%T is not an image filter", in))
}
}

View File

@@ -14,7 +14,6 @@
package resource
import (
"github.com/disintegration/gift"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/resources/images/exif"
@@ -49,7 +48,7 @@ type ImageOps interface {
Fill(spec string) (Image, error)
Fit(spec string) (Image, error)
Resize(spec string) (Image, error)
Filter(filters ...gift.Filter) (Image, error)
Filter(filters ...interface{}) (Image, error)
Exif() (*exif.Exif, error)
}

View File

@@ -21,7 +21,6 @@ import (
"strings"
"sync"
"github.com/disintegration/gift"
"github.com/gohugoio/hugo/resources/images/exif"
"github.com/spf13/afero"
@@ -174,7 +173,7 @@ func (r *resourceAdapter) Fit(spec string) (resource.Image, error) {
return r.getImageOps().Fit(spec)
}
func (r *resourceAdapter) Filter(filters ...gift.Filter) (resource.Image, error) {
func (r *resourceAdapter) Filter(filters ...interface{}) (resource.Image, error) {
return r.getImageOps().Filter(filters...)
}