You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.5 KiB

7 months ago
package core
import (
"bytes"
"fmt"
"image"
"image/jpeg"
"image/png"
"strings"
"golang.org/x/image/tiff"
)
// EncodeImage encodes img into buf using the given format name and returns
// the MIME type for use in data URIs.
func EncodeImage(buf *bytes.Buffer, img image.Image, format string, quality int) (mimeType string, err error) {
switch strings.ToLower(format) {
case "jpg", "jpeg":
err = jpeg.Encode(buf, img, &jpeg.Options{Quality: quality})
mimeType = "image/jpeg"
case "png":
err = png.Encode(buf, img)
mimeType = "image/png"
case "tif", "tiff":
err = tiff.Encode(buf, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
mimeType = "image/tiff"
case "webp", "bmp":
err = fmt.Errorf("format %s is read-only — use jpg, png or tif for output", format)
default:
err = fmt.Errorf("unknown output format: %s", format)
}
return
}
// ExtToFormat normalises a file extension string to a canonical format name.
func ExtToFormat(ext string) string {
switch strings.ToLower(ext) {
case "jpeg":
return "jpg"
case "tiff":
return "tif"
default:
return strings.ToLower(ext)
}
}
// GetResampleFilter returns the imaging.ResampleFilter for the given name.
// Falls back to Lanczos for unknown names.
func GetResampleFilter(name string) ResampleFilter {
switch strings.ToLower(name) {
case "cubic":
return ResampleCubic
case "linear":
return ResampleLinear
case "box":
return ResampleBox
default:
return ResampleLanczos
}
}

Powered by TurnKey Linux.