diff --git a/resource/image_test.go b/resource/image_test.go index 7906f4fbc..1e5b3d531 100644 --- a/resource/image_test.go +++ b/resource/image_test.go @@ -278,6 +278,13 @@ func TestImageResize8BitPNG(t *testing.T) { } +func TestSVGImage(t *testing.T) { + assert := require.New(t) + spec := newTestResourceSpec(assert) + svg := fetchResourceForSpec(spec, assert, "circle.svg") + assert.NotNil(svg) +} + func BenchmarkResizeParallel(b *testing.B) { assert := require.New(b) img := fetchSunset(assert) diff --git a/resource/resource.go b/resource/resource.go index 67aed39c2..828b03da1 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -23,8 +23,6 @@ import ( "strings" "sync" - "github.com/disintegration/imaging" - "github.com/spf13/cast" "github.com/gobwas/glob" @@ -288,6 +286,15 @@ func (r *Spec) newResource( gr := r.newGenericResource(targetPathBuilder, fi, absPublishDir, absSourceFilename, filepath.ToSlash(relTargetFilename), mimeType) if mimeType == "image" { + ext := strings.ToLower(helpers.Ext(absSourceFilename)) + + imgFormat, ok := imageFormats[ext] + if !ok { + // This allows SVG etc. to be used as resources. They will not have the methods of the Image, but + // that would not (currently) have worked. + return gr, nil + } + f, err := r.Fs.Source.Open(absSourceFilename) if err != nil { return nil, err @@ -299,13 +306,6 @@ func (r *Spec) newResource( return nil, err } - ext := strings.ToLower(helpers.Ext(absSourceFilename)) - - imgFormat, ok := imageFormats[ext] - if !ok { - return nil, imaging.ErrUnsupportedFormat - } - return &Image{ hash: hash, format: imgFormat, diff --git a/resource/testdata/circle.svg b/resource/testdata/circle.svg new file mode 100644 index 000000000..2759ae703 --- /dev/null +++ b/resource/testdata/circle.svg @@ -0,0 +1,5 @@ + + + Sorry, your browser does not support inline SVG. + + \ No newline at end of file diff --git a/resource/testhelpers_test.go b/resource/testhelpers_test.go index 7f6d4f307..89653ce11 100644 --- a/resource/testhelpers_test.go +++ b/resource/testhelpers_test.go @@ -88,6 +88,12 @@ func fetchImage(assert *require.Assertions, name string) *Image { } func fetchImageForSpec(spec *Spec, assert *require.Assertions, name string) *Image { + r := fetchResourceForSpec(spec, assert, name) + assert.IsType(&Image{}, r) + return r.(*Image) +} + +func fetchResourceForSpec(spec *Spec, assert *require.Assertions, name string) Resource { src, err := os.Open("testdata/" + name) assert.NoError(err) @@ -107,10 +113,9 @@ func fetchImageForSpec(spec *Spec, assert *require.Assertions, name string) *Ima r, err := spec.NewResourceFromFilename(factory, "/public", f, name) assert.NoError(err) - assert.IsType(&Image{}, r) - return r.(*Image) -} + return r +} func assertFileCache(assert *require.Assertions, fs *hugofs.Fs, filename string, width, height int) { f, err := fs.Source.Open(filepath.Join("/res/_gen/images", filename)) assert.NoError(err)