Debian Codegraph @

resources/images/images_golden_integration_test.go

raw
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 // See issue 11266.
428 func TestImagesGoldenProcessSmartCropRotate(t *testing.T) {
429 t.Parallel()
430
431 if imagetesting.SkipGoldenTests {
432 t.Skip("Skip golden test on this architecture")
433 }
434
435 // Will be used as the base folder for generated images.
436 name := "process/smartcrop-rotate"
437
438 files := `
439 -- hugo.toml --
440 -- assets/sunset.jpg --
441 sourcefilename: ../testdata/sunset.jpg
442 -- layouts/home.html --
443 Home.
444 {{ $landscape := resources.Get "sunset.jpg" }}
445 {{ $portrait := $landscape.Process "r90" }}
446
447 {{/* These are sorted. The end file name will be created from the spec + extension, so make sure these are unique. */}}
448 {{ template "process" (dict "spec" "crop 300x200 smart r180" "img" $landscape) }}
449 {{ template "process" (dict "spec" "crop 300x200 smart r270" "img" $landscape) }}
450 {{ template "process" (dict "spec" "crop 300x200 smart r90" "img" $landscape) }}
451 {{ template "process" (dict "spec" "fill 200x300 smart r270" "img" $portrait) }}
452 {{ template "process" (dict "spec" "fill 200x300 smart r90" "img" $portrait) }}
453 {{ template "process" (dict "spec" "fill 300x200 smart r180" "img" $landscape) }}
454 {{ template "process" (dict "spec" "fill 300x200 smart r270" "img" $landscape) }}
455 {{ template "process" (dict "spec" "fill 300x200 smart r90" "img" $landscape) }}
456
457 ` + goldenProcess
458
459 opts := imagetesting.DefaultGoldenOpts
460 opts.T = t
461 opts.Name = name
462 opts.Files = files
463
464 imagetesting.RunGolden(opts)
465 }
466
467 func TestImagesGoldenProcessAvif(t *testing.T) {
468 t.Parallel()
469
470 if imagetesting.SkipGoldenTests {
471 t.Skip("Skip golden test on this architecture")
472 }
473
474 // Will be used as the base folder for generated images.
475 name := "process/avif"
476
477 files := `
478 -- hugo.toml --
479 -- assets/anim.webp --
480 sourcefilename: ../testdata/webp/anim.webp
481 -- assets/dock.avif --
482 sourcefilename: ../testdata/bep/dock-75-hdr.avif
483 -- assets/sunset420.avif --
484 sourcefilename: ../testdata/sunset_420.avif
485 -- assets/sunset.jpg --
486 sourcefilename: ../testdata/sunset.jpg
487 -- assets/giphy.avif --
488 sourcefilename: ../testdata/giphy.avif
489 -- assets/fuzzycircle.webp --
490 sourcefilename: ../testdata/webp/fuzzy-cirlcle-transparent-32.webp
491 -- assets/gohugoio8.png --
492 sourcefilename: ../testdata/gohugoio8.png
493 -- assets/gohugoio24.png --
494 sourcefilename: ../testdata/gohugoio24.png
495 -- layouts/home.html --
496 Home.
497 {{ $sunset := resources.Get "sunset.jpg" }}
498 {{ $dock := resources.Get "dock.avif" }}
499 {{ $sunset420 := resources.Get "sunset420.avif" }}
500 {{ $webpAnim := resources.Get "anim.webp" }}
501 {{ $giphy := resources.Get "giphy.avif" }}
502 {{ $fuzzyCircle := resources.Get "fuzzycircle.webp" }}
503 {{ $gohugoio8 := resources.Get "gohugoio8.png" }}
504 {{ $gohugoio24 := resources.Get "gohugoio24.png" }}
505
506 {{ template "process" (dict "spec" "r1" "img" $dock) }}
507 {{ template "process" (dict "spec" "q50" "img" $dock) }}
508 {{ template "process" (dict "spec" "r2" "img" $sunset420) }}
509 {{ template "process" (dict "spec" "avif" "img" $webpAnim) }}
510 {{ template "process" (dict "spec" "gif" "img" $giphy) }}
511 {{ template "process" (dict "spec" "crop 300x300 smart avif" "img" $fuzzyCircle) }}
512 {{ template "process" (dict "spec" "crop 300x300 smart #ff9999 avif" "img" $fuzzyCircle) }}
513 {{ template "process" (dict "spec" "avif q79" "img" $gohugoio8) }}
514 {{ template "process" (dict "spec" "avif q80" "img" $gohugoio24) }}
515
516
517
518 ` + goldenProcess
519
520 opts := imagetesting.DefaultGoldenOpts
521 opts.T = t
522 opts.Name = name
523 opts.Files = files
524
525 imagetesting.RunGolden(opts)
526 }
527
528 func TestImagesGoldenProcessAviStraws(t *testing.T) {
529 t.Parallel()
530
531 if imagetesting.SkipGoldenTests {
532 t.Skip("Skip golden test on this architecture")
533 }
534
535 // Will be used as the base folder for generated images.
536 name := "process/avifstraws"
537
538 files := `
539 -- hugo.toml --
540 -- assets/straws.avif --
541 sourcefilename: ../testdata/bep/straws.avif
542 -- layouts/home.html --
543 Home.
544 {{ $straws := resources.Get "straws.avif" }}
545 {{ template "process" (dict "spec" "resize 900x" "img" $straws) }}
546
547
548 ` + goldenProcess
549
550 opts := imagetesting.DefaultGoldenOpts
551 opts.T = t
552 opts.Name = name
553 opts.Files = files
554
555 imagetesting.RunGolden(opts)
556 }
557
558 func TestImagesGoldenProcessWebP(t *testing.T) {
559 t.Parallel()
560
561 if imagetesting.SkipGoldenTests {
562 t.Skip("Skip golden test on this architecture")
563 }
564
565 // Will be used as the base folder for generated images.
566 name := "process/webp"
567
568 files := `
569 -- hugo.toml --
570 -- assets/highcontrast.webp --
571 sourcefilename: ../testdata/webp/highcontrast.webp
572 -- assets/anim.webp --
573 sourcefilename: ../testdata/webp/anim.webp
574 -- assets/fuzzycircle.webp --
575 sourcefilename: ../testdata/webp/fuzzy-cirlcle-transparent-32.webp
576 -- assets/fuzzycircle.png --
577 sourcefilename: ../testdata/fuzzy-cirlcle.png
578 -- assets/giphy.gif --
579 sourcefilename: ../testdata/giphy.gif
580 -- assets/sunset.jpg --
581 sourcefilename: ../testdata/sunset.jpg
582 -- layouts/home.html --
583 Home.
584 {{ $fuzzyCircle := resources.Get "fuzzycircle.webp" }}
585 {{ $highContrast := resources.Get "highcontrast.webp" }}
586 {{ $sunset := resources.Get "sunset.jpg" }}
587 {{ $sunsetGrayscale := $sunset.Filter (images.Grayscale) }}
588 {{ $animWebp := resources.Get "anim.webp" }}
589 {{ $giphy := resources.Get "giphy.gif" }}
590
591 {{/* These are sorted. The end file name will be created from the spec + extension, so make sure these are unique. */}}
592 {{ template "process" (dict "spec" "crop 300x300 gif" "img" $animWebp) }}
593 {{ template "process" (dict "spec" "crop 300x300 smart" "img" $fuzzyCircle) }}
594 {{ template "process" (dict "spec" "crop 300x300 smart #ff9999" "img" $fuzzyCircle) }}
595 {{ template "process" (dict "spec" "crop 300x300" "img" $animWebp) }}
596 {{ template "process" (dict "spec" "crop 500x200 smart webp" "img" $sunset) }}
597 {{ template "process" (dict "spec" "crop 500x200 smart webp" "img" $sunset) }}
598 {{ template "process" (dict "spec" "fit 300x400 webp" "img" $sunsetGrayscale) }}
599 {{ template "process" (dict "spec" "fit 400x500 webp" "img" $sunset) }}
600 {{ template "process" (dict "spec" "gif" "img" $highContrast) }}
601 {{ template "process" (dict "spec" "png" "img" $highContrast) }}
602 {{ template "process" (dict "spec" "resize 300x300" "img" $giphy) }}
603 {{ template "process" (dict "spec" "resize 300x300 webp" "img" $giphy) }}
604 {{ template "process" (dict "spec" "resize 300x300 webp lossless" "img" $sunset) }}
605 {{ template "process" (dict "spec" "resize 300x300 webp q1" "img" $sunset) }}
606 {{ template "process" (dict "spec" "resize 300x300 webp q33" "img" $sunset) }}
607 {{ template "process" (dict "spec" "resize 300x300 webp q75" "img" $sunset) }}
608 {{ template "process" (dict "spec" "resize 300x300 webp q100" "img" $sunset) }}
609 {{ template "process" (dict "spec" "resize 300x300 webp drawing" "img" $sunset) }}
610 {{ template "process" (dict "spec" "resize 300x300 webp icon" "img" $sunset) }}
611 {{ template "process" (dict "spec" "resize 300x300 webp q50 drawing" "img" $sunset) }}
612 {{ template "process" (dict "spec" "resize 400x" "img" $highContrast) }}
613
614 {{ define "process"}}
615 {{ $img := .img.Process .spec }}
616 {{ $ext := path.Ext $img.RelPermalink }}
617 {{ $name := printf "images/%s%s" (.spec | anchorize) $ext }}
618 {{ with $img | resources.Copy $name }}
619 {{ .Publish }}
620 {{ end }}
621 {{ end }}
622 `
623
624 opts := imagetesting.DefaultGoldenOpts
625 opts.T = t
626 opts.Name = name
627 opts.Files = files
628
629 imagetesting.RunGolden(opts)
630 }
631
632 func TestImagesGoldenWebPAnimation(t *testing.T) {
633 t.Parallel()
634
635 if imagetesting.SkipGoldenTests {
636 t.Skip("Skip golden test on this architecture")
637 }
638
639 // Will be used as the base folder for generated images.
640 name := "webp/animation"
641
642 files := `
643 -- hugo.toml --
644 disableKinds = ["page", "section", "taxonomy", "term", "sitemap", "robotsTXT", "404"]
645 -- assets/images/anim.webp --
646 sourcefilename: ../testdata/webp/anim.webp
647 -- assets/images/giphy.gif --
648 sourcefilename: ../testdata/giphy.gif
649 -- layouts/home.html --
650 Home.
651 {{ $webpAnim := resources.Get "images/anim.webp" }}
652 {{ $gifAnim := resources.Get "images/giphy.gif" }}
653 {{ ($webpAnim.Resize "100x100 webp").Publish }}
654 {{ ($webpAnim.Resize "100x100 gif").Publish }}
655 {{ ($gifAnim.Resize "100x100 gif").Publish }}
656 {{ ($gifAnim.Resize "100x100 webp").Publish }}
657 `
658
659 opts := imagetesting.DefaultGoldenOpts
660 opts.T = t
661 opts.Name = name
662 opts.Files = files
663
664 imagetesting.RunGolden(opts)
665 }
666
667 func TestImagesGoldenMethods(t *testing.T) {
668 t.Parallel()
669
670 if imagetesting.SkipGoldenTests {
671 t.Skip("Skip golden test on this architecture")
672 }
673
674 // Will be used as the base folder for generated images.
675 name := "methods"
676
677 files := `
678 -- hugo.toml --
679 [imaging]
680 bgColor = '#ebcc34'
681 hint = 'photo'
682 quality = 75
683 resampleFilter = 'MitchellNetravali'
684 -- assets/sunset.jpg --
685 sourcefilename: ../testdata/sunset.jpg
686 -- assets/gopher.png --
687 sourcefilename: ../testdata/gopher-hero8.png
688
689 -- layouts/home.html --
690 Home.
691 {{ $sunset := resources.Get "sunset.jpg" }}
692 {{ $gopher := resources.Get "gopher.png" }}
693
694
695 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "resize" "spec" "300x" ) }}
696 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "resize" "spec" "x200" ) }}
697 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "fill" "spec" "90x120 left" ) }}
698 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "fill" "spec" "90x120 right" ) }}
699 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "fit" "spec" "200x200" ) }}
700 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "200x200" ) }}
701 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "350x400 center" ) }}
702 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "350x400 smart" ) }}
703 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "350x400 center r90" ) }}
704 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "350x400 center q20" ) }}
705 {{ template "invoke" (dict "copyFormat" "png" "base" $gopher "method" "resize" "spec" "100x" ) }}
706 {{ template "invoke" (dict "copyFormat" "png" "base" $gopher "method" "resize" "spec" "100x #fc03ec" ) }}
707 {{ template "invoke" (dict "copyFormat" "jpg" "base" $gopher "method" "resize" "spec" "100x #03fc56 jpg" ) }}
708
709 {{ define "invoke"}}
710 {{ $spec := .spec }}
711 {{ $name := printf "images/%s-%s-%s.%s" .method ((trim .base.Name "/") | lower | anchorize) ($spec | anchorize) .copyFormat }}
712 {{ $img := ""}}
713 {{ if eq .method "resize" }}
714 {{ $img = .base.Resize $spec }}
715 {{ else if eq .method "fill" }}
716 {{ $img = .base.Fill $spec }}
717 {{ else if eq .method "fit" }}
718 {{ $img = .base.Fit $spec }}
719 {{ else if eq .method "crop" }}
720 {{ $img = .base.Crop $spec }}
721 {{ else }}
722 {{ errorf "Unknown method %q" .method }}
723 {{ end }}
724 {{ with $img | resources.Copy $name }}
725 {{ .Publish }}
726 {{ end }}
727 {{ end }}
728 `
729
730 opts := imagetesting.DefaultGoldenOpts
731 opts.T = t
732 opts.Name = name
733 opts.Files = files
734
735 imagetesting.RunGolden(opts)
736 }
737
738 func TestImagesGoldenConfigLossyVsQuality(t *testing.T) {
739 t.Parallel()
740
741 if imagetesting.SkipGoldenTests {
742 t.Skip("Skip golden test on this architecture")
743 }
744
745 files := `
746 -- hugo.toml --
747 [imaging]
748 quality = 90 # will only apply to jpeg in this setup.
749 compression = "lossless" # for webp
750 -- assets/sunset.jpg --
751 sourcefilename: ../testdata/sunset.jpg
752 -- layouts/home.html --
753 Home.
754 {{ $sunset := resources.Get "sunset.jpg" }}
755 {{ template "process" (dict "spec" "resize 300x300 webp" "img" $sunset) }}
756 {{ template "process" (dict "spec" "resize 300x300 webp lossy" "img" $sunset) }}
757 {{ template "process" (dict "spec" "resize 300x300 jpeg" "img" $sunset) }}
758
759 ` + goldenProcess
760
761 // Will be used as the base folder for generated images.
762 name := "losslessvsquality"
763
764 opts := imagetesting.DefaultGoldenOpts
765 opts.T = t
766 opts.Name = name
767 opts.Files = files
768
769 imagetesting.RunGolden(opts)
770 }