| 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 |
"testing" |
| 18 |
|
| 19 |
"github.com/bep/logg" |
| 20 |
"github.com/gohugoio/hugo/htesting" |
| 21 |
"github.com/gohugoio/hugo/hugolib" |
| 22 |
) |
| 23 |
|
| 24 |
func TestAutoOrient(t *testing.T) { |
| 25 |
files := ` |
| 26 |
-- hugo.toml -- |
| 27 |
-- assets/rotate270.jpg -- |
| 28 |
sourcefilename: ../testdata/exif/orientation6.jpg |
| 29 |
-- layouts/home.html -- |
| 30 |
{{ $img := resources.Get "rotate270.jpg" }} |
| 31 |
W/H original: {{ $img.Width }}/{{ $img.Height }} |
| 32 |
{{ $rotated := $img.Filter images.AutoOrient }} |
| 33 |
W/H rotated: {{ $rotated.Width }}/{{ $rotated.Height }} |
| 34 |
` |
| 35 |
|
| 36 |
b := hugolib.Test(t, files) |
| 37 |
b.AssertFileContent("public/index.html", "W/H original: 80/40\n\nW/H rotated: 40/80") |
| 38 |
} |
| 39 |
|
| 40 |
// Issue 12733. |
| 41 |
func TestOrientationEq(t *testing.T) { |
| 42 |
files := ` |
| 43 |
-- hugo.toml -- |
| 44 |
-- assets/rotate270.jpg -- |
| 45 |
sourcefilename: ../testdata/exif/orientation6.jpg |
| 46 |
-- layouts/home.html -- |
| 47 |
{{ $img := resources.Get "rotate270.jpg" }} |
| 48 |
{{ $orientation := $img.Exif.Tags.Orientation }} |
| 49 |
Orientation: {{ $orientation }}|eq 6: {{ eq $orientation 6 }}|Type: {{ printf "%T" $orientation }}| |
| 50 |
` |
| 51 |
|
| 52 |
b := hugolib.Test(t, files) |
| 53 |
b.AssertFileContent("public/index.html", "Orientation: 6|eq 6: true|") |
| 54 |
} |
| 55 |
|
| 56 |
func TestColorsIssue14453(t *testing.T) { |
| 57 |
// Go changed their JPEG implementation in Go 1.26, so we cannot run this test on earlier versions. See https://go.dev/doc/go1.26#imagejpegpkgimagejpeg |
| 58 |
if htesting.GoMinorVersion() < 26 { |
| 59 |
t.Skip("Go 1.26+ required") |
| 60 |
} |
| 61 |
files := ` |
| 62 |
-- hugo.toml -- |
| 63 |
-- assets/sunset.jpg -- |
| 64 |
sourcefilename: ../testdata/sunset.jpg |
| 65 |
-- layouts/home.html -- |
| 66 |
{{ $img := resources.Get "sunset.jpg" }} |
| 67 |
{{ $img := $img.Fit "100x100" }} |
| 68 |
{{ $img := $img.Filter (slice images.AutoOrient (images.Process "fit 100x100 webp")) -}} |
| 69 |
{{ $colors := $img.Colors }} |
| 70 |
Colors: {{ $colors }}| |
| 71 |
` |
| 72 |
tempDir := t.TempDir() |
| 73 |
for range 2 { |
| 74 |
b := hugolib.Test(t, files, hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) { |
| 75 |
cfg.NeedsOsFS = true |
| 76 |
cfg.WorkingDir = tempDir |
| 77 |
})) |
| 78 |
b.AssertFileContent("public/index.html", "Colors: [#2e2f33 #a69e94 #d29d59 #a26a3f #747c83 #7b848b]|") |
| 79 |
|
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestImageCropSmartKeepsTargetSizeIssue13688(t *testing.T) { |
| 84 |
t.Parallel() |
| 85 |
|
| 86 |
files := ` |
| 87 |
-- hugo.toml -- |
| 88 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 89 |
-- assets/sunset.jpg -- |
| 90 |
sourcefilename: ../testdata/sunset.jpg |
| 91 |
-- layouts/home.html -- |
| 92 |
{{ with resources.Get "sunset.jpg" }} |
| 93 |
Original: {{ .Width }}x{{ .Height }}| |
| 94 |
{{- with .Crop "900x561 TopLeft" -}} |
| 95 |
CropTopLeft: {{ .Width }}x{{ .Height }}| |
| 96 |
{{- end -}} |
| 97 |
{{- with .Crop "900x561 Smart" -}} |
| 98 |
CropSmart: {{ .Width }}x{{ .Height }}| |
| 99 |
{{- end -}} |
| 100 |
{{ end }} |
| 101 |
` |
| 102 |
|
| 103 |
b := hugolib.Test(t, files) |
| 104 |
|
| 105 |
b.AssertFileContent("public/index.html", "Original: 900x562|CropTopLeft: 900x561|CropSmart: 900x561|") |
| 106 |
} |
| 107 |
|
| 108 |
func TestImagingGlobalsDeprecated(t *testing.T) { |
| 109 |
t.Parallel() |
| 110 |
|
| 111 |
files := ` |
| 112 |
-- hugo.toml -- |
| 113 |
[imaging] |
| 114 |
quality = 70 |
| 115 |
hint = "picture" |
| 116 |
compression = "lossless" |
| 117 |
-- layouts/home.html -- |
| 118 |
Home. |
| 119 |
` |
| 120 |
|
| 121 |
b := hugolib.Test(t, files, hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) { |
| 122 |
cfg.LogLevel = logg.LevelInfo |
| 123 |
})) |
| 124 |
|
| 125 |
b.AssertLogContains( |
| 126 |
"project config key imaging.quality was deprecated in Hugo v0.163.0", |
| 127 |
"project config key imaging.hint was deprecated in Hugo v0.163.0", |
| 128 |
"project config key imaging.compression was deprecated in Hugo v0.163.0", |
| 129 |
) |
| 130 |
} |
| 131 |
|
| 132 |
func BenchmarkImageResize(b *testing.B) { |
| 133 |
files := ` |
| 134 |
-- content/p1/sunrise.jpg -- |
| 135 |
sourcefilename: ../../resources/testdata/sunrise.jpg |
| 136 |
-- content/p1/index.md -- |
| 137 |
-- content/p2/sunrise.jpg -- |
| 138 |
sourcefilename: ../../resources/testdata/sunrise.jpg |
| 139 |
-- content/p2/index.md -- |
| 140 |
-- content/p3/sunrise.jpg -- |
| 141 |
sourcefilename: ../../resources/testdata/sunrise.jpg |
| 142 |
-- content/p3/index.md -- |
| 143 |
-- content/p4/sunrise.jpg -- |
| 144 |
sourcefilename: ../../resources/testdata/sunrise.jpg |
| 145 |
-- content/p4/index.md -- |
| 146 |
-- content/p5/sunrise.jpg -- |
| 147 |
sourcefilename: ../../resources/testdata/sunrise.jpg |
| 148 |
-- content/p5/index.md -- |
| 149 |
-- content/p6/sunrise.jpg -- |
| 150 |
sourcefilename: ../../resources/testdata/sunrise.jpg |
| 151 |
-- content/p6/index.md -- |
| 152 |
-- content/p7/sunrise.jpg -- |
| 153 |
sourcefilename: ../../resources/testdata/sunrise.jpg |
| 154 |
-- content/p7/index.md -- |
| 155 |
-- content/p8/sunrise.jpg -- |
| 156 |
sourcefilename: ../../resources/testdata/sunrise.jpg |
| 157 |
-- content/p8/index.md -- |
| 158 |
-- content/p9/sunrise.jpg -- |
| 159 |
sourcefilename: ../../resources/testdata/sunrise.jpg |
| 160 |
-- content/p9/index.md -- |
| 161 |
-- content/p10/sunrise.jpg -- |
| 162 |
sourcefilename: ../../resources/testdata/sunrise.jpg |
| 163 |
-- content/p10/index.md -- |
| 164 |
-- layouts/home.html -- |
| 165 |
Home. |
| 166 |
-- layouts/page.html -- |
| 167 |
Page. |
| 168 |
{{ $image := .Resources.Get "sunrise.jpg" }} |
| 169 |
{{ ($image.Process "resize 200x200").Publish }} |
| 170 |
|
| 171 |
` |
| 172 |
|
| 173 |
cfg := hugolib.IntegrationTestConfig{ |
| 174 |
T: b, |
| 175 |
TxtarString: files, |
| 176 |
} |
| 177 |
|
| 178 |
for b.Loop() { |
| 179 |
b.StopTimer() |
| 180 |
builder := hugolib.NewIntegrationTestBuilder(cfg).Init() |
| 181 |
b.StartTimer() |
| 182 |
builder.Build() |
| 183 |
} |
| 184 |
} |