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"
|
2019-10-20 04:39:00 -04:00
|
|
|
"image/color"
|
2019-08-18 05:21:27 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
|
|
|
"github.com/gohugoio/hugo/config"
|
2021-12-16 09:12:13 -05:00
|
|
|
"github.com/gohugoio/hugo/media"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2021-04-07 10:49:34 -04:00
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
"errors"
|
2021-04-07 10:49:34 -04:00
|
|
|
|
|
|
|
"github.com/bep/gowebp/libwebp/webpoptions"
|
|
|
|
|
2019-08-26 13:12:41 -04:00
|
|
|
"github.com/disintegration/gift"
|
2019-08-18 05:21:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-26 13:12:41 -04:00
|
|
|
imageFormats = map[string]Format{
|
|
|
|
".jpg": JPEG,
|
|
|
|
".jpeg": JPEG,
|
2021-12-13 02:55:15 -05:00
|
|
|
".jpe": JPEG,
|
|
|
|
".jif": JPEG,
|
|
|
|
".jfif": JPEG,
|
2019-08-26 13:12:41 -04:00
|
|
|
".png": PNG,
|
|
|
|
".tif": TIFF,
|
|
|
|
".tiff": TIFF,
|
|
|
|
".bmp": BMP,
|
|
|
|
".gif": GIF,
|
2021-04-07 10:49:34 -04:00
|
|
|
".webp": WEBP,
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
|
2021-12-16 09:12:13 -05:00
|
|
|
imageFormatsBySubType = map[string]Format{
|
2023-01-04 12:24:36 -05:00
|
|
|
media.Builtin.JPEGType.SubType: JPEG,
|
|
|
|
media.Builtin.PNGType.SubType: PNG,
|
|
|
|
media.Builtin.TIFFType.SubType: TIFF,
|
|
|
|
media.Builtin.BMPType.SubType: BMP,
|
|
|
|
media.Builtin.GIFType.SubType: GIF,
|
|
|
|
media.Builtin.WEBPType.SubType: WEBP,
|
2021-12-16 09:12:13 -05:00
|
|
|
}
|
|
|
|
|
2019-08-18 05:21:27 -04:00
|
|
|
// Add or increment if changes to an image format's processing requires
|
|
|
|
// re-generation.
|
2019-08-26 13:12:41 -04:00
|
|
|
imageFormatsVersions = map[Format]int{
|
2021-07-07 04:24:47 -04:00
|
|
|
PNG: 3, // Fix transparency issue with 32 bit images.
|
|
|
|
WEBP: 2, // Fix transparency issue with 32 bit images.
|
2022-10-04 05:00:07 -04:00
|
|
|
GIF: 1, // Fix resize issue with animated GIFs when target != GIF.
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment to mark all processed images as stale. Only use when absolutely needed.
|
|
|
|
// See the finer grained smartCropVersionNumber and imageFormatsVersions.
|
|
|
|
mainImageVersionNumber = 0
|
|
|
|
)
|
|
|
|
|
2019-08-26 13:12:41 -04:00
|
|
|
var anchorPositions = map[string]gift.Anchor{
|
|
|
|
strings.ToLower("Center"): gift.CenterAnchor,
|
|
|
|
strings.ToLower("TopLeft"): gift.TopLeftAnchor,
|
|
|
|
strings.ToLower("Top"): gift.TopAnchor,
|
|
|
|
strings.ToLower("TopRight"): gift.TopRightAnchor,
|
|
|
|
strings.ToLower("Left"): gift.LeftAnchor,
|
|
|
|
strings.ToLower("Right"): gift.RightAnchor,
|
|
|
|
strings.ToLower("BottomLeft"): gift.BottomLeftAnchor,
|
|
|
|
strings.ToLower("Bottom"): gift.BottomAnchor,
|
|
|
|
strings.ToLower("BottomRight"): gift.BottomRightAnchor,
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
|
2021-04-07 10:49:34 -04:00
|
|
|
// These encoding hints are currently only relevant for Webp.
|
|
|
|
var hints = map[string]webpoptions.EncodingPreset{
|
|
|
|
"picture": webpoptions.EncodingPresetPicture,
|
|
|
|
"photo": webpoptions.EncodingPresetPhoto,
|
|
|
|
"drawing": webpoptions.EncodingPresetDrawing,
|
|
|
|
"icon": webpoptions.EncodingPresetIcon,
|
|
|
|
"text": webpoptions.EncodingPresetText,
|
|
|
|
}
|
|
|
|
|
2019-08-26 13:12:41 -04:00
|
|
|
var imageFilters = map[string]gift.Resampling{
|
|
|
|
|
|
|
|
strings.ToLower("NearestNeighbor"): gift.NearestNeighborResampling,
|
|
|
|
strings.ToLower("Box"): gift.BoxResampling,
|
|
|
|
strings.ToLower("Linear"): gift.LinearResampling,
|
|
|
|
strings.ToLower("Hermite"): hermiteResampling,
|
|
|
|
strings.ToLower("MitchellNetravali"): mitchellNetravaliResampling,
|
|
|
|
strings.ToLower("CatmullRom"): catmullRomResampling,
|
|
|
|
strings.ToLower("BSpline"): bSplineResampling,
|
|
|
|
strings.ToLower("Gaussian"): gaussianResampling,
|
|
|
|
strings.ToLower("Lanczos"): gift.LanczosResampling,
|
|
|
|
strings.ToLower("Hann"): hannResampling,
|
|
|
|
strings.ToLower("Hamming"): hammingResampling,
|
|
|
|
strings.ToLower("Blackman"): blackmanResampling,
|
|
|
|
strings.ToLower("Bartlett"): bartlettResampling,
|
|
|
|
strings.ToLower("Welch"): welchResampling,
|
|
|
|
strings.ToLower("Cosine"): cosineResampling,
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 13:12:41 -04:00
|
|
|
func ImageFormatFromExt(ext string) (Format, bool) {
|
2019-08-18 05:21:27 -04:00
|
|
|
f, found := imageFormats[ext]
|
|
|
|
return f, found
|
|
|
|
}
|
|
|
|
|
2021-12-16 09:12:13 -05:00
|
|
|
func ImageFormatFromMediaSubType(sub string) (Format, bool) {
|
|
|
|
f, found := imageFormatsBySubType[sub]
|
|
|
|
return f, found
|
|
|
|
}
|
|
|
|
|
2021-04-07 10:49:34 -04:00
|
|
|
const (
|
|
|
|
defaultJPEGQuality = 75
|
|
|
|
defaultResampleFilter = "box"
|
2023-01-04 12:24:36 -05:00
|
|
|
defaultBgColor = "#ffffff"
|
2021-04-07 10:49:34 -04:00
|
|
|
defaultHint = "photo"
|
|
|
|
)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
var (
|
|
|
|
defaultImaging = map[string]any{
|
|
|
|
"resampleFilter": defaultResampleFilter,
|
|
|
|
"bgColor": defaultBgColor,
|
|
|
|
"hint": defaultHint,
|
|
|
|
"quality": defaultJPEGQuality,
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
defaultImageConfig *config.ConfigNamespace[ImagingConfig, ImagingConfigInternal]
|
|
|
|
)
|
2019-10-20 04:39:00 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
defaultImageConfig, err = DecodeConfig(defaultImaging)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2021-04-07 10:49:34 -04:00
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
2021-04-07 10:49:34 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func DecodeConfig(in map[string]any) (*config.ConfigNamespace[ImagingConfig, ImagingConfigInternal], error) {
|
|
|
|
if in == nil {
|
|
|
|
in = make(map[string]any)
|
2019-10-20 04:39:00 -04:00
|
|
|
}
|
2021-04-07 10:49:34 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
buildConfig := func(in any) (ImagingConfigInternal, any, error) {
|
|
|
|
m, err := maps.ToStringMapE(in)
|
|
|
|
if err != nil {
|
|
|
|
return ImagingConfigInternal{}, nil, err
|
|
|
|
}
|
|
|
|
// Merge in the defaults.
|
|
|
|
maps.MergeShallow(m, defaultImaging)
|
|
|
|
|
|
|
|
var i ImagingConfigInternal
|
|
|
|
if err := mapstructure.Decode(m, &i.Imaging); err != nil {
|
|
|
|
return i, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.Imaging.init(); err != nil {
|
|
|
|
return i, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
i.BgColor, err = hexStringToColor(i.Imaging.BgColor)
|
|
|
|
if err != nil {
|
|
|
|
return i, nil, err
|
|
|
|
}
|
2019-08-18 05:21:27 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if i.Imaging.Anchor != "" && i.Imaging.Anchor != smartCropIdentifier {
|
|
|
|
anchor, found := anchorPositions[i.Imaging.Anchor]
|
|
|
|
if !found {
|
|
|
|
return i, nil, fmt.Errorf("invalid anchor value %q in imaging config", i.Anchor)
|
|
|
|
}
|
|
|
|
i.Anchor = anchor
|
|
|
|
}
|
|
|
|
|
|
|
|
filter, found := imageFilters[i.Imaging.ResampleFilter]
|
2021-04-07 10:49:34 -04:00
|
|
|
if !found {
|
2023-01-04 12:24:36 -05:00
|
|
|
return i, nil, fmt.Errorf("%q is not a valid resample filter", filter)
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
i.ResampleFilter = filter
|
|
|
|
|
|
|
|
return i, nil, nil
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
ns, err := config.DecodeNamespace[ImagingConfig](in, buildConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode media types: %w", err)
|
2019-08-29 04:18:51 -04:00
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
return ns, nil
|
2019-08-29 04:18:51 -04:00
|
|
|
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func DecodeImageConfig(action, config string, defaults *config.ConfigNamespace[ImagingConfig, ImagingConfigInternal], sourceFormat Format) (ImageConfig, error) {
|
2019-08-18 05:21:27 -04:00
|
|
|
var (
|
2021-04-07 10:49:34 -04:00
|
|
|
c ImageConfig = GetDefaultImageConfig(action, defaults)
|
2019-08-18 05:21:27 -04:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
c.Action = action
|
|
|
|
|
|
|
|
if config == "" {
|
|
|
|
return c, errors.New("image config cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Fields(config)
|
|
|
|
for _, part := range parts {
|
|
|
|
part = strings.ToLower(part)
|
|
|
|
|
2019-08-26 13:12:41 -04:00
|
|
|
if part == smartCropIdentifier {
|
|
|
|
c.AnchorStr = smartCropIdentifier
|
2019-08-18 05:21:27 -04:00
|
|
|
} else if pos, ok := anchorPositions[part]; ok {
|
|
|
|
c.Anchor = pos
|
|
|
|
c.AnchorStr = part
|
|
|
|
} else if filter, ok := imageFilters[part]; ok {
|
|
|
|
c.Filter = filter
|
|
|
|
c.FilterStr = part
|
2021-04-07 10:49:34 -04:00
|
|
|
} else if hint, ok := hints[part]; ok {
|
|
|
|
c.Hint = hint
|
2019-10-20 04:39:00 -04:00
|
|
|
} else if part[0] == '#' {
|
|
|
|
c.BgColorStr = part[1:]
|
|
|
|
c.BgColor, err = hexStringToColor(c.BgColorStr)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
2019-08-18 05:21:27 -04:00
|
|
|
} else if part[0] == 'q' {
|
|
|
|
c.Quality, err = strconv.Atoi(part[1:])
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
if c.Quality < 1 || c.Quality > 100 {
|
|
|
|
return c, errors.New("quality ranges from 1 to 100 inclusive")
|
|
|
|
}
|
2021-04-07 10:49:34 -04:00
|
|
|
c.qualitySetForImage = true
|
2019-08-18 05:21:27 -04:00
|
|
|
} else if part[0] == 'r' {
|
|
|
|
c.Rotate, err = strconv.Atoi(part[1:])
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
} else if strings.Contains(part, "x") {
|
|
|
|
widthHeight := strings.Split(part, "x")
|
|
|
|
if len(widthHeight) <= 2 {
|
|
|
|
first := widthHeight[0]
|
|
|
|
if first != "" {
|
|
|
|
c.Width, err = strconv.Atoi(first)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(widthHeight) == 2 {
|
|
|
|
second := widthHeight[1]
|
|
|
|
if second != "" {
|
|
|
|
c.Height, err = strconv.Atoi(second)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return c, errors.New("invalid image dimensions")
|
|
|
|
}
|
2019-09-21 10:50:27 -04:00
|
|
|
} else if f, ok := ImageFormatFromExt("." + part); ok {
|
|
|
|
c.TargetFormat = f
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 13:55:02 -04:00
|
|
|
switch c.Action {
|
|
|
|
case "crop", "fill", "fit":
|
|
|
|
if c.Width == 0 || c.Height == 0 {
|
|
|
|
return c, errors.New("must provide Width and Height")
|
|
|
|
}
|
|
|
|
case "resize":
|
|
|
|
if c.Width == 0 && c.Height == 0 {
|
|
|
|
return c, errors.New("must provide Width or Height")
|
|
|
|
}
|
|
|
|
default:
|
2022-05-02 10:07:52 -04:00
|
|
|
return c, fmt.Errorf("BUG: unknown action %q encountered while decoding image configuration", c.Action)
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.FilterStr == "" {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.FilterStr = defaults.Config.Imaging.ResampleFilter
|
|
|
|
c.Filter = defaults.Config.ResampleFilter
|
2021-04-07 10:49:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Hint == 0 {
|
|
|
|
c.Hint = webpoptions.EncodingPresetPhoto
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.AnchorStr == "" {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.AnchorStr = defaults.Config.Imaging.Anchor
|
|
|
|
c.Anchor = defaults.Config.Anchor
|
2021-04-07 10:49:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// default to the source format
|
|
|
|
if c.TargetFormat == 0 {
|
|
|
|
c.TargetFormat = sourceFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Quality <= 0 && c.TargetFormat.RequiresDefaultQuality() {
|
|
|
|
// We need a quality setting for all JPEGs and WEBPs.
|
2023-01-04 12:24:36 -05:00
|
|
|
c.Quality = defaults.Config.Imaging.Quality
|
2021-04-07 10:49:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.BgColor == nil && c.TargetFormat != sourceFormat {
|
|
|
|
if sourceFormat.SupportsTransparency() && !c.TargetFormat.SupportsTransparency() {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.BgColor = defaults.Config.BgColor
|
|
|
|
c.BgColorStr = defaults.Config.Imaging.BgColor
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImageConfig holds configuration to create a new image from an existing one, resize etc.
|
|
|
|
type ImageConfig struct {
|
2021-04-07 10:49:34 -04:00
|
|
|
// This defines the output format of the output image. It defaults to the source format.
|
2019-09-21 10:50:27 -04:00
|
|
|
TargetFormat Format
|
|
|
|
|
2019-08-18 05:21:27 -04:00
|
|
|
Action string
|
|
|
|
|
2019-08-26 13:12:41 -04:00
|
|
|
// If set, this will be used as the key in filenames etc.
|
|
|
|
Key string
|
|
|
|
|
2019-08-18 05:21:27 -04:00
|
|
|
// Quality ranges from 1 to 100 inclusive, higher is better.
|
2021-04-07 10:49:34 -04:00
|
|
|
// This is only relevant for JPEG and WEBP images.
|
2019-08-18 05:21:27 -04:00
|
|
|
// Default is 75.
|
2021-04-07 10:49:34 -04:00
|
|
|
Quality int
|
|
|
|
qualitySetForImage bool // Whether the above is set for this image.
|
2019-08-18 05:21:27 -04:00
|
|
|
|
|
|
|
// Rotate rotates an image by the given angle counter-clockwise.
|
|
|
|
// The rotation will be performed first.
|
|
|
|
Rotate int
|
|
|
|
|
2019-10-20 04:39:00 -04:00
|
|
|
// Used to fill any transparency.
|
|
|
|
// When set in site config, it's used when converting to a format that does
|
|
|
|
// not support transparency.
|
|
|
|
// When set per image operation, it's used even for formats that does support
|
|
|
|
// transparency.
|
|
|
|
BgColor color.Color
|
|
|
|
BgColorStr string
|
|
|
|
|
2021-04-07 10:49:34 -04:00
|
|
|
// Hint about what type of picture this is. Used to optimize encoding
|
|
|
|
// when target is set to webp.
|
|
|
|
Hint webpoptions.EncodingPreset
|
|
|
|
|
2019-08-18 05:21:27 -04:00
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
|
2019-08-26 13:12:41 -04:00
|
|
|
Filter gift.Resampling
|
2019-08-18 05:21:27 -04:00
|
|
|
FilterStr string
|
|
|
|
|
2019-08-26 13:12:41 -04:00
|
|
|
Anchor gift.Anchor
|
2019-08-18 05:21:27 -04:00
|
|
|
AnchorStr string
|
|
|
|
}
|
|
|
|
|
2019-08-26 13:12:41 -04:00
|
|
|
func (i ImageConfig) GetKey(format Format) string {
|
|
|
|
if i.Key != "" {
|
|
|
|
return i.Action + "_" + i.Key
|
|
|
|
}
|
|
|
|
|
2019-08-18 05:21:27 -04:00
|
|
|
k := strconv.Itoa(i.Width) + "x" + strconv.Itoa(i.Height)
|
|
|
|
if i.Action != "" {
|
|
|
|
k += "_" + i.Action
|
|
|
|
}
|
2021-04-07 10:49:34 -04:00
|
|
|
// This slightly odd construct is here to preserve the old image keys.
|
|
|
|
if i.qualitySetForImage || i.TargetFormat.RequiresDefaultQuality() {
|
2019-08-18 05:21:27 -04:00
|
|
|
k += "_q" + strconv.Itoa(i.Quality)
|
|
|
|
}
|
|
|
|
if i.Rotate != 0 {
|
|
|
|
k += "_r" + strconv.Itoa(i.Rotate)
|
|
|
|
}
|
2019-10-20 04:39:00 -04:00
|
|
|
if i.BgColorStr != "" {
|
|
|
|
k += "_bg" + i.BgColorStr
|
|
|
|
}
|
|
|
|
|
2021-04-07 10:49:34 -04:00
|
|
|
if i.TargetFormat == WEBP {
|
|
|
|
k += "_h" + strconv.Itoa(int(i.Hint))
|
|
|
|
}
|
|
|
|
|
2019-08-18 05:21:27 -04:00
|
|
|
anchor := i.AnchorStr
|
2019-08-26 13:12:41 -04:00
|
|
|
if anchor == smartCropIdentifier {
|
2019-08-18 05:21:27 -04:00
|
|
|
anchor = anchor + strconv.Itoa(smartCropVersionNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
k += "_" + i.FilterStr
|
|
|
|
|
2022-02-22 11:50:23 -05:00
|
|
|
if strings.EqualFold(i.Action, "fill") || strings.EqualFold(i.Action, "crop") {
|
2019-08-18 05:21:27 -04:00
|
|
|
k += "_" + anchor
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := imageFormatsVersions[format]; ok {
|
|
|
|
k += "_" + strconv.Itoa(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if mainImageVersionNumber > 0 {
|
|
|
|
k += "_" + strconv.Itoa(mainImageVersionNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
type ImagingConfigInternal struct {
|
2021-04-07 10:49:34 -04:00
|
|
|
BgColor color.Color
|
|
|
|
Hint webpoptions.EncodingPreset
|
|
|
|
ResampleFilter gift.Resampling
|
|
|
|
Anchor gift.Anchor
|
2019-10-20 04:39:00 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
Imaging ImagingConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ImagingConfigInternal) Compile(externalCfg *ImagingConfig) error {
|
|
|
|
var err error
|
|
|
|
i.BgColor, err = hexStringToColor(externalCfg.BgColor)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if externalCfg.Anchor != "" && externalCfg.Anchor != smartCropIdentifier {
|
|
|
|
anchor, found := anchorPositions[externalCfg.Anchor]
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("invalid anchor value %q in imaging config", i.Anchor)
|
|
|
|
}
|
|
|
|
i.Anchor = anchor
|
|
|
|
}
|
|
|
|
|
|
|
|
filter, found := imageFilters[externalCfg.ResampleFilter]
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("%q is not a valid resample filter", filter)
|
|
|
|
}
|
|
|
|
i.ResampleFilter = filter
|
|
|
|
|
|
|
|
return nil
|
2021-04-07 10:49:34 -04:00
|
|
|
|
2019-10-20 04:39:00 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// ImagingConfig contains default image processing configuration. This will be fetched
|
2019-08-18 05:21:27 -04:00
|
|
|
// from site (or language) config.
|
2023-01-04 12:24:36 -05:00
|
|
|
type ImagingConfig struct {
|
2019-08-18 05:21:27 -04:00
|
|
|
// Default image quality setting (1-100). Only used for JPEG images.
|
|
|
|
Quality int
|
|
|
|
|
2021-04-07 10:49:34 -04:00
|
|
|
// Resample filter to use in resize operations.
|
2019-08-18 05:21:27 -04:00
|
|
|
ResampleFilter string
|
|
|
|
|
2021-04-07 10:49:34 -04:00
|
|
|
// Hint about what type of image this is.
|
|
|
|
// Currently only used when encoding to Webp.
|
|
|
|
// Default is "photo".
|
|
|
|
// Valid values are "picture", "photo", "drawing", "icon", or "text".
|
|
|
|
Hint string
|
|
|
|
|
2019-08-26 13:12:41 -04:00
|
|
|
// The anchor to use in Fill. Default is "smart", i.e. Smart Crop.
|
2019-08-18 05:21:27 -04:00
|
|
|
Anchor string
|
2019-08-29 04:18:51 -04:00
|
|
|
|
2019-10-20 04:39:00 -04:00
|
|
|
// Default color used in fill operations (e.g. "fff" for white).
|
|
|
|
BgColor string
|
|
|
|
|
2019-08-29 04:18:51 -04:00
|
|
|
Exif ExifConfig
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (cfg *ImagingConfig) init() error {
|
2021-04-07 10:49:34 -04:00
|
|
|
if cfg.Quality < 0 || cfg.Quality > 100 {
|
|
|
|
return errors.New("image quality must be a number between 1 and 100")
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.BgColor = strings.ToLower(strings.TrimPrefix(cfg.BgColor, "#"))
|
|
|
|
cfg.Anchor = strings.ToLower(cfg.Anchor)
|
|
|
|
cfg.ResampleFilter = strings.ToLower(cfg.ResampleFilter)
|
|
|
|
cfg.Hint = strings.ToLower(cfg.Hint)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if cfg.Anchor == "" {
|
|
|
|
cfg.Anchor = smartCropIdentifier
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.TrimSpace(cfg.Exif.IncludeFields) == "" && strings.TrimSpace(cfg.Exif.ExcludeFields) == "" {
|
|
|
|
// Don't change this for no good reason. Please don't.
|
|
|
|
cfg.Exif.ExcludeFields = "GPS|Exif|Exposure[M|P|B]|Contrast|Resolution|Sharp|JPEG|Metering|Sensing|Saturation|ColorSpace|Flash|WhiteBalance"
|
|
|
|
}
|
|
|
|
|
2021-04-07 10:49:34 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-29 04:18:51 -04:00
|
|
|
type ExifConfig struct {
|
|
|
|
|
|
|
|
// Regexp matching the Exif fields you want from the (massive) set of Exif info
|
|
|
|
// available. As we cache this info to disk, this is for performance and
|
|
|
|
// disk space reasons more than anything.
|
|
|
|
// If you want it all, put ".*" in this config setting.
|
|
|
|
// Note that if neither this or ExcludeFields is set, Hugo will return a small
|
|
|
|
// default set.
|
|
|
|
IncludeFields string
|
|
|
|
|
|
|
|
// Regexp matching the Exif fields you want to exclude. This may be easier to use
|
|
|
|
// than IncludeFields above, depending on what you want.
|
|
|
|
ExcludeFields string
|
|
|
|
|
|
|
|
// Hugo extracts the "photo taken" date/time into .Date by default.
|
|
|
|
// Set this to true to turn it off.
|
|
|
|
DisableDate bool
|
|
|
|
|
|
|
|
// Hugo extracts the "photo taken where" (GPS latitude and longitude) into
|
|
|
|
// .Long and .Lat. Set this to true to turn it off.
|
|
|
|
DisableLatLong bool
|
2019-08-18 05:21:27 -04:00
|
|
|
}
|