0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-16 21:56:43 -05:00

add svg output support, but only with primitive

This commit is contained in:
Will Norris 2019-10-13 09:06:27 -07:00
parent b951a9ccc3
commit a1e81bb21f
3 changed files with 17 additions and 2 deletions

View file

@ -19,6 +19,7 @@ const (
optFlipHorizontal = "fh" optFlipHorizontal = "fh"
optFormatJPEG = "jpeg" optFormatJPEG = "jpeg"
optFormatPNG = "png" optFormatPNG = "png"
optFormatSVG = "svg"
optFormatTIFF = "tiff" optFormatTIFF = "tiff"
optPrimitivePrefix = "p" optPrimitivePrefix = "p"
optRotatePrefix = "r" optRotatePrefix = "r"
@ -70,7 +71,7 @@ type Options struct {
// will always be overwritten by the value of Proxy.ScaleUp. // will always be overwritten by the value of Proxy.ScaleUp.
ScaleUp bool ScaleUp bool
// Desired image format. Valid values are "jpeg", "png", "tiff". // Desired image format. Valid values are "jpeg", "png", "svg", "tiff".
Format string Format string
// Crop rectangle params // Crop rectangle params
@ -256,7 +257,7 @@ func ParseOptions(str string) Options {
options.FlipHorizontal = true options.FlipHorizontal = true
case opt == optScaleUp: // this option is intentionally not documented above case opt == optScaleUp: // this option is intentionally not documented above
options.ScaleUp = true options.ScaleUp = true
case opt == optFormatJPEG, opt == optFormatPNG, opt == optFormatTIFF: case opt == optFormatJPEG, opt == optFormatPNG, opt == optFormatSVG, opt == optFormatTIFF:
options.Format = opt options.Format = opt
case opt == optSmartCrop: case opt == optSmartCrop:
options.SmartCrop = true options.SmartCrop = true

View file

@ -263,6 +263,9 @@ func peekContentType(p *bufio.Reader) string {
if err != nil && err != bufio.ErrBufferFull && err != io.EOF { if err != nil && err != bufio.ErrBufferFull && err != io.EOF {
return "" return ""
} }
if bytes.HasPrefix(byt, []byte("<svg")) {
return "image/svg+xml"
}
return http.DetectContentType(byt) return http.DetectContentType(byt)
} }

View file

@ -65,6 +65,11 @@ func Transform(img []byte, opt Options) ([]byte, error) {
format = "jpeg" format = "jpeg"
} }
if opt.Format == "svg" && opt.Primitive.Count == 0 {
// svg output only supported with primitive transform
opt.Format = ""
}
if opt.Format != "" { if opt.Format != "" {
format = opt.Format format = opt.Format
} }
@ -109,6 +114,12 @@ func Transform(img []byte, opt Options) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
case "svg":
o := opt // copy
o.Primitive.Count = 0
m = transformImage(m, o)
model := transformPrimitive(m, opt)
buf.WriteString(model.SVG())
default: default:
return nil, fmt.Errorf("unsupported format: %v", format) return nil, fmt.Errorf("unsupported format: %v", format)
} }