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
This commit is contained in:
J. Ansorg
2019-09-21 16:50:27 +02:00
committed by Bjørn Erik Pedersen
parent 34dc06b032
commit e5856e61d8
7 changed files with 100 additions and 15 deletions

View File

@@ -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