0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2025-04-15 03:03:10 -05:00

Merge pull request #1 from j-mcnally/Feature/Pad

allow images to be padded to properly fit requested dimensions
This commit is contained in:
Justin McNally 2021-11-29 16:53:09 -05:00 committed by GitHub
commit 93948624a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View file

@ -26,6 +26,7 @@ import (
const (
optFit = "fit"
optPad = "pad"
optFlipVertical = "fv"
optFlipHorizontal = "fh"
optFormatJPEG = "jpeg"
@ -63,6 +64,9 @@ type Options struct {
// will not be cropped, and aspect ratio will be maintained.
Fit bool
// If true we will pad the image to requested dimensions
Pad bool
// Rotate image the specified degrees counter-clockwise. Valid values
// are 90, 180, 270.
Rotate int
@ -98,6 +102,9 @@ func (o Options) String() string {
if o.Fit {
opts = append(opts, optFit)
}
if o.Pad {
opts = append(opts, optPad)
}
if o.Rotate != 0 {
opts = append(opts, fmt.Sprintf("%s%d", optRotatePrefix, o.Rotate))
}
@ -255,6 +262,8 @@ func ParseOptions(str string) Options {
case len(opt) == 0: // do nothing
case opt == optFit:
options.Fit = true
case opt == optPad:
options.Pad = true
case opt == optFlipVertical:
options.FlipVertical = true
case opt == optFlipHorizontal:

View file

@ -18,6 +18,7 @@ import (
"bytes"
"fmt"
"image"
"image/color"
_ "image/gif" // register gif format
"image/jpeg"
"image/png"
@ -320,5 +321,14 @@ func transformImage(m image.Image, opt Options) image.Image {
m = imaging.FlipH(m)
}
if opt.Pad {
originalWidth := m.Bounds().Max.X
newCenter := (w / 2) - (originalWidth / 2)
m2 := imaging.New(w, h, color.NRGBA{255, 255, 255, 255})
m2 = imaging.Paste(m2, m, image.Pt(newCenter, 0))
m = m2
}
return m
}