| 1 |
// Copyright 2024 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_test |
| 15 |
|
| 16 |
import ( |
| 17 |
_ "image/jpeg" |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/gohugoio/hugo/resources/images/imagetesting" |
| 22 |
) |
| 23 |
|
| 24 |
const goldenProcess = ` |
| 25 |
{{ define "process"}} |
| 26 |
{{ $img := .img.Process .spec }} |
| 27 |
{{ $ext := path.Ext $img.RelPermalink }} |
| 28 |
{{ $name := printf "images/%s%s" (.spec | anchorize) $ext }} |
| 29 |
{{ with $img | resources.Copy $name }} |
| 30 |
{{ .Publish }} |
| 31 |
{{ end }} |
| 32 |
{{ end }} |
| 33 |
` |
| 34 |
|
| 35 |
// To regenerate the golden files, see the -writegoldenfiles flag in the imagetesting package. |
| 36 |
func TestImagesGoldenFiltersMisc(t *testing.T) { |
| 37 |
t.Parallel() |
| 38 |
|
| 39 |
if imagetesting.SkipGoldenTests { |
| 40 |
t.Skip("Skip golden test on this architecture") |
| 41 |
} |
| 42 |
|
| 43 |
// Will be used as the base folder for generated images. |
| 44 |
name := "filters/misc" |
| 45 |
|
| 46 |
files := ` |
| 47 |
-- hugo.toml -- |
| 48 |
-- assets/rotate270.jpg -- |
| 49 |
sourcefilename: ../testdata/exif/orientation6.jpg |
| 50 |
-- assets/sunset.jpg -- |
| 51 |
sourcefilename: ../testdata/sunset.jpg |
| 52 |
-- assets/gopher.png -- |
| 53 |
sourcefilename: ../testdata/gopher-hero8.png |
| 54 |
-- layouts/home.html -- |
| 55 |
Home. |
| 56 |
{{ $sunset := (resources.Get "sunset.jpg").Resize "x300" }} |
| 57 |
{{ $sunsetGrayscale := $sunset.Filter (images.Grayscale) }} |
| 58 |
{{ $gopher := (resources.Get "gopher.png").Resize "x80" }} |
| 59 |
{{ $overlayFilter := images.Overlay $gopher 20 20 }} |
| 60 |
|
| 61 |
{{ $textOpts := dict |
| 62 |
"color" "#fbfaf5" |
| 63 |
"linespacing" 8 |
| 64 |
"size" 40 |
| 65 |
"x" 25 |
| 66 |
"y" 190 |
| 67 |
}} |
| 68 |
|
| 69 |
{{/* These are sorted. */}} |
| 70 |
{{ template "filters" (dict "name" "brightness-40.jpg" "img" $sunset "filters" (images.Brightness 40)) }} |
| 71 |
{{ template "filters" (dict "name" "contrast-50.jpg" "img" $sunset "filters" (images.Contrast 50)) }} |
| 72 |
{{ template "filters" (dict "name" "dither-default.jpg" "img" $sunset "filters" (images.Dither)) }} |
| 73 |
{{ template "filters" (dict "name" "gamma-1.667.jpg" "img" $sunset "filters" (images.Gamma 1.667)) }} |
| 74 |
{{ template "filters" (dict "name" "gaussianblur-5.jpg" "img" $sunset "filters" (images.GaussianBlur 5)) }} |
| 75 |
{{ template "filters" (dict "name" "grayscale.jpg" "img" $sunset "filters" (images.Grayscale)) }} |
| 76 |
{{ template "filters" (dict "name" "grayscale+colorize-180-50-20.jpg" "img" $sunset "filters" (slice images.Grayscale (images.Colorize 180 50 20))) }} |
| 77 |
{{ template "filters" (dict "name" "colorbalance-180-50-20.jpg" "img" $sunset "filters" (images.ColorBalance 180 50 20)) }} |
| 78 |
{{ template "filters" (dict "name" "hue--15.jpg" "img" $sunset "filters" (images.Hue -15)) }} |
| 79 |
{{ template "filters" (dict "name" "invert.jpg" "img" $sunset "filters" (images.Invert)) }} |
| 80 |
{{ template "filters" (dict "name" "opacity-0.65.jpg" "img" $sunset "filters" (images.Opacity 0.65)) }} |
| 81 |
{{ template "filters" (dict "name" "overlay-20-20.jpg" "img" $sunset "filters" ($overlayFilter)) }} |
| 82 |
{{ template "filters" (dict "name" "padding-20-40-#976941.jpg" "img" $sunset "filters" (images.Padding 20 40 "#976941" )) }} |
| 83 |
{{ template "filters" (dict "name" "pixelate-10.jpg" "img" $sunset "filters" (images.Pixelate 10)) }} |
| 84 |
{{ template "filters" (dict "name" "rotate270.jpg" "img" (resources.Get "rotate270.jpg") "filters" images.AutoOrient) }} |
| 85 |
{{ template "filters" (dict "name" "saturation-65.jpg" "img" $sunset "filters" (images.Saturation 65)) }} |
| 86 |
{{ template "filters" (dict "name" "sepia-80.jpg" "img" $sunsetGrayscale "filters" (images.Sepia 80)) }} |
| 87 |
{{ template "filters" (dict "name" "sigmoid-0.6--4.jpg" "img" $sunset "filters" (images.Sigmoid 0.6 -4 )) }} |
| 88 |
{{ template "filters" (dict "name" "text.jpg" "img" $sunset "filters" (images.Text "Hugo Rocks!" $textOpts )) }} |
| 89 |
{{ template "filters" (dict "name" "unsharpmask.jpg" "img" $sunset "filters" (images.UnsharpMask 10 0.4 0.03)) }} |
| 90 |
|
| 91 |
|
| 92 |
{{ define "filters"}} |
| 93 |
{{ if lt (len (path.Ext .name)) 4 }} |
| 94 |
{{ errorf "No extension in %q" .name }} |
| 95 |
{{ end }} |
| 96 |
{{ $img := .img.Filter .filters }} |
| 97 |
{{ $name := printf "images/%s" .name }} |
| 98 |
{{ with $img | resources.Copy $name }} |
| 99 |
{{ .Publish }} |
| 100 |
{{ end }} |
| 101 |
{{ end }} |
| 102 |
` |
| 103 |
|
| 104 |
opts := imagetesting.DefaultGoldenOpts |
| 105 |
opts.T = t |
| 106 |
opts.Name = name |
| 107 |
opts.Files = files |
| 108 |
|
| 109 |
imagetesting.RunGolden(opts) |
| 110 |
} |
| 111 |
|
| 112 |
func TestImagesGoldenFiltersMask(t *testing.T) { |
| 113 |
t.Parallel() |
| 114 |
|
| 115 |
if imagetesting.SkipGoldenTests { |
| 116 |
t.Skip("Skip golden test on this architecture") |
| 117 |
} |
| 118 |
|
| 119 |
// Will be used as the base folder for generated images. |
| 120 |
name := "filters/mask" |
| 121 |
|
| 122 |
files := ` |
| 123 |
-- hugo.toml -- |
| 124 |
[imaging] |
| 125 |
bgColor = '#ebcc34' |
| 126 |
hint = 'photo' |
| 127 |
quality = 75 |
| 128 |
resampleFilter = 'Lanczos' |
| 129 |
-- assets/sunset.jpg -- |
| 130 |
sourcefilename: ../testdata/sunset.jpg |
| 131 |
-- assets/mask.png -- |
| 132 |
sourcefilename: ../testdata/mask.png |
| 133 |
|
| 134 |
-- layouts/home.html -- |
| 135 |
Home. |
| 136 |
{{ $sunset := resources.Get "sunset.jpg" }} |
| 137 |
{{ $mask := resources.Get "mask.png" }} |
| 138 |
|
| 139 |
{{ template "mask" (dict "name" "transparant.png" "base" $sunset "mask" $mask) }} |
| 140 |
{{ template "mask" (dict "name" "yellow.jpg" "base" $sunset "mask" $mask) }} |
| 141 |
{{ template "mask" (dict "name" "wide.jpg" "base" $sunset "mask" $mask "spec" "resize 600x200") }} |
| 142 |
{{/* This looks a little odd, but is correct and the recommended way to do this. |
| 143 |
This will 1. Scale the image to x300, 2. Apply the mask, 3. Create the final image with background color #323ea. |
| 144 |
It's possible to have multiple images.Process filters in the chain, but for the options for the final image (target format, bgGolor etc.), |
| 145 |
the last entry will win. |
| 146 |
*/}} |
| 147 |
{{ template "mask" (dict "name" "blue.jpg" "base" $sunset "mask" $mask "spec" "resize x300 #323ea8") }} |
| 148 |
|
| 149 |
{{ define "mask"}} |
| 150 |
{{ $ext := path.Ext .name }} |
| 151 |
{{ if lt (len (path.Ext .name)) 4 }} |
| 152 |
{{ errorf "No extension in %q" .name }} |
| 153 |
{{ end }} |
| 154 |
{{ $format := strings.TrimPrefix "." $ext }} |
| 155 |
{{ $spec := .spec | default (printf "resize x300 %s" $format) }} |
| 156 |
{{ $filters := slice (images.Process $spec) (images.Mask .mask) }} |
| 157 |
{{ $name := printf "images/%s" .name }} |
| 158 |
{{ $img := .base.Filter $filters }} |
| 159 |
{{ with $img | resources.Copy $name }} |
| 160 |
{{ .Publish }} |
| 161 |
{{ end }} |
| 162 |
{{ end }} |
| 163 |
` |
| 164 |
|
| 165 |
opts := imagetesting.DefaultGoldenOpts |
| 166 |
opts.T = t |
| 167 |
opts.Name = name |
| 168 |
opts.Files = files |
| 169 |
|
| 170 |
imagetesting.RunGolden(opts) |
| 171 |
} |
| 172 |
|
| 173 |
// Issue 13272, 13273. |
| 174 |
func TestImagesGoldenFiltersMaskCacheIssues(t *testing.T) { |
| 175 |
if imagetesting.SkipGoldenTests { |
| 176 |
t.Skip("Skip golden test on this architecture") |
| 177 |
} |
| 178 |
|
| 179 |
// Will be used as the base folder for generated images. |
| 180 |
name := "filters/mask2" |
| 181 |
|
| 182 |
files := ` |
| 183 |
-- hugo.toml -- |
| 184 |
[caches] |
| 185 |
[caches.images] |
| 186 |
dir = ':cacheDir/golden_images' |
| 187 |
maxAge = "30s" |
| 188 |
[imaging] |
| 189 |
bgColor = '#33ff44' |
| 190 |
hint = 'photo' |
| 191 |
quality = 75 |
| 192 |
resampleFilter = 'Lanczos' |
| 193 |
-- assets/sunset.jpg -- |
| 194 |
sourcefilename: ../testdata/sunset.jpg |
| 195 |
-- assets/mask.png -- |
| 196 |
sourcefilename: ../testdata/mask.png |
| 197 |
|
| 198 |
-- layouts/home.html -- |
| 199 |
Home. |
| 200 |
{{ $sunset := resources.Get "sunset.jpg" }} |
| 201 |
{{ $mask := resources.Get "mask.png" }} |
| 202 |
|
| 203 |
|
| 204 |
{{ template "mask" (dict "name" "green.jpg" "base" $sunset "mask" $mask) }} |
| 205 |
|
| 206 |
{{ define "mask"}} |
| 207 |
{{ $ext := path.Ext .name }} |
| 208 |
{{ if lt (len (path.Ext .name)) 4 }} |
| 209 |
{{ errorf "No extension in %q" .name }} |
| 210 |
{{ end }} |
| 211 |
{{ $format := strings.TrimPrefix "." $ext }} |
| 212 |
{{ $spec := .spec | default (printf "resize x300 %s" $format) }} |
| 213 |
{{ $filters := slice (images.Process $spec) (images.Mask .mask) }} |
| 214 |
{{ $name := printf "images/%s" .name }} |
| 215 |
{{ $img := .base.Filter $filters }} |
| 216 |
{{ with $img | resources.Copy $name }} |
| 217 |
{{ .Publish }} |
| 218 |
{{ end }} |
| 219 |
{{ end }} |
| 220 |
` |
| 221 |
|
| 222 |
tempDir := t.TempDir() |
| 223 |
|
| 224 |
opts := imagetesting.DefaultGoldenOpts |
| 225 |
opts.WorkingDir = tempDir |
| 226 |
opts.T = t |
| 227 |
opts.Name = name |
| 228 |
opts.Files = files |
| 229 |
opts.SkipAssertions = true |
| 230 |
|
| 231 |
imagetesting.RunGolden(opts) |
| 232 |
|
| 233 |
files = strings.Replace(files, "#33ff44", "#a83269", -1) |
| 234 |
files = strings.Replace(files, "green", "pink", -1) |
| 235 |
files = strings.Replace(files, "mask.png", "mask2.png", -1) |
| 236 |
opts.Files = files |
| 237 |
opts.SkipAssertions = false |
| 238 |
opts.Rebuild = true |
| 239 |
|
| 240 |
imagetesting.RunGolden(opts) |
| 241 |
} |
| 242 |
|
| 243 |
// The padding color must survive the trip through an alpha-premultiplied |
| 244 |
// destination image; the two images below should look the same. |
| 245 |
// See issue 12536. |
| 246 |
func TestImagesGoldenFiltersPaddingTransparentColor(t *testing.T) { |
| 247 |
t.Parallel() |
| 248 |
|
| 249 |
if imagetesting.SkipGoldenTests { |
| 250 |
t.Skip("Skip golden test on this architecture") |
| 251 |
} |
| 252 |
|
| 253 |
// Will be used as the base folder for generated images. |
| 254 |
name := "filters/padding" |
| 255 |
|
| 256 |
files := ` |
| 257 |
-- hugo.toml -- |
| 258 |
-- assets/sunset.jpg -- |
| 259 |
sourcefilename: ../testdata/sunset.jpg |
| 260 |
-- layouts/home.html -- |
| 261 |
Home. |
| 262 |
{{ $sunset := resources.Get "sunset.jpg" }} |
| 263 |
{{ $onepass := $sunset.Filter (images.Process "resize x200 png") (images.Padding 20 "#00f7") }} |
| 264 |
{{ $chained := ($sunset.Resize "x200 png").Filter (images.Padding 20 "#00f7") }} |
| 265 |
{{ with $onepass | resources.Copy "images/padding-transparent-onepass.png" }}{{ .Publish }}{{ end }} |
| 266 |
{{ with $chained | resources.Copy "images/padding-transparent-chained.png" }}{{ .Publish }}{{ end }} |
| 267 |
` |
| 268 |
|
| 269 |
opts := imagetesting.DefaultGoldenOpts |
| 270 |
opts.T = t |
| 271 |
opts.Name = name |
| 272 |
opts.Files = files |
| 273 |
|
| 274 |
imagetesting.RunGolden(opts) |
| 275 |
} |
| 276 |
|
| 277 |
// Filtering an indexed PNG must not clamp the result to the source palette. |
| 278 |
// See issue 12543. |
| 279 |
func TestImagesGoldenFiltersPaletted(t *testing.T) { |
| 280 |
t.Parallel() |
| 281 |
|
| 282 |
if imagetesting.SkipGoldenTests { |
| 283 |
t.Skip("Skip golden test on this architecture") |
| 284 |
} |
| 285 |
|
| 286 |
// Will be used as the base folder for generated images. |
| 287 |
name := "filters/paletted" |
| 288 |
|
| 289 |
files := ` |
| 290 |
-- hugo.toml -- |
| 291 |
-- assets/gohugoio8.png -- |
| 292 |
sourcefilename: ../testdata/gohugoio8.png |
| 293 |
-- assets/sunset.jpg -- |
| 294 |
sourcefilename: ../testdata/sunset.jpg |
| 295 |
-- assets/card.gif -- |
| 296 |
sourcefilename: ../testdata/gohugoio-card.gif |
| 297 |
-- layouts/home.html -- |
| 298 |
Home. |
| 299 |
{{ $bg := (resources.Get "gohugoio8.png").Resize "500x" }} |
| 300 |
{{ $sunset := (resources.Get "sunset.jpg").Resize "x160" }} |
| 301 |
{{ $card := resources.Get "card.gif" }} |
| 302 |
{{ $textOpts := dict "color" "#ff0060" "size" 40 "x" 190 "y" 80 }} |
| 303 |
|
| 304 |
{{ $overlay := $bg.Filter (images.Overlay $sunset 230 70) }} |
| 305 |
{{ $text := $bg.Filter (images.Text "Hugo Rocks!" $textOpts) }} |
| 306 |
{{/* Chains of geometric filters only; these should preserve the source palette. */}} |
| 307 |
{{ $processed := $bg.Filter (images.Process "crop 300x150 TopRight png") images.AutoOrient }} |
| 308 |
{{ $gifcrop := $card.Filter (images.Process "crop 100x50 TopRight png") }} |
| 309 |
{{ with $overlay | resources.Copy "images/overlay.png" }}{{ .Publish }}{{ end }} |
| 310 |
{{ with $text | resources.Copy "images/text.png" }}{{ .Publish }}{{ end }} |
| 311 |
{{ with $processed | resources.Copy "images/process-autoorient.png" }}{{ .Publish }}{{ end }} |
| 312 |
{{ with $gifcrop | resources.Copy "images/gif-process.png" }}{{ .Publish }}{{ end }} |
| 313 |
` |
| 314 |
|
| 315 |
opts := imagetesting.DefaultGoldenOpts |
| 316 |
opts.T = t |
| 317 |
opts.Name = name |
| 318 |
opts.Files = files |
| 319 |
|
| 320 |
imagetesting.RunGolden(opts) |
| 321 |
} |
| 322 |
|
| 323 |
func TestImagesGoldenFiltersText(t *testing.T) { |
| 324 |
t.Parallel() |
| 325 |
|
| 326 |
if imagetesting.SkipGoldenTests { |
| 327 |
t.Skip("Skip golden test on this architecture") |
| 328 |
} |
| 329 |
|
| 330 |
// Will be used as the base folder for generated images. |
| 331 |
name := "filters/text" |
| 332 |
|
| 333 |
files := ` |
| 334 |
-- hugo.toml -- |
| 335 |
-- assets/sunset.jpg -- |
| 336 |
sourcefilename: ../testdata/sunset.jpg |
| 337 |
|
| 338 |
-- layouts/home.html -- |
| 339 |
Home. |
| 340 |
{{ $sunset := resources.Get "sunset.jpg" }} |
| 341 |
{{ $textOpts := dict |
| 342 |
"color" "#fbfaf5" |
| 343 |
"linespacing" 8 |
| 344 |
"size" 28 |
| 345 |
"x" (div $sunset.Width 2 | int) |
| 346 |
"y" (div $sunset.Height 2 | int) |
| 347 |
"alignx" "center" |
| 348 |
}} |
| 349 |
|
| 350 |
{{ $text := "Pariatur deserunt sunt nisi sunt tempor quis eu. Sint et nulla enim officia sunt cupidatat. Eu amet ipsum qui velit cillum cillum ad Lorem in non ad aute." }} |
| 351 |
{{ template "filters" (dict "name" "text_alignx-center.jpg" "img" $sunset "filters" (images.Text $text $textOpts )) }} |
| 352 |
{{ $textOpts = (dict "alignx" "right") | merge $textOpts }} |
| 353 |
{{ template "filters" (dict "name" "text_alignx-right.jpg" "img" $sunset "filters" (images.Text $text $textOpts )) }} |
| 354 |
{{ $textOpts = (dict "alignx" "left") | merge $textOpts }} |
| 355 |
{{ template "filters" (dict "name" "text_alignx-left.jpg" "img" $sunset "filters" (images.Text $text $textOpts )) }} |
| 356 |
{{ $textOpts = (dict "alignx" "center" "aligny" "center") | merge $textOpts }} |
| 357 |
{{ $text = "Est exercitation deserunt exercitation nostrud magna. Eiusmod anim deserunt sit elit dolore ea incididunt nisi. Ea ullamco excepteur voluptate occaecat duis pariatur proident cupidatat. Eu id esse qui consectetur commodo ad ex esse cupidatat velit duis cupidatat. Aliquip irure tempor consequat non amet in mollit ipsum officia tempor laborum." }} |
| 358 |
{{ template "filters" (dict "name" "text_alignx-center_aligny-center.jpg" "img" $sunset "filters" (images.Text $text $textOpts )) }} |
| 359 |
{{ $textOpts = (dict "alignx" "center" "aligny" "bottom") | merge $textOpts }} |
| 360 |
{{ template "filters" (dict "name" "text_alignx-center_aligny-bottom.jpg" "img" $sunset "filters" (images.Text $text $textOpts )) }} |
| 361 |
|
| 362 |
{{ define "filters"}} |
| 363 |
{{ if lt (len (path.Ext .name)) 4 }} |
| 364 |
{{ errorf "No extension in %q" .name }} |
| 365 |
{{ end }} |
| 366 |
{{ $img := .img.Filter .filters }} |
| 367 |
{{ $name := printf "images/%s" .name }} |
| 368 |
{{ with $img | resources.Copy $name }} |
| 369 |
{{ .Publish }} |
| 370 |
{{ end }} |
| 371 |
{{ end }} |
| 372 |
` |
| 373 |
|
| 374 |
opts := imagetesting.DefaultGoldenOpts |
| 375 |
opts.T = t |
| 376 |
opts.Name = name |
| 377 |
opts.Files = files |
| 378 |
|
| 379 |
imagetesting.RunGolden(opts) |
| 380 |
} |
| 381 |
|
| 382 |
func TestImagesGoldenProcessMisc(t *testing.T) { |
| 383 |
t.Parallel() |
| 384 |
|
| 385 |
if imagetesting.SkipGoldenTests { |
| 386 |
t.Skip("Skip golden test on this architecture") |
| 387 |
} |
| 388 |
|
| 389 |
// Will be used as the base folder for generated images. |
| 390 |
name := "process/misc" |
| 391 |
|
| 392 |
files := ` |
| 393 |
-- hugo.toml -- |
| 394 |
-- assets/giphy.gif -- |
| 395 |
sourcefilename: ../testdata/giphy.gif |
| 396 |
-- assets/sunset.jpg -- |
| 397 |
sourcefilename: ../testdata/sunset.jpg |
| 398 |
-- assets/gopher.png -- |
| 399 |
sourcefilename: ../testdata/gopher-hero8.png |
| 400 |
-- layouts/home.html -- |
| 401 |
Home. |
| 402 |
{{ $sunset := resources.Get "sunset.jpg" }} |
| 403 |
{{ $sunsetGrayscale := $sunset.Filter (images.Grayscale) }} |
| 404 |
{{ $gopher := resources.Get "gopher.png" }} |
| 405 |
{{ $giphy := resources.Get "giphy.gif" }} |
| 406 |
|
| 407 |
|
| 408 |
{{/* These are sorted. The end file name will be created from the spec + extension, so make sure these are unique. */}} |
| 409 |
{{ template "process" (dict "spec" "crop 500x200 smart" "img" $sunset) }} |
| 410 |
{{ template "process" (dict "spec" "fill 500x200 smart" "img" $sunset) }} |
| 411 |
{{ template "process" (dict "spec" "fit 500x200 smart" "img" $sunset) }} |
| 412 |
{{ template "process" (dict "spec" "resize 100x100 gif" "img" $giphy) }} |
| 413 |
{{ template "process" (dict "spec" "resize 100x100 r180" "img" $gopher) }} |
| 414 |
{{ template "process" (dict "spec" "resize 300x300 jpg #b31280" "img" $gopher) }} |
| 415 |
|
| 416 |
|
| 417 |
` + goldenProcess |
| 418 |
|
| 419 |
opts := imagetesting.DefaultGoldenOpts |
| 420 |
opts.T = t |
| 421 |
opts.Name = name |
| 422 |
opts.Files = files |
| 423 |
|
| 424 |
imagetesting.RunGolden(opts) |
| 425 |
} |
| 426 |
|
| 427 |
func TestImagesGoldenProcessAvif(t *testing.T) { |
| 428 |
t.Parallel() |
| 429 |
|
| 430 |
if imagetesting.SkipGoldenTests { |
| 431 |
t.Skip("Skip golden test on this architecture") |
| 432 |
} |
| 433 |
|
| 434 |
// Will be used as the base folder for generated images. |
| 435 |
name := "process/avif" |
| 436 |
|
| 437 |
files := ` |
| 438 |
-- hugo.toml -- |
| 439 |
-- assets/anim.webp -- |
| 440 |
sourcefilename: ../testdata/webp/anim.webp |
| 441 |
-- assets/dock.avif -- |
| 442 |
sourcefilename: ../testdata/bep/dock-75-hdr.avif |
| 443 |
-- assets/sunset420.avif -- |
| 444 |
sourcefilename: ../testdata/sunset_420.avif |
| 445 |
-- assets/sunset.jpg -- |
| 446 |
sourcefilename: ../testdata/sunset.jpg |
| 447 |
-- assets/giphy.avif -- |
| 448 |
sourcefilename: ../testdata/giphy.avif |
| 449 |
-- assets/fuzzycircle.webp -- |
| 450 |
sourcefilename: ../testdata/webp/fuzzy-cirlcle-transparent-32.webp |
| 451 |
-- assets/gohugoio8.png -- |
| 452 |
sourcefilename: ../testdata/gohugoio8.png |
| 453 |
-- assets/gohugoio24.png -- |
| 454 |
sourcefilename: ../testdata/gohugoio24.png |
| 455 |
-- layouts/home.html -- |
| 456 |
Home. |
| 457 |
{{ $sunset := resources.Get "sunset.jpg" }} |
| 458 |
{{ $dock := resources.Get "dock.avif" }} |
| 459 |
{{ $sunset420 := resources.Get "sunset420.avif" }} |
| 460 |
{{ $webpAnim := resources.Get "anim.webp" }} |
| 461 |
{{ $giphy := resources.Get "giphy.avif" }} |
| 462 |
{{ $fuzzyCircle := resources.Get "fuzzycircle.webp" }} |
| 463 |
{{ $gohugoio8 := resources.Get "gohugoio8.png" }} |
| 464 |
{{ $gohugoio24 := resources.Get "gohugoio24.png" }} |
| 465 |
|
| 466 |
{{ template "process" (dict "spec" "r1" "img" $dock) }} |
| 467 |
{{ template "process" (dict "spec" "q50" "img" $dock) }} |
| 468 |
{{ template "process" (dict "spec" "r2" "img" $sunset420) }} |
| 469 |
{{ template "process" (dict "spec" "avif" "img" $webpAnim) }} |
| 470 |
{{ template "process" (dict "spec" "gif" "img" $giphy) }} |
| 471 |
{{ template "process" (dict "spec" "crop 300x300 smart avif" "img" $fuzzyCircle) }} |
| 472 |
{{ template "process" (dict "spec" "crop 300x300 smart #ff9999 avif" "img" $fuzzyCircle) }} |
| 473 |
{{ template "process" (dict "spec" "avif q79" "img" $gohugoio8) }} |
| 474 |
{{ template "process" (dict "spec" "avif q80" "img" $gohugoio24) }} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
` + goldenProcess |
| 479 |
|
| 480 |
opts := imagetesting.DefaultGoldenOpts |
| 481 |
opts.T = t |
| 482 |
opts.Name = name |
| 483 |
opts.Files = files |
| 484 |
|
| 485 |
imagetesting.RunGolden(opts) |
| 486 |
} |
| 487 |
|
| 488 |
func TestImagesGoldenProcessAviStraws(t *testing.T) { |
| 489 |
t.Parallel() |
| 490 |
|
| 491 |
if imagetesting.SkipGoldenTests { |
| 492 |
t.Skip("Skip golden test on this architecture") |
| 493 |
} |
| 494 |
|
| 495 |
// Will be used as the base folder for generated images. |
| 496 |
name := "process/avifstraws" |
| 497 |
|
| 498 |
files := ` |
| 499 |
-- hugo.toml -- |
| 500 |
-- assets/straws.avif -- |
| 501 |
sourcefilename: ../testdata/bep/straws.avif |
| 502 |
-- layouts/home.html -- |
| 503 |
Home. |
| 504 |
{{ $straws := resources.Get "straws.avif" }} |
| 505 |
{{ template "process" (dict "spec" "resize 900x" "img" $straws) }} |
| 506 |
|
| 507 |
|
| 508 |
` + goldenProcess |
| 509 |
|
| 510 |
opts := imagetesting.DefaultGoldenOpts |
| 511 |
opts.T = t |
| 512 |
opts.Name = name |
| 513 |
opts.Files = files |
| 514 |
|
| 515 |
imagetesting.RunGolden(opts) |
| 516 |
} |
| 517 |
|
| 518 |
func TestImagesGoldenProcessWebP(t *testing.T) { |
| 519 |
t.Parallel() |
| 520 |
|
| 521 |
if imagetesting.SkipGoldenTests { |
| 522 |
t.Skip("Skip golden test on this architecture") |
| 523 |
} |
| 524 |
|
| 525 |
// Will be used as the base folder for generated images. |
| 526 |
name := "process/webp" |
| 527 |
|
| 528 |
files := ` |
| 529 |
-- hugo.toml -- |
| 530 |
-- assets/highcontrast.webp -- |
| 531 |
sourcefilename: ../testdata/webp/highcontrast.webp |
| 532 |
-- assets/anim.webp -- |
| 533 |
sourcefilename: ../testdata/webp/anim.webp |
| 534 |
-- assets/fuzzycircle.webp -- |
| 535 |
sourcefilename: ../testdata/webp/fuzzy-cirlcle-transparent-32.webp |
| 536 |
-- assets/fuzzycircle.png -- |
| 537 |
sourcefilename: ../testdata/fuzzy-cirlcle.png |
| 538 |
-- assets/giphy.gif -- |
| 539 |
sourcefilename: ../testdata/giphy.gif |
| 540 |
-- assets/sunset.jpg -- |
| 541 |
sourcefilename: ../testdata/sunset.jpg |
| 542 |
-- layouts/home.html -- |
| 543 |
Home. |
| 544 |
{{ $fuzzyCircle := resources.Get "fuzzycircle.webp" }} |
| 545 |
{{ $highContrast := resources.Get "highcontrast.webp" }} |
| 546 |
{{ $sunset := resources.Get "sunset.jpg" }} |
| 547 |
{{ $sunsetGrayscale := $sunset.Filter (images.Grayscale) }} |
| 548 |
{{ $animWebp := resources.Get "anim.webp" }} |
| 549 |
{{ $giphy := resources.Get "giphy.gif" }} |
| 550 |
|
| 551 |
{{/* These are sorted. The end file name will be created from the spec + extension, so make sure these are unique. */}} |
| 552 |
{{ template "process" (dict "spec" "crop 300x300 gif" "img" $animWebp) }} |
| 553 |
{{ template "process" (dict "spec" "crop 300x300 smart" "img" $fuzzyCircle) }} |
| 554 |
{{ template "process" (dict "spec" "crop 300x300 smart #ff9999" "img" $fuzzyCircle) }} |
| 555 |
{{ template "process" (dict "spec" "crop 300x300" "img" $animWebp) }} |
| 556 |
{{ template "process" (dict "spec" "crop 500x200 smart webp" "img" $sunset) }} |
| 557 |
{{ template "process" (dict "spec" "crop 500x200 smart webp" "img" $sunset) }} |
| 558 |
{{ template "process" (dict "spec" "fit 300x400 webp" "img" $sunsetGrayscale) }} |
| 559 |
{{ template "process" (dict "spec" "fit 400x500 webp" "img" $sunset) }} |
| 560 |
{{ template "process" (dict "spec" "gif" "img" $highContrast) }} |
| 561 |
{{ template "process" (dict "spec" "png" "img" $highContrast) }} |
| 562 |
{{ template "process" (dict "spec" "resize 300x300" "img" $giphy) }} |
| 563 |
{{ template "process" (dict "spec" "resize 300x300 webp" "img" $giphy) }} |
| 564 |
{{ template "process" (dict "spec" "resize 300x300 webp lossless" "img" $sunset) }} |
| 565 |
{{ template "process" (dict "spec" "resize 300x300 webp q1" "img" $sunset) }} |
| 566 |
{{ template "process" (dict "spec" "resize 300x300 webp q33" "img" $sunset) }} |
| 567 |
{{ template "process" (dict "spec" "resize 300x300 webp q75" "img" $sunset) }} |
| 568 |
{{ template "process" (dict "spec" "resize 300x300 webp q100" "img" $sunset) }} |
| 569 |
{{ template "process" (dict "spec" "resize 300x300 webp drawing" "img" $sunset) }} |
| 570 |
{{ template "process" (dict "spec" "resize 300x300 webp icon" "img" $sunset) }} |
| 571 |
{{ template "process" (dict "spec" "resize 300x300 webp q50 drawing" "img" $sunset) }} |
| 572 |
{{ template "process" (dict "spec" "resize 400x" "img" $highContrast) }} |
| 573 |
|
| 574 |
{{ define "process"}} |
| 575 |
{{ $img := .img.Process .spec }} |
| 576 |
{{ $ext := path.Ext $img.RelPermalink }} |
| 577 |
{{ $name := printf "images/%s%s" (.spec | anchorize) $ext }} |
| 578 |
{{ with $img | resources.Copy $name }} |
| 579 |
{{ .Publish }} |
| 580 |
{{ end }} |
| 581 |
{{ end }} |
| 582 |
` |
| 583 |
|
| 584 |
opts := imagetesting.DefaultGoldenOpts |
| 585 |
opts.T = t |
| 586 |
opts.Name = name |
| 587 |
opts.Files = files |
| 588 |
|
| 589 |
imagetesting.RunGolden(opts) |
| 590 |
} |
| 591 |
|
| 592 |
func TestImagesGoldenWebPAnimation(t *testing.T) { |
| 593 |
t.Parallel() |
| 594 |
|
| 595 |
if imagetesting.SkipGoldenTests { |
| 596 |
t.Skip("Skip golden test on this architecture") |
| 597 |
} |
| 598 |
|
| 599 |
// Will be used as the base folder for generated images. |
| 600 |
name := "webp/animation" |
| 601 |
|
| 602 |
files := ` |
| 603 |
-- hugo.toml -- |
| 604 |
disableKinds = ["page", "section", "taxonomy", "term", "sitemap", "robotsTXT", "404"] |
| 605 |
-- assets/images/anim.webp -- |
| 606 |
sourcefilename: ../testdata/webp/anim.webp |
| 607 |
-- assets/images/giphy.gif -- |
| 608 |
sourcefilename: ../testdata/giphy.gif |
| 609 |
-- layouts/home.html -- |
| 610 |
Home. |
| 611 |
{{ $webpAnim := resources.Get "images/anim.webp" }} |
| 612 |
{{ $gifAnim := resources.Get "images/giphy.gif" }} |
| 613 |
{{ ($webpAnim.Resize "100x100 webp").Publish }} |
| 614 |
{{ ($webpAnim.Resize "100x100 gif").Publish }} |
| 615 |
{{ ($gifAnim.Resize "100x100 gif").Publish }} |
| 616 |
{{ ($gifAnim.Resize "100x100 webp").Publish }} |
| 617 |
` |
| 618 |
|
| 619 |
opts := imagetesting.DefaultGoldenOpts |
| 620 |
opts.T = t |
| 621 |
opts.Name = name |
| 622 |
opts.Files = files |
| 623 |
|
| 624 |
imagetesting.RunGolden(opts) |
| 625 |
} |
| 626 |
|
| 627 |
func TestImagesGoldenMethods(t *testing.T) { |
| 628 |
t.Parallel() |
| 629 |
|
| 630 |
if imagetesting.SkipGoldenTests { |
| 631 |
t.Skip("Skip golden test on this architecture") |
| 632 |
} |
| 633 |
|
| 634 |
// Will be used as the base folder for generated images. |
| 635 |
name := "methods" |
| 636 |
|
| 637 |
files := ` |
| 638 |
-- hugo.toml -- |
| 639 |
[imaging] |
| 640 |
bgColor = '#ebcc34' |
| 641 |
hint = 'photo' |
| 642 |
quality = 75 |
| 643 |
resampleFilter = 'MitchellNetravali' |
| 644 |
-- assets/sunset.jpg -- |
| 645 |
sourcefilename: ../testdata/sunset.jpg |
| 646 |
-- assets/gopher.png -- |
| 647 |
sourcefilename: ../testdata/gopher-hero8.png |
| 648 |
|
| 649 |
-- layouts/home.html -- |
| 650 |
Home. |
| 651 |
{{ $sunset := resources.Get "sunset.jpg" }} |
| 652 |
{{ $gopher := resources.Get "gopher.png" }} |
| 653 |
|
| 654 |
|
| 655 |
{{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "resize" "spec" "300x" ) }} |
| 656 |
{{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "resize" "spec" "x200" ) }} |
| 657 |
{{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "fill" "spec" "90x120 left" ) }} |
| 658 |
{{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "fill" "spec" "90x120 right" ) }} |
| 659 |
{{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "fit" "spec" "200x200" ) }} |
| 660 |
{{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "200x200" ) }} |
| 661 |
{{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "350x400 center" ) }} |
| 662 |
{{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "350x400 smart" ) }} |
| 663 |
{{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "350x400 center r90" ) }} |
| 664 |
{{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "350x400 center q20" ) }} |
| 665 |
{{ template "invoke" (dict "copyFormat" "png" "base" $gopher "method" "resize" "spec" "100x" ) }} |
| 666 |
{{ template "invoke" (dict "copyFormat" "png" "base" $gopher "method" "resize" "spec" "100x #fc03ec" ) }} |
| 667 |
{{ template "invoke" (dict "copyFormat" "jpg" "base" $gopher "method" "resize" "spec" "100x #03fc56 jpg" ) }} |
| 668 |
|
| 669 |
{{ define "invoke"}} |
| 670 |
{{ $spec := .spec }} |
| 671 |
{{ $name := printf "images/%s-%s-%s.%s" .method ((trim .base.Name "/") | lower | anchorize) ($spec | anchorize) .copyFormat }} |
| 672 |
{{ $img := ""}} |
| 673 |
{{ if eq .method "resize" }} |
| 674 |
{{ $img = .base.Resize $spec }} |
| 675 |
{{ else if eq .method "fill" }} |
| 676 |
{{ $img = .base.Fill $spec }} |
| 677 |
{{ else if eq .method "fit" }} |
| 678 |
{{ $img = .base.Fit $spec }} |
| 679 |
{{ else if eq .method "crop" }} |
| 680 |
{{ $img = .base.Crop $spec }} |
| 681 |
{{ else }} |
| 682 |
{{ errorf "Unknown method %q" .method }} |
| 683 |
{{ end }} |
| 684 |
{{ with $img | resources.Copy $name }} |
| 685 |
{{ .Publish }} |
| 686 |
{{ end }} |
| 687 |
{{ end }} |
| 688 |
` |
| 689 |
|
| 690 |
opts := imagetesting.DefaultGoldenOpts |
| 691 |
opts.T = t |
| 692 |
opts.Name = name |
| 693 |
opts.Files = files |
| 694 |
|
| 695 |
imagetesting.RunGolden(opts) |
| 696 |
} |
| 697 |
|
| 698 |
func TestImagesGoldenConfigLossyVsQuality(t *testing.T) { |
| 699 |
t.Parallel() |
| 700 |
|
| 701 |
if imagetesting.SkipGoldenTests { |
| 702 |
t.Skip("Skip golden test on this architecture") |
| 703 |
} |
| 704 |
|
| 705 |
files := ` |
| 706 |
-- hugo.toml -- |
| 707 |
[imaging] |
| 708 |
quality = 90 # will only apply to jpeg in this setup. |
| 709 |
compression = "lossless" # for webp |
| 710 |
-- assets/sunset.jpg -- |
| 711 |
sourcefilename: ../testdata/sunset.jpg |
| 712 |
-- layouts/home.html -- |
| 713 |
Home. |
| 714 |
{{ $sunset := resources.Get "sunset.jpg" }} |
| 715 |
{{ template "process" (dict "spec" "resize 300x300 webp" "img" $sunset) }} |
| 716 |
{{ template "process" (dict "spec" "resize 300x300 webp lossy" "img" $sunset) }} |
| 717 |
{{ template "process" (dict "spec" "resize 300x300 jpeg" "img" $sunset) }} |
| 718 |
|
| 719 |
` + goldenProcess |
| 720 |
|
| 721 |
// Will be used as the base folder for generated images. |
| 722 |
name := "losslessvsquality" |
| 723 |
|
| 724 |
opts := imagetesting.DefaultGoldenOpts |
| 725 |
opts.T = t |
| 726 |
opts.Name = name |
| 727 |
opts.Files = files |
| 728 |
|
| 729 |
imagetesting.RunGolden(opts) |
| 730 |
} |