| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package images |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"fmt" |
| 19 |
"image/color" |
| 20 |
"maps" |
| 21 |
"slices" |
| 22 |
"strconv" |
| 23 |
"strings" |
| 24 |
|
| 25 |
"github.com/gohugoio/hugo/common/hashing" |
| 26 |
"github.com/gohugoio/hugo/common/hmaps" |
| 27 |
"github.com/gohugoio/hugo/config" |
| 28 |
"github.com/gohugoio/hugo/media" |
| 29 |
"github.com/mitchellh/mapstructure" |
| 30 |
|
| 31 |
"github.com/gohugoio/gift" |
| 32 |
) |
| 33 |
|
| 34 |
const ( |
| 35 |
ActionResize = "resize" |
| 36 |
ActionCrop = "crop" |
| 37 |
ActionFit = "fit" |
| 38 |
ActionFill = "fill" |
| 39 |
ActionFilter = "filter" |
| 40 |
) |
| 41 |
|
| 42 |
var Actions = map[string]bool{ |
| 43 |
ActionResize: true, |
| 44 |
ActionCrop: true, |
| 45 |
ActionFit: true, |
| 46 |
ActionFill: true, |
| 47 |
} |
| 48 |
|
| 49 |
var ( |
| 50 |
imageFormats = map[string]Format{ |
| 51 |
".jpg": JPEG, |
| 52 |
".jpeg": JPEG, |
| 53 |
".jpe": JPEG, |
| 54 |
".jif": JPEG, |
| 55 |
".jfif": JPEG, |
| 56 |
".png": PNG, |
| 57 |
".tif": TIFF, |
| 58 |
".tiff": TIFF, |
| 59 |
".bmp": BMP, |
| 60 |
".gif": GIF, |
| 61 |
".webp": WEBP, |
| 62 |
".avif": AVIF, |
| 63 |
".heif": HEIF, |
| 64 |
".heic": HEIC, |
| 65 |
} |
| 66 |
|
| 67 |
// These are the image types we can process. |
| 68 |
processableImageSubTypes = map[string]Format{ |
| 69 |
media.Builtin.JPEGType.SubType: JPEG, |
| 70 |
media.Builtin.PNGType.SubType: PNG, |
| 71 |
media.Builtin.TIFFType.SubType: TIFF, |
| 72 |
media.Builtin.BMPType.SubType: BMP, |
| 73 |
media.Builtin.GIFType.SubType: GIF, |
| 74 |
media.Builtin.WEBPType.SubType: WEBP, |
| 75 |
media.Builtin.AVIFType.SubType: AVIF, |
| 76 |
} |
| 77 |
|
| 78 |
// We cannot process these formats, but we can provide metadata support for them (including width/height). |
| 79 |
metaOnlyImageSubTypes = map[string]Format{ |
| 80 |
media.Builtin.AVIFType.SubType: AVIF, |
| 81 |
media.Builtin.HEIFType.SubType: HEIF, |
| 82 |
media.Builtin.HEICType.SubType: HEIC, |
| 83 |
} |
| 84 |
|
| 85 |
// Increment to mark all processed images as stale. Only use when absolutely needed. |
| 86 |
// See the finer grained smartCropVersionNumber. |
| 87 |
MainImageVersionNumber = 1 |
| 88 |
|
| 89 |
// Increment a format's version number to mark all processed images targeting |
| 90 |
// that format as stale, e.g. after a change to its encoder. This is finer |
| 91 |
// grained than mainImageVersionNumber, which invalidates every format. |
| 92 |
FormatVersionNumbers = map[Format]int{ |
| 93 |
AVIF: 1, |
| 94 |
PNG: 1, // See issue 12543, 12536 |
| 95 |
} |
| 96 |
) |
| 97 |
|
| 98 |
var anchorPositions = map[string]gift.Anchor{ |
| 99 |
strings.ToLower("Center"): gift.CenterAnchor, |
| 100 |
strings.ToLower("TopLeft"): gift.TopLeftAnchor, |
| 101 |
strings.ToLower("Top"): gift.TopAnchor, |
| 102 |
strings.ToLower("TopRight"): gift.TopRightAnchor, |
| 103 |
strings.ToLower("Left"): gift.LeftAnchor, |
| 104 |
strings.ToLower("Right"): gift.RightAnchor, |
| 105 |
strings.ToLower("BottomLeft"): gift.BottomLeftAnchor, |
| 106 |
strings.ToLower("Bottom"): gift.BottomAnchor, |
| 107 |
strings.ToLower("BottomRight"): gift.BottomRightAnchor, |
| 108 |
smartCropIdentifier: SmartCropAnchor, |
| 109 |
} |
| 110 |
|
| 111 |
var compressionMethods = map[string]bool{ |
| 112 |
"lossy": true, |
| 113 |
"lossless": true, |
| 114 |
} |
| 115 |
|
| 116 |
// These encoding hints are used by Webp (preset) and Avif (chroma subsampling). |
| 117 |
var hints = map[string]bool{ |
| 118 |
"picture": true, |
| 119 |
"photo": true, |
| 120 |
"drawing": true, |
| 121 |
"icon": true, |
| 122 |
"text": true, |
| 123 |
} |
| 124 |
|
| 125 |
var imageFilters = map[string]gift.Resampling{ |
| 126 |
strings.ToLower("NearestNeighbor"): gift.NearestNeighborResampling, |
| 127 |
strings.ToLower("Box"): gift.BoxResampling, |
| 128 |
strings.ToLower("Linear"): gift.LinearResampling, |
| 129 |
strings.ToLower("Hermite"): hermiteResampling, |
| 130 |
strings.ToLower("MitchellNetravali"): mitchellNetravaliResampling, |
| 131 |
strings.ToLower("CatmullRom"): catmullRomResampling, |
| 132 |
strings.ToLower("BSpline"): bSplineResampling, |
| 133 |
strings.ToLower("Gaussian"): gaussianResampling, |
| 134 |
strings.ToLower("Lanczos"): gift.LanczosResampling, |
| 135 |
strings.ToLower("Hann"): hannResampling, |
| 136 |
strings.ToLower("Hamming"): hammingResampling, |
| 137 |
strings.ToLower("Blackman"): blackmanResampling, |
| 138 |
strings.ToLower("Bartlett"): bartlettResampling, |
| 139 |
strings.ToLower("Welch"): welchResampling, |
| 140 |
strings.ToLower("Cosine"): cosineResampling, |
| 141 |
} |
| 142 |
|
| 143 |
func ImageFormatFromExt(ext string) (Format, bool) { |
| 144 |
f, found := imageFormats[ext] |
| 145 |
return f, found |
| 146 |
} |
| 147 |
|
| 148 |
type ImageResourceType int |
| 149 |
|
| 150 |
const ( |
| 151 |
// ImageResourceTypeNone means that the resource is not an image, and thus does not support any image operations. |
| 152 |
ImageResourceTypeNone ImageResourceType = iota |
| 153 |
// This is an image, but with no support for any image operations. |
| 154 |
ImageResourceTypeBasic |
| 155 |
// ImageResourceTypeMetaOnly means that only metadata operations (e.g. getting width/height and other metadata) are supported for this format. |
| 156 |
ImageResourceTypeMetaOnly |
| 157 |
// ImageResourceTypeProcessable means that all image operations (resizing, cropping, etc.) are supported for this format. |
| 158 |
ImageResourceTypeProcessable |
| 159 |
) |
| 160 |
|
| 161 |
// ImageFormatFromMediaSubType returns the image format for the given media subtype, and how much image processing operations are supported for this format. |
| 162 |
func ImageFormatFromMediaSubType(sub string) (Format, ImageResourceType) { |
| 163 |
f, found := processableImageSubTypes[sub] |
| 164 |
if found { |
| 165 |
return f, ImageResourceTypeProcessable |
| 166 |
} |
| 167 |
if f, found = metaOnlyImageSubTypes[sub]; found { |
| 168 |
return f, ImageResourceTypeMetaOnly |
| 169 |
} |
| 170 |
return f, ImageResourceTypeBasic |
| 171 |
} |
| 172 |
|
| 173 |
const ( |
| 174 |
defaultResampleFilter = "box" |
| 175 |
defaultBgColor = "#ffffff" |
| 176 |
defaultHint = "photo" |
| 177 |
defaultCompression = "lossy" |
| 178 |
defaultWebpUseSharpYuv = false |
| 179 |
defaultWebpMethod = 2 |
| 180 |
defaultAvifEncoderSpeed = 10 |
| 181 |
) |
| 182 |
|
| 183 |
func DecodeConfig(in map[string]any) (*config.ConfigNamespace[ImagingConfig, ImagingConfigInternal], error) { |
| 184 |
if in == nil { |
| 185 |
in = make(map[string]any) |
| 186 |
} |
| 187 |
|
| 188 |
buildConfig := func(in any) (ImagingConfigInternal, any, error) { |
| 189 |
m, err := hmaps.ToStringMapE(in) |
| 190 |
if err != nil { |
| 191 |
return ImagingConfigInternal{}, nil, err |
| 192 |
} |
| 193 |
|
| 194 |
i := ImagingConfigInternal{ |
| 195 |
Imaging: ImagingConfig{ |
| 196 |
BgColor: defaultBgColor, |
| 197 |
ResampleFilter: defaultResampleFilter, |
| 198 |
Webp: WebpConfig{ |
| 199 |
UseSharpYuv: defaultWebpUseSharpYuv, |
| 200 |
Method: defaultWebpMethod, |
| 201 |
}, |
| 202 |
Avif: AvifConfig{ |
| 203 |
EncoderSpeed: defaultAvifEncoderSpeed, |
| 204 |
}, |
| 205 |
}, |
| 206 |
} |
| 207 |
|
| 208 |
if err := mapstructure.Decode(m, &i.Imaging); err != nil { |
| 209 |
return i, nil, err |
| 210 |
} |
| 211 |
|
| 212 |
if err := i.Imaging.init(); err != nil { |
| 213 |
return i, nil, err |
| 214 |
} |
| 215 |
|
| 216 |
i.BgColor, err = hexStringToColorGo(i.Imaging.BgColor) |
| 217 |
if err != nil { |
| 218 |
return i, nil, err |
| 219 |
} |
| 220 |
|
| 221 |
if i.Imaging.Anchor != "" { |
| 222 |
anchor, found := anchorPositions[i.Imaging.Anchor] |
| 223 |
if !found { |
| 224 |
return i, nil, fmt.Errorf("invalid anchor value %q in imaging config", i.Imaging.Anchor) |
| 225 |
} |
| 226 |
i.Anchor = anchor |
| 227 |
} |
| 228 |
|
| 229 |
filter, found := imageFilters[i.Imaging.ResampleFilter] |
| 230 |
if !found { |
| 231 |
return i, nil, fmt.Errorf("%q is not a valid resample filter", filter) |
| 232 |
} |
| 233 |
|
| 234 |
i.ResampleFilter = filter |
| 235 |
|
| 236 |
return i, i.Imaging, nil |
| 237 |
} |
| 238 |
|
| 239 |
ns, err := config.DecodeNamespace[ImagingConfig](in, buildConfig) |
| 240 |
if err != nil { |
| 241 |
return nil, err |
| 242 |
} |
| 243 |
return ns, nil |
| 244 |
} |
| 245 |
|
| 246 |
func DecodeImageConfig(options []string, defaults *config.ConfigNamespace[ImagingConfig, ImagingConfigInternal], sourceFormat Format) (ImageConfig, error) { |
| 247 |
var ( |
| 248 |
c ImageConfig = newImageConfig() |
| 249 |
err error |
| 250 |
) |
| 251 |
|
| 252 |
// Make to lower case, trim space and remove any empty strings. |
| 253 |
n := 0 |
| 254 |
for _, s := range options { |
| 255 |
s = strings.TrimSpace(s) |
| 256 |
if s != "" { |
| 257 |
options[n] = strings.ToLower(s) |
| 258 |
n++ |
| 259 |
} |
| 260 |
} |
| 261 |
options = options[:n] |
| 262 |
|
| 263 |
for _, part := range options { |
| 264 |
if _, ok := Actions[part]; ok { |
| 265 |
c.Action = part |
| 266 |
} else if pos, ok := anchorPositions[part]; ok { |
| 267 |
c.Anchor = pos |
| 268 |
} else if filter, ok := imageFilters[part]; ok { |
| 269 |
c.Filter = filter |
| 270 |
} else if _, ok := hints[part]; ok { |
| 271 |
c.Hint = part |
| 272 |
} else if _, ok := compressionMethods[part]; ok { |
| 273 |
c.Compression = part |
| 274 |
} else if f, ok := ImageFormatFromExt("." + part); ok { |
| 275 |
c.TargetFormat = f |
| 276 |
} else if part[0] == '#' { |
| 277 |
c.BgColor, err = hexStringToColorGo(part[1:]) |
| 278 |
if err != nil { |
| 279 |
return c, err |
| 280 |
} |
| 281 |
} else if part[0] == 'q' { |
| 282 |
c.Quality, err = strconv.Atoi(part[1:]) |
| 283 |
if err != nil { |
| 284 |
return c, err |
| 285 |
} |
| 286 |
if c.Quality < 1 || c.Quality > 100 { |
| 287 |
return c, errors.New("quality ranges from 1 to 100 inclusive") |
| 288 |
} |
| 289 |
} else if part[0] == 'r' { |
| 290 |
c.Rotate, err = strconv.Atoi(part[1:]) |
| 291 |
if err != nil { |
| 292 |
return c, err |
| 293 |
} |
| 294 |
} else if strings.Contains(part, "x") { |
| 295 |
widthHeight := strings.Split(part, "x") |
| 296 |
if len(widthHeight) <= 2 { |
| 297 |
first := widthHeight[0] |
| 298 |
if first != "" { |
| 299 |
c.Width, err = strconv.Atoi(first) |
| 300 |
if err != nil { |
| 301 |
return c, err |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if len(widthHeight) == 2 { |
| 306 |
second := widthHeight[1] |
| 307 |
if second != "" { |
| 308 |
c.Height, err = strconv.Atoi(second) |
| 309 |
if err != nil { |
| 310 |
return c, err |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
} else { |
| 315 |
return c, errors.New("invalid image dimensions") |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
switch c.Action { |
| 321 |
case ActionCrop, ActionFill, ActionFit: |
| 322 |
if c.Width == 0 || c.Height == 0 { |
| 323 |
return c, errors.New("must provide Width and Height") |
| 324 |
} |
| 325 |
case ActionResize: |
| 326 |
if c.Width == 0 && c.Height == 0 { |
| 327 |
return c, errors.New("must provide Width or Height") |
| 328 |
} |
| 329 |
default: |
| 330 |
if c.Width != 0 || c.Height != 0 { |
| 331 |
return c, errors.New("width or height are not supported for this action") |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
if err := c.init(defaults.Config, sourceFormat); err != nil { |
| 336 |
return c, err |
| 337 |
} |
| 338 |
|
| 339 |
if MainImageVersionNumber > 0 { |
| 340 |
options = append(options, strconv.Itoa(MainImageVersionNumber)) |
| 341 |
} |
| 342 |
|
| 343 |
if v := FormatVersionNumbers[c.TargetFormat]; v > 0 { |
| 344 |
options = append(options, "tfv"+strconv.Itoa(v)) |
| 345 |
} |
| 346 |
|
| 347 |
if smartCropVersionNumber > 0 && c.usesSmartCrop() { |
| 348 |
options = append(options, strconv.Itoa(smartCropVersionNumber)) |
| 349 |
} |
| 350 |
|
| 351 |
c.Key = hashing.HashStringHex(options) |
| 352 |
|
| 353 |
return c, nil |
| 354 |
} |
| 355 |
|
| 356 |
// ImageConfig holds configuration to create a new image from an existing one, resize etc. |
| 357 |
type ImageConfig struct { |
| 358 |
// This defines the output format of the output image. It defaults to the source format. |
| 359 |
TargetFormat Format |
| 360 |
|
| 361 |
// Optional image processing action to perform ("resize", "crop", "fit", "fill"). When empty, the operation may still perform e.g. format conversion. |
| 362 |
Action string |
| 363 |
|
| 364 |
// If set, this will be used as the key in filenames etc. |
| 365 |
Key string |
| 366 |
|
| 367 |
// Whether the palette of a paletted source image should be re-applied to |
| 368 |
// the result when the target format is PNG, so an indexed PNG stays indexed. |
| 369 |
// This is always the case for the geometric actions (resize, crop, fit, fill); |
| 370 |
// a filter chain turns it off unless all its filters are geometric, |
| 371 |
// as other filters routinely introduce colors outside the source palette. |
| 372 |
// See issue 12543. |
| 373 |
PreserveSourcePalette bool |
| 374 |
|
| 375 |
// Quality ranges from 1 to 100 inclusive, higher is better. |
| 376 |
// This is only relevant for JPEG and WEBP images. |
| 377 |
// For WEBP it's only relevant for lossy encoding. |
| 378 |
// Default is 75. |
| 379 |
Quality int |
| 380 |
|
| 381 |
// Rotate rotates an image by the given angle counter-clockwise. |
| 382 |
// The rotation will be performed first. |
| 383 |
Rotate int |
| 384 |
|
| 385 |
// Used to fill any transparency. |
| 386 |
// When set in project config, it's used when converting to a format that does |
| 387 |
// not support transparency. |
| 388 |
// When set per image operation, it's used even for formats that does support |
| 389 |
// transparency. |
| 390 |
BgColor color.Color |
| 391 |
|
| 392 |
Filter gift.Resampling |
| 393 |
Anchor gift.Anchor |
| 394 |
|
| 395 |
// Hint about what type of picture this is. Used to optimize encoding |
| 396 |
// when target is webp (preset) or avif (chroma subsampling). |
| 397 |
Hint string |
| 398 |
|
| 399 |
Compression string |
| 400 |
|
| 401 |
// WebP-specific options. |
| 402 |
UseSharpYuv bool |
| 403 |
Method int |
| 404 |
|
| 405 |
// AVIF-specific options. |
| 406 |
EncoderSpeed int |
| 407 |
|
| 408 |
Width int |
| 409 |
Height int |
| 410 |
} |
| 411 |
|
| 412 |
func (c *ImageConfig) init(defaults ImagingConfigInternal, sourceFormat Format) error { |
| 413 |
if c.Action != "" && c.Anchor == -1 { |
| 414 |
c.Anchor = defaults.Anchor |
| 415 |
} |
| 416 |
if c.TargetFormat == 0 { |
| 417 |
c.TargetFormat = sourceFormat |
| 418 |
} |
| 419 |
if c.Filter == nil { |
| 420 |
c.Filter = defaults.ResampleFilter |
| 421 |
} |
| 422 |
if c.Anchor == -1 { |
| 423 |
c.Anchor = defaults.Anchor |
| 424 |
} |
| 425 |
if c.Hint == "" { |
| 426 |
c.Hint = defaults.Imaging.hintFor(c.TargetFormat) |
| 427 |
} |
| 428 |
|
| 429 |
if c.Quality == 0 { |
| 430 |
c.Quality = defaults.Imaging.qualityFor(c.TargetFormat) |
| 431 |
} |
| 432 |
|
| 433 |
if c.Compression == "" { |
| 434 |
c.Compression = defaults.Imaging.compressionFor(c.TargetFormat) |
| 435 |
} |
| 436 |
|
| 437 |
if c.TargetFormat == AVIF { |
| 438 |
c.EncoderSpeed = defaults.Imaging.Avif.EncoderSpeed |
| 439 |
} |
| 440 |
|
| 441 |
if c.TargetFormat == WEBP { |
| 442 |
c.Method = defaults.Imaging.Webp.Method |
| 443 |
c.UseSharpYuv = defaults.Imaging.Webp.UseSharpYuv |
| 444 |
} |
| 445 |
|
| 446 |
if c.BgColor == nil && c.TargetFormat != sourceFormat { |
| 447 |
if sourceFormat.SupportsTransparency() && !c.TargetFormat.SupportsTransparency() { |
| 448 |
c.BgColor = defaults.BgColor |
| 449 |
} |
| 450 |
} |
| 451 |
return nil |
| 452 |
} |
| 453 |
|
| 454 |
func (c ImageConfig) usesSmartCrop() bool { |
| 455 |
return c.Anchor == SmartCropAnchor && (c.Action == ActionCrop || c.Action == ActionFill) |
| 456 |
} |
| 457 |
|
| 458 |
func (c ImageConfig) Reanchor(a gift.Anchor) ImageConfig { |
| 459 |
c.Anchor = a |
| 460 |
c.Key = hashing.HashStringHex(c.Key, "reanchor", a) |
| 461 |
return c |
| 462 |
} |
| 463 |
|
| 464 |
type ImagingConfigInternal struct { |
| 465 |
BgColor color.Color |
| 466 |
ResampleFilter gift.Resampling |
| 467 |
Anchor gift.Anchor |
| 468 |
|
| 469 |
Imaging ImagingConfig |
| 470 |
} |
| 471 |
|
| 472 |
func (i *ImagingConfigInternal) Compile(externalCfg *ImagingConfig) error { |
| 473 |
var err error |
| 474 |
i.BgColor, err = hexStringToColorGo(externalCfg.BgColor) |
| 475 |
if err != nil { |
| 476 |
return err |
| 477 |
} |
| 478 |
|
| 479 |
if externalCfg.Anchor != "" { |
| 480 |
anchor, found := anchorPositions[externalCfg.Anchor] |
| 481 |
if !found { |
| 482 |
return fmt.Errorf("invalid anchor value %q in imaging config", externalCfg.Anchor) |
| 483 |
} |
| 484 |
i.Anchor = anchor |
| 485 |
} |
| 486 |
|
| 487 |
filter, found := imageFilters[externalCfg.ResampleFilter] |
| 488 |
if !found { |
| 489 |
return fmt.Errorf("%q is not a valid resample filter", externalCfg.ResampleFilter) |
| 490 |
} |
| 491 |
i.ResampleFilter = filter |
| 492 |
|
| 493 |
return nil |
| 494 |
} |
| 495 |
|
| 496 |
// ImagingConfig contains default image processing configuration. This will be fetched |
| 497 |
// from site (or language) config. |
| 498 |
type ImagingConfig struct { |
| 499 |
// Default image quality setting (1-100). Used as the fallback for JPEG, |
| 500 |
// WebP and AVIF when no per-format quality is set. When left unset, JPEG |
| 501 |
// and WebP default to 75 and AVIF to 60 (its scale differs perceptually). |
| 502 |
// Deprecated in v0.163.0: set the quality per format instead, see |
| 503 |
// imaging.jpeg.quality, imaging.webp.quality and imaging.avif.quality. |
| 504 |
Quality int `json:"-"` |
| 505 |
|
| 506 |
// Compression method to use. |
| 507 |
// One of "lossy" or "lossless". |
| 508 |
// Note that lossless is currently only supported for WebP and AVIF. |
| 509 |
// Deprecated in v0.163.0: set the compression method per format instead, see imaging.webp.compression and imaging.avif.compression. |
| 510 |
Compression string `json:"-"` |
| 511 |
|
| 512 |
// Resample filter to use in resize operations. |
| 513 |
ResampleFilter string |
| 514 |
|
| 515 |
// Hint about what type of image this is. |
| 516 |
// Used when encoding to Webp (preset) and Avif (chroma subsampling). |
| 517 |
// Default is "photo". |
| 518 |
// Valid values are "picture", "photo", "drawing", "icon", or "text". |
| 519 |
// Moved to WebpConfig in v0.155.0, but kept here for backwards compatibility. |
| 520 |
Hint string `json:"-"` |
| 521 |
|
| 522 |
// The anchor to use in Fill. Default is "smart", i.e. Smart Crop. |
| 523 |
Anchor string |
| 524 |
|
| 525 |
// Default color used in fill operations (e.g. "fff" for white). |
| 526 |
BgColor string |
| 527 |
|
| 528 |
Exif ExifConfig |
| 529 |
Meta MetaConfig |
| 530 |
Jpeg JpegConfig |
| 531 |
Webp WebpConfig |
| 532 |
Avif AvifConfig |
| 533 |
} |
| 534 |
|
| 535 |
func (cfg *ImagingConfig) qualityFor(f Format) int { |
| 536 |
switch f { |
| 537 |
case JPEG: |
| 538 |
return cfg.Jpeg.Quality |
| 539 |
case WEBP: |
| 540 |
return cfg.Webp.Quality |
| 541 |
case AVIF: |
| 542 |
return cfg.Avif.Quality |
| 543 |
} |
| 544 |
return 0 |
| 545 |
} |
| 546 |
|
| 547 |
func (cfg *ImagingConfig) compressionFor(f Format) string { |
| 548 |
switch f { |
| 549 |
case WEBP: |
| 550 |
return cfg.Webp.Compression |
| 551 |
case AVIF: |
| 552 |
return cfg.Avif.Compression |
| 553 |
} |
| 554 |
return "" |
| 555 |
} |
| 556 |
|
| 557 |
// hintFor returns the configured hint for the given target format. |
| 558 |
func (cfg *ImagingConfig) hintFor(f Format) string { |
| 559 |
switch f { |
| 560 |
case WEBP: |
| 561 |
return cfg.Webp.Hint |
| 562 |
case AVIF: |
| 563 |
return cfg.Avif.Hint |
| 564 |
} |
| 565 |
return "" |
| 566 |
} |
| 567 |
|
| 568 |
var validMetaSources = map[string]bool{ |
| 569 |
"exif": true, |
| 570 |
"iptc": true, |
| 571 |
"xmp": true, |
| 572 |
} |
| 573 |
|
| 574 |
func (cfg *ImagingConfig) init() error { |
| 575 |
cfg.BgColor = strings.ToLower(strings.TrimPrefix(cfg.BgColor, "#")) |
| 576 |
cfg.Anchor = strings.ToLower(cfg.Anchor) |
| 577 |
cfg.ResampleFilter = strings.ToLower(cfg.ResampleFilter) |
| 578 |
cfg.Hint = strings.ToLower(cfg.Hint) |
| 579 |
cfg.Compression = strings.ToLower(cfg.Compression) |
| 580 |
if err := cfg.Jpeg.init(cfg); err != nil { |
| 581 |
return fmt.Errorf("invalid jpeg config: %w", err) |
| 582 |
} |
| 583 |
if err := cfg.Webp.init(cfg); err != nil { |
| 584 |
return fmt.Errorf("invalid webp config: %w", err) |
| 585 |
} |
| 586 |
if err := cfg.Avif.init(cfg); err != nil { |
| 587 |
return fmt.Errorf("invalid avif config: %w", err) |
| 588 |
} |
| 589 |
if cfg.Quality < 0 || cfg.Quality > 100 { |
| 590 |
return fmt.Errorf("imaging.quality must be between 1 and 100 inclusive, got %d", cfg.Quality) |
| 591 |
} |
| 592 |
|
| 593 |
if cfg.Anchor == "" { |
| 594 |
cfg.Anchor = smartCropIdentifier |
| 595 |
} |
| 596 |
|
| 597 |
if strings.TrimSpace(cfg.Exif.IncludeFields) == "" && strings.TrimSpace(cfg.Exif.ExcludeFields) == "" { |
| 598 |
// Don't change this for no good reason. Please don't. |
| 599 |
cfg.Exif.ExcludeFields = "GPS|Exif|Exposure[M|P|B]|Contrast|Resolution|Sharp|JPEG|Metering|Sensing|Saturation|ColorSpace|Flash|WhiteBalance" |
| 600 |
} |
| 601 |
|
| 602 |
if len(cfg.Meta.Fields) == 0 { |
| 603 |
// Default: include all fields except technical metadata. |
| 604 |
// Don't change this for no good reason. Please don't. |
| 605 |
cfg.Meta.Fields = []string{ |
| 606 |
"! *{GPS,Exif,Exposure[MPB],Contrast,Resolution,Sharp,JPEG,Metering,Sensing,Saturation,ColorSpace,Flash,WhiteBalance}*", |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
if len(cfg.Meta.Sources) == 0 { |
| 611 |
// Default to EXIF and IPTC (XMP is slower to decode). |
| 612 |
cfg.Meta.Sources = []string{"exif", "iptc"} |
| 613 |
} else { |
| 614 |
// Normalize to lowercase. |
| 615 |
for i, s := range cfg.Meta.Sources { |
| 616 |
cfg.Meta.Sources[i] = strings.ToLower(s) |
| 617 |
if !validMetaSources[cfg.Meta.Sources[i]] { |
| 618 |
keys := slices.Collect(maps.Keys(validMetaSources)) |
| 619 |
slices.Sort(keys) |
| 620 |
return fmt.Errorf("invalid metadata source %q in imaging.meta.sources config; must be one of %s", s, keys) |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
return nil |
| 626 |
} |
| 627 |
|
| 628 |
type ExifConfig struct { |
| 629 |
// Regexp matching the Exif fields you want from the (massive) set of Exif info |
| 630 |
// available. As we cache this info to disk, this is for performance and |
| 631 |
// disk space reasons more than anything. |
| 632 |
// If you want it all, put ".*" in this config setting. |
| 633 |
// Note that if neither this or ExcludeFields is set, Hugo will return a small |
| 634 |
// default set. |
| 635 |
IncludeFields string |
| 636 |
|
| 637 |
// Regexp matching the Exif fields you want to exclude. This may be easier to use |
| 638 |
// than IncludeFields above, depending on what you want. |
| 639 |
ExcludeFields string |
| 640 |
|
| 641 |
// Hugo extracts the "photo taken" date/time into .Date by default. |
| 642 |
// Set this to true to turn it off. |
| 643 |
DisableDate bool |
| 644 |
|
| 645 |
// Hugo extracts the "photo taken where" (GPS latitude and longitude) into |
| 646 |
// .Long and .Lat. Set this to true to turn it off. |
| 647 |
DisableLatLong bool |
| 648 |
} |
| 649 |
|
| 650 |
type MetaConfig struct { |
| 651 |
// Glob patterns for which metadata fields to include. |
| 652 |
// Use "! " prefix to exclude patterns (e.g., "! *GPS*" excludes GPS fields). |
| 653 |
// Patterns are OR'd together for inclusion, AND'd for exclusion. |
| 654 |
// If empty, a default set excluding technical metadata is used. |
| 655 |
// Use ["**"] to include all fields. |
| 656 |
Fields []string |
| 657 |
|
| 658 |
// Which metadata sources to include. |
| 659 |
// Valid values are "exif", "iptc", "xmp". |
| 660 |
// Default is ["exif", "iptc"] (XMP is excluded for performance reasons). |
| 661 |
Sources []string |
| 662 |
} |
| 663 |
|
| 664 |
// JpegConfig holds JPEG-specific encoding configuration. |
| 665 |
type JpegConfig struct { |
| 666 |
// Quality setting (1-100). Falls back to the global imaging.quality if unset. |
| 667 |
Quality int |
| 668 |
} |
| 669 |
|
| 670 |
func (c *JpegConfig) init(ic *ImagingConfig) error { |
| 671 |
if c.Quality == 0 { |
| 672 |
c.Quality = ic.Quality |
| 673 |
} |
| 674 |
if c.Quality == 0 { |
| 675 |
c.Quality = 75 |
| 676 |
} |
| 677 |
if c.Quality < 1 || c.Quality > 100 { |
| 678 |
return fmt.Errorf("imaging.jpeg.quality must be between 1 and 100 inclusive, got %d", c.Quality) |
| 679 |
} |
| 680 |
return nil |
| 681 |
} |
| 682 |
|
| 683 |
// AvifConfig holds AVIF-specific encoding configuration. |
| 684 |
type AvifConfig struct { |
| 685 |
// Quality setting (1-100). Falls back to the global imaging.quality if unset. |
| 686 |
Quality int |
| 687 |
|
| 688 |
// Compression method to use. |
| 689 |
// One of "lossy" or "lossless". |
| 690 |
Compression string |
| 691 |
|
| 692 |
// Hint about what type of image this is. Used for chroma subsampling. |
| 693 |
// Valid values are "picture", "photo", "drawing", "icon", or "text". |
| 694 |
// Default is "photo". |
| 695 |
Hint string |
| 696 |
|
| 697 |
// Encoder quality/speed trade-off, 1 (slowest, best quality / smallest |
| 698 |
// files) to 10 (fastest). Default is 10 — fast enough for incremental |
| 699 |
// builds with quality indistinguishable from slower settings at typical |
| 700 |
// web thumbnail sizes. Lower values reduce file size at the cost of |
| 701 |
// build time. |
| 702 |
// We recommend sticking with the default of 10 unless you have a specific reason to change it, |
| 703 |
// and to stay above 5 to avoid very long build times and timeouts. |
| 704 |
EncoderSpeed int |
| 705 |
} |
| 706 |
|
| 707 |
func (c *AvifConfig) init(ic *ImagingConfig) error { |
| 708 |
if c.Hint == "" { |
| 709 |
c.Hint = ic.Hint |
| 710 |
} |
| 711 |
if c.Compression == "" { |
| 712 |
c.Compression = ic.Compression |
| 713 |
} |
| 714 |
if c.Quality == 0 { |
| 715 |
c.Quality = ic.Quality |
| 716 |
} |
| 717 |
if c.Hint == "" { |
| 718 |
c.Hint = defaultHint |
| 719 |
} |
| 720 |
if c.Compression == "" { |
| 721 |
c.Compression = defaultCompression |
| 722 |
} |
| 723 |
if c.Quality == 0 { |
| 724 |
c.Quality = 60 |
| 725 |
} |
| 726 |
|
| 727 |
c.Hint = strings.ToLower(c.Hint) |
| 728 |
c.Compression = strings.ToLower(c.Compression) |
| 729 |
|
| 730 |
if c.Hint != "" && !hints[c.Hint] { |
| 731 |
return fmt.Errorf("imaging.avif.hint must be one of picture, photo, drawing, icon, or text, got %q", c.Hint) |
| 732 |
} |
| 733 |
if c.EncoderSpeed < 1 || c.EncoderSpeed > 10 { |
| 734 |
return fmt.Errorf("imaging.avif.encoderSpeed must be between 1 and 10, got %d", c.EncoderSpeed) |
| 735 |
} |
| 736 |
|
| 737 |
if c.Compression != "" && !compressionMethods[c.Compression] { |
| 738 |
return fmt.Errorf("imaging.avif.compression must be one of lossy or lossless, got %q", c.Compression) |
| 739 |
} |
| 740 |
if c.Quality < 1 || c.Quality > 100 { |
| 741 |
return fmt.Errorf("imaging.avif.quality must be between 1 and 100 inclusive, got %d", c.Quality) |
| 742 |
} |
| 743 |
|
| 744 |
return nil |
| 745 |
} |
| 746 |
|
| 747 |
// WebpConfig holds WebP-specific encoding configuration. |
| 748 |
type WebpConfig struct { |
| 749 |
// Quality setting (1-100). Falls back to the global imaging.quality if unset. |
| 750 |
// Only relevant for lossy encoding. |
| 751 |
Quality int |
| 752 |
|
| 753 |
// Compression method to use. |
| 754 |
// One of "lossy" or "lossless". |
| 755 |
Compression string |
| 756 |
|
| 757 |
// Hint about what type of image this is. |
| 758 |
// Valid values are "picture", "photo", "drawing", "icon", or "text". |
| 759 |
// Default is "photo". |
| 760 |
Hint string |
| 761 |
|
| 762 |
// Use sharp (and slow) RGB->YUV conversion. |
| 763 |
// Default is true. |
| 764 |
UseSharpYuv bool |
| 765 |
|
| 766 |
// Quality/speed trade-off (0=fast, 6=slower-better). |
| 767 |
// Default is 2. |
| 768 |
Method int |
| 769 |
} |
| 770 |
|
| 771 |
func (c *WebpConfig) init(ic *ImagingConfig) error { |
| 772 |
if c.Hint == "" { |
| 773 |
c.Hint = ic.Hint |
| 774 |
} |
| 775 |
if c.Compression == "" { |
| 776 |
c.Compression = ic.Compression |
| 777 |
} |
| 778 |
if c.Quality == 0 { |
| 779 |
c.Quality = ic.Quality |
| 780 |
} |
| 781 |
if c.Hint == "" { |
| 782 |
c.Hint = defaultHint |
| 783 |
} |
| 784 |
if c.Compression == "" { |
| 785 |
c.Compression = defaultCompression |
| 786 |
} |
| 787 |
if c.Quality == 0 { |
| 788 |
c.Quality = 75 |
| 789 |
} |
| 790 |
|
| 791 |
c.Hint = strings.ToLower(c.Hint) |
| 792 |
c.Compression = strings.ToLower(c.Compression) |
| 793 |
|
| 794 |
if c.Hint != "" && !hints[c.Hint] { |
| 795 |
return fmt.Errorf("imaging.webp.hint must be one of picture, photo, drawing, icon, or text, got %q", c.Hint) |
| 796 |
} |
| 797 |
if c.Method < 0 || c.Method > 6 { |
| 798 |
return fmt.Errorf("imaging.webp.method must be between 0 and 6, got %d", c.Method) |
| 799 |
} |
| 800 |
if c.Compression != "" && !compressionMethods[c.Compression] { |
| 801 |
return fmt.Errorf("imaging.webp.compression must be one of lossy or lossless, got %q", c.Compression) |
| 802 |
} |
| 803 |
if c.Quality < 1 || c.Quality > 100 { |
| 804 |
return fmt.Errorf("imaging.webp.quality must be between 1 and 100 inclusive, got %d", c.Quality) |
| 805 |
} |
| 806 |
return nil |
| 807 |
} |