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 // Note, if you're enabling writeGoldenFiles on a MacOS ARM 64 you need to run the test with GOARCH=amd64, e.g.
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 func TestImagesGoldenFiltersText(t *testing.T) {
244 t.Parallel()
245
246 if imagetesting.SkipGoldenTests {
247 t.Skip("Skip golden test on this architecture")
248 }
249
250 // Will be used as the base folder for generated images.
251 name := "filters/text"
252
253 files := `
254 -- hugo.toml --
255 -- assets/sunset.jpg --
256 sourcefilename: ../testdata/sunset.jpg
257
258 -- layouts/home.html --
259 Home.
260 {{ $sunset := resources.Get "sunset.jpg" }}
261 {{ $textOpts := dict
262 "color" "#fbfaf5"
263 "linespacing" 8
264 "size" 28
265 "x" (div $sunset.Width 2 | int)
266 "y" (div $sunset.Height 2 | int)
267 "alignx" "center"
268 }}
269
270 {{ $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." }}
271 {{ template "filters" (dict "name" "text_alignx-center.jpg" "img" $sunset "filters" (images.Text $text $textOpts )) }}
272 {{ $textOpts = (dict "alignx" "right") | merge $textOpts }}
273 {{ template "filters" (dict "name" "text_alignx-right.jpg" "img" $sunset "filters" (images.Text $text $textOpts )) }}
274 {{ $textOpts = (dict "alignx" "left") | merge $textOpts }}
275 {{ template "filters" (dict "name" "text_alignx-left.jpg" "img" $sunset "filters" (images.Text $text $textOpts )) }}
276 {{ $textOpts = (dict "alignx" "center" "aligny" "center") | merge $textOpts }}
277 {{ $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." }}
278 {{ template "filters" (dict "name" "text_alignx-center_aligny-center.jpg" "img" $sunset "filters" (images.Text $text $textOpts )) }}
279 {{ $textOpts = (dict "alignx" "center" "aligny" "bottom") | merge $textOpts }}
280 {{ template "filters" (dict "name" "text_alignx-center_aligny-bottom.jpg" "img" $sunset "filters" (images.Text $text $textOpts )) }}
281
282 {{ define "filters"}}
283 {{ if lt (len (path.Ext .name)) 4 }}
284 {{ errorf "No extension in %q" .name }}
285 {{ end }}
286 {{ $img := .img.Filter .filters }}
287 {{ $name := printf "images/%s" .name }}
288 {{ with $img | resources.Copy $name }}
289 {{ .Publish }}
290 {{ end }}
291 {{ end }}
292 `
293
294 opts := imagetesting.DefaultGoldenOpts
295 opts.T = t
296 opts.Name = name
297 opts.Files = files
298 // opts.WriteFiles = true
299 // opts.DevMode = true
300
301 imagetesting.RunGolden(opts)
302 }
303
304 func TestImagesGoldenProcessMisc(t *testing.T) {
305 t.Parallel()
306
307 if imagetesting.SkipGoldenTests {
308 t.Skip("Skip golden test on this architecture")
309 }
310
311 // Will be used as the base folder for generated images.
312 name := "process/misc"
313
314 files := `
315 -- hugo.toml --
316 -- assets/giphy.gif --
317 sourcefilename: ../testdata/giphy.gif
318 -- assets/sunset.jpg --
319 sourcefilename: ../testdata/sunset.jpg
320 -- assets/gopher.png --
321 sourcefilename: ../testdata/gopher-hero8.png
322 -- layouts/home.html --
323 Home.
324 {{ $sunset := resources.Get "sunset.jpg" }}
325 {{ $sunsetGrayscale := $sunset.Filter (images.Grayscale) }}
326 {{ $gopher := resources.Get "gopher.png" }}
327 {{ $giphy := resources.Get "giphy.gif" }}
328
329
330 {{/* These are sorted. The end file name will be created from the spec + extension, so make sure these are unique. */}}
331 {{ template "process" (dict "spec" "crop 500x200 smart" "img" $sunset) }}
332 {{ template "process" (dict "spec" "fill 500x200 smart" "img" $sunset) }}
333 {{ template "process" (dict "spec" "fit 500x200 smart" "img" $sunset) }}
334 {{ template "process" (dict "spec" "resize 100x100 gif" "img" $giphy) }}
335 {{ template "process" (dict "spec" "resize 100x100 r180" "img" $gopher) }}
336 {{ template "process" (dict "spec" "resize 300x300 jpg #b31280" "img" $gopher) }}
337
338
339 ` + goldenProcess
340
341 opts := imagetesting.DefaultGoldenOpts
342 opts.T = t
343 opts.Name = name
344 opts.Files = files
345
346 imagetesting.RunGolden(opts)
347 }
348
349 func TestImagesGoldenProcessAvif(t *testing.T) {
350 t.Parallel()
351
352 if imagetesting.SkipGoldenTests {
353 t.Skip("Skip golden test on this architecture")
354 }
355
356 // Will be used as the base folder for generated images.
357 name := "process/avif"
358
359 files := `
360 -- hugo.toml --
361 -- assets/anim.webp --
362 sourcefilename: ../testdata/webp/anim.webp
363 -- assets/dock.avif --
364 sourcefilename: ../testdata/bep/dock-75-hdr.avif
365 -- assets/sunset420.avif --
366 sourcefilename: ../testdata/sunset_420.avif
367 -- assets/sunset.jpg --
368 sourcefilename: ../testdata/sunset.jpg
369 -- assets/giphy.avif --
370 sourcefilename: ../testdata/giphy.avif
371 -- assets/fuzzycircle.webp --
372 sourcefilename: ../testdata/webp/fuzzy-cirlcle-transparent-32.webp
373 -- assets/gohugoio8.png --
374 sourcefilename: ../testdata/gohugoio8.png
375 -- assets/gohugoio24.png --
376 sourcefilename: ../testdata/gohugoio24.png
377 -- layouts/home.html --
378 Home.
379 {{ $sunset := resources.Get "sunset.jpg" }}
380 {{ $dock := resources.Get "dock.avif" }}
381 {{ $sunset420 := resources.Get "sunset420.avif" }}
382 {{ $webpAnim := resources.Get "anim.webp" }}
383 {{ $giphy := resources.Get "giphy.avif" }}
384 {{ $fuzzyCircle := resources.Get "fuzzycircle.webp" }}
385 {{ $gohugoio8 := resources.Get "gohugoio8.png" }}
386 {{ $gohugoio24 := resources.Get "gohugoio24.png" }}
387
388 {{ template "process" (dict "spec" "r1" "img" $dock) }}
389 {{ template "process" (dict "spec" "q50" "img" $dock) }}
390 {{ template "process" (dict "spec" "r2" "img" $sunset420) }}
391 {{ template "process" (dict "spec" "avif" "img" $webpAnim) }}
392 {{ template "process" (dict "spec" "gif" "img" $giphy) }}
393 {{ template "process" (dict "spec" "crop 300x300 smart avif" "img" $fuzzyCircle) }}
394 {{ template "process" (dict "spec" "crop 300x300 smart #ff9999 avif" "img" $fuzzyCircle) }}
395 {{ template "process" (dict "spec" "avif q79" "img" $gohugoio8) }}
396 {{ template "process" (dict "spec" "avif q80" "img" $gohugoio24) }}
397
398
399
400 ` + goldenProcess
401
402 opts := imagetesting.DefaultGoldenOpts
403 opts.T = t
404 opts.Name = name
405 opts.Files = files
406
407 imagetesting.RunGolden(opts)
408 }
409
410 func TestImagesGoldenProcessAviStraws(t *testing.T) {
411 t.Parallel()
412
413 if imagetesting.SkipGoldenTests {
414 t.Skip("Skip golden test on this architecture")
415 }
416
417 // Will be used as the base folder for generated images.
418 name := "process/avifstraws"
419
420 files := `
421 -- hugo.toml --
422 -- assets/straws.avif --
423 sourcefilename: ../testdata/bep/straws.avif
424 -- layouts/home.html --
425 Home.
426 {{ $straws := resources.Get "straws.avif" }}
427 {{ template "process" (dict "spec" "resize 900x" "img" $straws) }}
428
429
430 ` + goldenProcess
431
432 opts := imagetesting.DefaultGoldenOpts
433 opts.T = t
434 opts.Name = name
435 opts.Files = files
436
437 imagetesting.RunGolden(opts)
438 }
439
440 func TestImagesGoldenProcessWebP(t *testing.T) {
441 t.Parallel()
442
443 if imagetesting.SkipGoldenTests {
444 t.Skip("Skip golden test on this architecture")
445 }
446
447 // Will be used as the base folder for generated images.
448 name := "process/webp"
449
450 files := `
451 -- hugo.toml --
452 -- assets/highcontrast.webp --
453 sourcefilename: ../testdata/webp/highcontrast.webp
454 -- assets/anim.webp --
455 sourcefilename: ../testdata/webp/anim.webp
456 -- assets/fuzzycircle.webp --
457 sourcefilename: ../testdata/webp/fuzzy-cirlcle-transparent-32.webp
458 -- assets/fuzzycircle.png --
459 sourcefilename: ../testdata/fuzzy-cirlcle.png
460 -- assets/giphy.gif --
461 sourcefilename: ../testdata/giphy.gif
462 -- assets/sunset.jpg --
463 sourcefilename: ../testdata/sunset.jpg
464 -- layouts/home.html --
465 Home.
466 {{ $fuzzyCircle := resources.Get "fuzzycircle.webp" }}
467 {{ $highContrast := resources.Get "highcontrast.webp" }}
468 {{ $sunset := resources.Get "sunset.jpg" }}
469 {{ $sunsetGrayscale := $sunset.Filter (images.Grayscale) }}
470 {{ $animWebp := resources.Get "anim.webp" }}
471 {{ $giphy := resources.Get "giphy.gif" }}
472
473 {{/* These are sorted. The end file name will be created from the spec + extension, so make sure these are unique. */}}
474 {{ template "process" (dict "spec" "crop 300x300 gif" "img" $animWebp) }}
475 {{ template "process" (dict "spec" "crop 300x300 smart" "img" $fuzzyCircle) }}
476 {{ template "process" (dict "spec" "crop 300x300 smart #ff9999" "img" $fuzzyCircle) }}
477 {{ template "process" (dict "spec" "crop 300x300" "img" $animWebp) }}
478 {{ template "process" (dict "spec" "crop 500x200 smart webp" "img" $sunset) }}
479 {{ template "process" (dict "spec" "crop 500x200 smart webp" "img" $sunset) }}
480 {{ template "process" (dict "spec" "fit 300x400 webp" "img" $sunsetGrayscale) }}
481 {{ template "process" (dict "spec" "fit 400x500 webp" "img" $sunset) }}
482 {{ template "process" (dict "spec" "gif" "img" $highContrast) }}
483 {{ template "process" (dict "spec" "png" "img" $highContrast) }}
484 {{ template "process" (dict "spec" "resize 300x300" "img" $giphy) }}
485 {{ template "process" (dict "spec" "resize 300x300 webp" "img" $giphy) }}
486 {{ template "process" (dict "spec" "resize 300x300 webp lossless" "img" $sunset) }}
487 {{ template "process" (dict "spec" "resize 300x300 webp q1" "img" $sunset) }}
488 {{ template "process" (dict "spec" "resize 300x300 webp q33" "img" $sunset) }}
489 {{ template "process" (dict "spec" "resize 300x300 webp q75" "img" $sunset) }}
490 {{ template "process" (dict "spec" "resize 300x300 webp q100" "img" $sunset) }}
491 {{ template "process" (dict "spec" "resize 300x300 webp drawing" "img" $sunset) }}
492 {{ template "process" (dict "spec" "resize 300x300 webp icon" "img" $sunset) }}
493 {{ template "process" (dict "spec" "resize 300x300 webp q50 drawing" "img" $sunset) }}
494 {{ template "process" (dict "spec" "resize 400x" "img" $highContrast) }}
495
496 {{ define "process"}}
497 {{ $img := .img.Process .spec }}
498 {{ $ext := path.Ext $img.RelPermalink }}
499 {{ $name := printf "images/%s%s" (.spec | anchorize) $ext }}
500 {{ with $img | resources.Copy $name }}
501 {{ .Publish }}
502 {{ end }}
503 {{ end }}
504 `
505
506 opts := imagetesting.DefaultGoldenOpts
507 opts.T = t
508 opts.Name = name
509 opts.Files = files
510
511 imagetesting.RunGolden(opts)
512 }
513
514 func TestImagesGoldenWebPAnimation(t *testing.T) {
515 t.Parallel()
516
517 if imagetesting.SkipGoldenTests {
518 t.Skip("Skip golden test on this architecture")
519 }
520
521 // Will be used as the base folder for generated images.
522 name := "webp/animation"
523
524 files := `
525 -- hugo.toml --
526 disableKinds = ["page", "section", "taxonomy", "term", "sitemap", "robotsTXT", "404"]
527 -- assets/images/anim.webp --
528 sourcefilename: ../testdata/webp/anim.webp
529 -- assets/images/giphy.gif --
530 sourcefilename: ../testdata/giphy.gif
531 -- layouts/home.html --
532 Home.
533 {{ $webpAnim := resources.Get "images/anim.webp" }}
534 {{ $gifAnim := resources.Get "images/giphy.gif" }}
535 {{ ($webpAnim.Resize "100x100 webp").Publish }}
536 {{ ($webpAnim.Resize "100x100 gif").Publish }}
537 {{ ($gifAnim.Resize "100x100 gif").Publish }}
538 {{ ($gifAnim.Resize "100x100 webp").Publish }}
539 `
540
541 opts := imagetesting.DefaultGoldenOpts
542 opts.T = t
543 opts.Name = name
544 opts.Files = files
545
546 imagetesting.RunGolden(opts)
547 }
548
549 func TestImagesGoldenMethods(t *testing.T) {
550 t.Parallel()
551
552 if imagetesting.SkipGoldenTests {
553 t.Skip("Skip golden test on this architecture")
554 }
555
556 // Will be used as the base folder for generated images.
557 name := "methods"
558
559 files := `
560 -- hugo.toml --
561 [imaging]
562 bgColor = '#ebcc34'
563 hint = 'photo'
564 quality = 75
565 resampleFilter = 'MitchellNetravali'
566 -- assets/sunset.jpg --
567 sourcefilename: ../testdata/sunset.jpg
568 -- assets/gopher.png --
569 sourcefilename: ../testdata/gopher-hero8.png
570
571 -- layouts/home.html --
572 Home.
573 {{ $sunset := resources.Get "sunset.jpg" }}
574 {{ $gopher := resources.Get "gopher.png" }}
575
576
577 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "resize" "spec" "300x" ) }}
578 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "resize" "spec" "x200" ) }}
579 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "fill" "spec" "90x120 left" ) }}
580 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "fill" "spec" "90x120 right" ) }}
581 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "fit" "spec" "200x200" ) }}
582 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "200x200" ) }}
583 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "350x400 center" ) }}
584 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "350x400 smart" ) }}
585 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "350x400 center r90" ) }}
586 {{ template "invoke" (dict "copyFormat" "jpg" "base" $sunset "method" "crop" "spec" "350x400 center q20" ) }}
587 {{ template "invoke" (dict "copyFormat" "png" "base" $gopher "method" "resize" "spec" "100x" ) }}
588 {{ template "invoke" (dict "copyFormat" "png" "base" $gopher "method" "resize" "spec" "100x #fc03ec" ) }}
589 {{ template "invoke" (dict "copyFormat" "jpg" "base" $gopher "method" "resize" "spec" "100x #03fc56 jpg" ) }}
590
591 {{ define "invoke"}}
592 {{ $spec := .spec }}
593 {{ $name := printf "images/%s-%s-%s.%s" .method ((trim .base.Name "/") | lower | anchorize) ($spec | anchorize) .copyFormat }}
594 {{ $img := ""}}
595 {{ if eq .method "resize" }}
596 {{ $img = .base.Resize $spec }}
597 {{ else if eq .method "fill" }}
598 {{ $img = .base.Fill $spec }}
599 {{ else if eq .method "fit" }}
600 {{ $img = .base.Fit $spec }}
601 {{ else if eq .method "crop" }}
602 {{ $img = .base.Crop $spec }}
603 {{ else }}
604 {{ errorf "Unknown method %q" .method }}
605 {{ end }}
606 {{ with $img | resources.Copy $name }}
607 {{ .Publish }}
608 {{ end }}
609 {{ end }}
610 `
611
612 opts := imagetesting.DefaultGoldenOpts
613 opts.T = t
614 opts.Name = name
615 opts.Files = files
616
617 imagetesting.RunGolden(opts)
618 }
619
620 func TestImagesGoldenConfigLossyVsQuality(t *testing.T) {
621 t.Parallel()
622
623 if imagetesting.SkipGoldenTests {
624 t.Skip("Skip golden test on this architecture")
625 }
626
627 files := `
628 -- hugo.toml --
629 [imaging]
630 quality = 90 # will only apply to jpeg in this setup.
631 compression = "lossless" # for webp
632 -- assets/sunset.jpg --
633 sourcefilename: ../testdata/sunset.jpg
634 -- layouts/home.html --
635 Home.
636 {{ $sunset := resources.Get "sunset.jpg" }}
637 {{ template "process" (dict "spec" "resize 300x300 webp" "img" $sunset) }}
638 {{ template "process" (dict "spec" "resize 300x300 webp lossy" "img" $sunset) }}
639 {{ template "process" (dict "spec" "resize 300x300 jpeg" "img" $sunset) }}
640
641 ` + goldenProcess
642
643 // Will be used as the base folder for generated images.
644 name := "losslessvsquality"
645
646 opts := imagetesting.DefaultGoldenOpts
647 opts.T = t
648 opts.Name = name
649 opts.Files = files
650
651 imagetesting.RunGolden(opts)
652 }