2019-08-18 05:21:27 -04:00
|
|
|
// 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 (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
qt "github.com/frankban/quicktest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeConfig(t *testing.T) {
|
|
|
|
c := qt.New(t)
|
2022-03-17 17:03:27 -04:00
|
|
|
m := map[string]any{
|
2019-08-18 05:21:27 -04:00
|
|
|
"quality": 42,
|
|
|
|
"resampleFilter": "NearestNeighbor",
|
|
|
|
"anchor": "topLeft",
|
|
|
|
}
|
|
|
|
|
2019-10-20 04:39:00 -04:00
|
|
|
imagingConfig, err := DecodeConfig(m)
|
2019-08-18 05:21:27 -04:00
|
|
|
|
|
|
|
c.Assert(err, qt.IsNil)
|
2023-01-04 12:24:36 -05:00
|
|
|
conf := imagingConfig.Config
|
|
|
|
c.Assert(conf.Imaging.Quality, qt.Equals, 42)
|
|
|
|
c.Assert(conf.Imaging.ResampleFilter, qt.Equals, "nearestneighbor")
|
|
|
|
c.Assert(conf.Imaging.Anchor, qt.Equals, "topleft")
|
2019-08-18 05:21:27 -04:00
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
m = map[string]any{}
|
2019-08-18 05:21:27 -04:00
|
|
|
|
2019-10-20 04:39:00 -04:00
|
|
|
imagingConfig, err = DecodeConfig(m)
|
2019-08-18 05:21:27 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2023-01-04 12:24:36 -05:00
|
|
|
conf = imagingConfig.Config
|
|
|
|
c.Assert(conf.Imaging.ResampleFilter, qt.Equals, "box")
|
|
|
|
c.Assert(conf.Imaging.Anchor, qt.Equals, "smart")
|
2019-08-18 05:21:27 -04:00
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
_, err = DecodeConfig(map[string]any{
|
2019-08-18 05:21:27 -04:00
|
|
|
"quality": 123,
|
|
|
|
})
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
_, err = DecodeConfig(map[string]any{
|
2019-08-18 05:21:27 -04:00
|
|
|
"resampleFilter": "asdf",
|
|
|
|
})
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
_, err = DecodeConfig(map[string]any{
|
2019-08-18 05:21:27 -04:00
|
|
|
"anchor": "asdf",
|
|
|
|
})
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
imagingConfig, err = DecodeConfig(map[string]any{
|
2019-08-18 05:21:27 -04:00
|
|
|
"anchor": "Smart",
|
|
|
|
})
|
2023-01-04 12:24:36 -05:00
|
|
|
conf = imagingConfig.Config
|
2019-08-18 05:21:27 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2023-01-04 12:24:36 -05:00
|
|
|
c.Assert(conf.Imaging.Anchor, qt.Equals, "smart")
|
2019-08-29 04:18:51 -04:00
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
imagingConfig, err = DecodeConfig(map[string]any{
|
|
|
|
"exif": map[string]any{
|
2019-08-29 04:18:51 -04:00
|
|
|
"disableLatLong": true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
c.Assert(err, qt.IsNil)
|
2023-01-04 12:24:36 -05:00
|
|
|
conf = imagingConfig.Config
|
|
|
|
c.Assert(conf.Imaging.Exif.DisableLatLong, qt.Equals, true)
|
|
|
|
c.Assert(conf.Imaging.Exif.ExcludeFields, qt.Equals, "GPS|Exif|Exposure[M|P|B]|Contrast|Resolution|Sharp|JPEG|Metering|Sensing|Saturation|ColorSpace|Flash|WhiteBalance")
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeImageConfig(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
2022-03-20 13:55:02 -04:00
|
|
|
action string
|
2019-08-18 05:21:27 -04:00
|
|
|
in string
|
2022-03-17 17:03:27 -04:00
|
|
|
expect any
|
2019-08-18 05:21:27 -04:00
|
|
|
}{
|
2022-03-20 13:55:02 -04:00
|
|
|
{"resize", "300x400", newImageConfig("resize", 300, 400, 75, 0, "box", "smart", "")},
|
|
|
|
{"resize", "300x400 #fff", newImageConfig("resize", 300, 400, 75, 0, "box", "smart", "fff")},
|
|
|
|
{"resize", "100x200 bottomRight", newImageConfig("resize", 100, 200, 75, 0, "box", "BottomRight", "")},
|
|
|
|
{"resize", "10x20 topleft Lanczos", newImageConfig("resize", 10, 20, 75, 0, "Lanczos", "topleft", "")},
|
|
|
|
{"resize", "linear left 10x r180", newImageConfig("resize", 10, 0, 75, 180, "linear", "left", "")},
|
|
|
|
{"resize", "x20 riGht Cosine q95", newImageConfig("resize", 0, 20, 95, 0, "cosine", "right", "")},
|
|
|
|
{"crop", "300x400", newImageConfig("crop", 300, 400, 75, 0, "box", "smart", "")},
|
|
|
|
{"fill", "300x400", newImageConfig("fill", 300, 400, 75, 0, "box", "smart", "")},
|
|
|
|
{"fit", "300x400", newImageConfig("fit", 300, 400, 75, 0, "box", "smart", "")},
|
|
|
|
|
|
|
|
{"resize", "", false},
|
|
|
|
{"resize", "foo", false},
|
|
|
|
{"crop", "100x", false},
|
|
|
|
{"fill", "100x", false},
|
|
|
|
{"fit", "100x", false},
|
|
|
|
{"foo", "100x", false},
|
2019-08-18 05:21:27 -04:00
|
|
|
} {
|
|
|
|
|
2021-04-07 10:49:34 -04:00
|
|
|
cfg, err := DecodeConfig(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-20 13:55:02 -04:00
|
|
|
result, err := DecodeImageConfig(this.action, this.in, cfg, PNG)
|
2019-08-18 05:21:27 -04:00
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] parseImageConfig didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("[%d] err: %s", i, err)
|
|
|
|
}
|
|
|
|
if fmt.Sprint(result) != fmt.Sprint(this.expect) {
|
|
|
|
t.Fatalf("[%d] got\n%v\n but expected\n%v", i, result, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 13:55:02 -04:00
|
|
|
func newImageConfig(action string, width, height, quality, rotate int, filter, anchor, bgColor string) ImageConfig {
|
2023-01-04 12:24:36 -05:00
|
|
|
var c ImageConfig = GetDefaultImageConfig(action, nil)
|
2021-04-07 10:49:34 -04:00
|
|
|
c.TargetFormat = PNG
|
|
|
|
c.Hint = 2
|
2019-08-18 05:21:27 -04:00
|
|
|
c.Width = width
|
|
|
|
c.Height = height
|
|
|
|
c.Quality = quality
|
2021-04-07 10:49:34 -04:00
|
|
|
c.qualitySetForImage = quality != 75
|
2019-08-18 05:21:27 -04:00
|
|
|
c.Rotate = rotate
|
2019-10-20 04:39:00 -04:00
|
|
|
c.BgColorStr = bgColor
|
|
|
|
c.BgColor, _ = hexStringToColor(bgColor)
|
2019-08-18 05:21:27 -04:00
|
|
|
|
|
|
|
if filter != "" {
|
|
|
|
filter = strings.ToLower(filter)
|
|
|
|
if v, ok := imageFilters[filter]; ok {
|
|
|
|
c.Filter = v
|
|
|
|
c.FilterStr = filter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if anchor != "" {
|
2021-04-07 10:49:34 -04:00
|
|
|
if anchor == smartCropIdentifier {
|
2019-08-18 05:21:27 -04:00
|
|
|
c.AnchorStr = anchor
|
2021-04-07 10:49:34 -04:00
|
|
|
} else {
|
|
|
|
anchor = strings.ToLower(anchor)
|
|
|
|
if v, ok := anchorPositions[anchor]; ok {
|
|
|
|
c.Anchor = v
|
|
|
|
c.AnchorStr = anchor
|
|
|
|
}
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|