never resize larger than the original image

This commit is contained in:
Will Norris 2013-12-26 19:32:23 -08:00
parent 8f7f5be71c
commit 3c72d0b5ca

View file

@ -40,18 +40,28 @@ func Transform(img []byte, opt *Options) ([]byte, error) {
} }
// convert percentage width and height values to absolute values // convert percentage width and height values to absolute values
imgW := m.Bounds().Max.X - m.Bounds().Min.X
imgH := m.Bounds().Max.Y - m.Bounds().Min.Y
var h, w int var h, w int
if opt.Width > 0 && opt.Width < 1 { if opt.Width > 0 && opt.Width < 1 {
w = int(float64(m.Bounds().Max.X-m.Bounds().Min.X) * opt.Width) w = int(float64(imgW) * opt.Width)
} else { } else {
w = int(opt.Width) w = int(opt.Width)
} }
if opt.Height > 0 && opt.Height < 1 { if opt.Height > 0 && opt.Height < 1 {
h = int(float64(m.Bounds().Max.Y-m.Bounds().Min.Y) * opt.Height) h = int(float64(imgH) * opt.Height)
} else { } else {
h = int(opt.Height) h = int(opt.Height)
} }
// never resize larger than the original image
if w > imgW {
w = imgW
}
if h > imgH {
h = imgH
}
// resize // resize
if w != 0 || h != 0 { if w != 0 || h != 0 {
if opt.Fit { if opt.Fit {