diff options
author | J. Ansorg <github@joachim-ansorg.de> | 2019-09-21 16:50:27 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-09-21 16:50:27 +0200 |
commit | e5856e61d88ef5149582851b00e06b5b93dce9f8 (patch) | |
tree | 5cfa1d63c34fbca7c4c226827dc574794edd4ecd /resources/images | |
parent | 34dc06b032741abac342d7a2a77510ded9b72ae8 (diff) |
resources: Support output image format in image operations
The image format is defined as the image extension of the known formats,
excluding the dot.
All of 'img.Resize "600x jpeg"', 'img.Resize "600x jpg"',
and 'img.Resize "600x png"' are valid format definitions.
If the target format is defined in the operation definition string,
then the converted image will be stored in this format. Permalinks and
media type are updated correspondingly.
Unknown image extensions in the operation definition have not effect.
See #6298
Diffstat (limited to 'resources/images')
-rw-r--r-- | resources/images/config.go | 6 | ||||
-rw-r--r-- | resources/images/image.go | 32 |
2 files changed, 36 insertions, 2 deletions
diff --git a/resources/images/config.go b/resources/images/config.go index a290922ab..6bc701bfe 100644 --- a/resources/images/config.go +++ b/resources/images/config.go @@ -187,7 +187,8 @@ func DecodeImageConfig(action, config string, defaults Imaging) (ImageConfig, er } else { return c, errors.New("invalid image dimensions") } - + } else if f, ok := ImageFormatFromExt("." + part); ok { + c.TargetFormat = f } } @@ -212,6 +213,9 @@ func DecodeImageConfig(action, config string, defaults Imaging) (ImageConfig, er // ImageConfig holds configuration to create a new image from an existing one, resize etc. type ImageConfig struct { + // This defines the output format of the output image. It defaults to the source format + TargetFormat Format + Action string // If set, this will be used as the key in filenames etc. diff --git a/resources/images/image.go b/resources/images/image.go index e72d96837..bd7500c28 100644 --- a/resources/images/image.go +++ b/resources/images/image.go @@ -23,6 +23,7 @@ import ( "io" "sync" + "github.com/gohugoio/hugo/media" "github.com/gohugoio/hugo/resources/images/exif" "github.com/disintegration/gift" @@ -59,7 +60,7 @@ type Image struct { } func (i *Image) EncodeTo(conf ImageConfig, img image.Image, w io.Writer) error { - switch i.Format { + switch conf.TargetFormat { case JPEG: var rgba *image.RGBA @@ -250,6 +251,35 @@ const ( BMP ) +// RequiresDefaultQuality returns if the default quality needs to be applied to images of this format +func (f Format) RequiresDefaultQuality() bool { + return f == JPEG +} + +// DefaultExtension returns the default file extension of this format, starting with a dot. +// For example: .jpg for JPEG +func (f Format) DefaultExtension() string { + return f.MediaType().FullSuffix() +} + +// MediaType returns the media type of this image, e.g. image/jpeg for JPEG +func (f Format) MediaType() media.Type { + switch f { + case JPEG: + return media.JPGType + case PNG: + return media.PNGType + case GIF: + return media.GIFType + case TIFF: + return media.TIFFType + case BMP: + return media.BMPType + default: + panic(fmt.Sprintf("%d is not a valid image format", f)) + } +} + type imageConfig struct { config image.Config configInit sync.Once |