mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-14 20:33:59 +02:00
Add $image.Process
Which supports all the existing actions: resize, crop, fit, fill. But it also allows plain format conversions: ``` {{ $img = $img.Process "webp" }} ``` Which will be a simple re-encoding of the source image. Fixes #11483
This commit is contained in:
@@ -84,10 +84,7 @@ func TestImageTransformBasic(t *testing.T) {
|
||||
fileCache := spec.FileCaches.ImageCache().Fs
|
||||
|
||||
assertWidthHeight := func(img images.ImageResource, w, h int) {
|
||||
c.Helper()
|
||||
c.Assert(img, qt.Not(qt.IsNil))
|
||||
c.Assert(img.Width(), qt.Equals, w)
|
||||
c.Assert(img.Height(), qt.Equals, h)
|
||||
assertWidthHeight(c, img, w, h)
|
||||
}
|
||||
|
||||
colors, err := image.Colors()
|
||||
@@ -164,6 +161,45 @@ func TestImageTransformBasic(t *testing.T) {
|
||||
c.Assert(cropped, qt.Equals, croppedAgain)
|
||||
}
|
||||
|
||||
func TestImageProcess(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
_, img := fetchSunset(c)
|
||||
resized, err := img.Process("resiZe 300x200")
|
||||
c.Assert(err, qt.IsNil)
|
||||
assertWidthHeight(c, resized, 300, 200)
|
||||
rotated, err := resized.Process("R90")
|
||||
c.Assert(err, qt.IsNil)
|
||||
assertWidthHeight(c, rotated, 200, 300)
|
||||
converted, err := img.Process("png")
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(converted.MediaType().Type, qt.Equals, "image/png")
|
||||
|
||||
checkProcessVsMethod := func(action, spec string) {
|
||||
var expect images.ImageResource
|
||||
var err error
|
||||
switch action {
|
||||
case images.ActionCrop:
|
||||
expect, err = img.Crop(spec)
|
||||
case images.ActionFill:
|
||||
expect, err = img.Fill(spec)
|
||||
case images.ActionFit:
|
||||
expect, err = img.Fit(spec)
|
||||
case images.ActionResize:
|
||||
expect, err = img.Resize(spec)
|
||||
}
|
||||
c.Assert(err, qt.IsNil)
|
||||
got, err := img.Process(spec + " " + action)
|
||||
c.Assert(err, qt.IsNil)
|
||||
assertWidthHeight(c, got, expect.Width(), expect.Height())
|
||||
c.Assert(got.MediaType(), qt.Equals, expect.MediaType())
|
||||
}
|
||||
|
||||
checkProcessVsMethod(images.ActionCrop, "300x200 topleFt")
|
||||
checkProcessVsMethod(images.ActionFill, "300x200 topleft")
|
||||
checkProcessVsMethod(images.ActionFit, "300x200 png")
|
||||
checkProcessVsMethod(images.ActionResize, "300x R90")
|
||||
}
|
||||
|
||||
func TestImageTransformFormat(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
@@ -852,3 +888,10 @@ func BenchmarkResizeParallel(b *testing.B) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func assertWidthHeight(c *qt.C, img images.ImageResource, w, h int) {
|
||||
c.Helper()
|
||||
c.Assert(img, qt.Not(qt.IsNil))
|
||||
c.Assert(img.Width(), qt.Equals, w)
|
||||
c.Assert(img.Height(), qt.Equals, h)
|
||||
}
|
||||
|
Reference in New Issue
Block a user