Auto-calc width or height if some of them is 0

This commit is contained in:
DarthSim
2018-09-07 21:58:06 +06:00
parent 7d7bbe3649
commit 9f0a2a4939

View File

@@ -128,16 +128,30 @@ func extractMeta(img *C.VipsImage) (int, int, int, bool) {
return width, height, angle, flip
}
func calcScale(width, height int, po processingOptions) float64 {
if (po.Width == width && po.Height == height) || (po.Resize != resizeFill && po.Resize != resizeFit) {
return 1
}
func needToScale(width, height int, po processingOptions) bool {
return ((po.Width != 0 && po.Width != width) || (po.Height != 0 && po.Height != height)) &&
(po.Resize == resizeFill || po.Resize == resizeFit)
}
func needToCrop(width, height int, po processingOptions) bool {
return (po.Width != width || po.Height != height) &&
(po.Resize == resizeFill || po.Resize == resizeCrop)
}
func calcScale(width, height int, po processingOptions) float64 {
fsw, fsh, fow, foh := float64(width), float64(height), float64(po.Width), float64(po.Height)
wr := fow / fsw
hr := foh / fsh
if po.Width == 0 {
return hr
}
if po.Height == 0 {
return wr
}
if po.Resize == resizeFit {
return math.Min(wr, hr)
}
@@ -216,8 +230,7 @@ func processImage(data []byte, imgtype imageType, po processingOptions, t *timer
}
}
if po.Width != imgWidth || po.Height != imgHeight {
if po.Resize == resizeFill || po.Resize == resizeFit {
if needToScale(imgWidth, imgHeight, po) {
scale := calcScale(imgWidth, imgHeight, po)
// Do some shrink-on-load
@@ -248,13 +261,15 @@ func processImage(data []byte, imgtype imageType, po processingOptions, t *timer
return nil, err
}
// Update actual image size after resize
imgWidth, imgHeight, _, _ = extractMeta(img)
if premultiplied {
if err = vipsUnpremultiply(&img, bandFormat); err != nil {
return nil, err
}
}
}
}
if err = vipsImportColourProfile(&img); err != nil {
return nil, err
@@ -286,7 +301,15 @@ func processImage(data []byte, imgtype imageType, po processingOptions, t *timer
t.Check()
if (po.Width != imgWidth || po.Height != imgHeight) && (po.Resize == resizeFill || po.Resize == resizeCrop) {
if po.Width == 0 {
po.Width = imgWidth
}
if po.Height == 0 {
po.Height = imgHeight
}
if needToCrop(imgWidth, imgHeight, po) {
if po.Gravity == gravitySmart {
if err = vipsImageCopyMemory(&img); err != nil {
return nil, err
@@ -295,7 +318,7 @@ func processImage(data []byte, imgtype imageType, po processingOptions, t *timer
return nil, err
}
} else {
left, top := calcCrop(int(img.Xsize), int(img.Ysize), po)
left, top := calcCrop(imgWidth, imgHeight, po)
if err = vipsCrop(&img, left, top, po.Width, po.Height); err != nil {
return nil, err
}