Scale images to fit or fill

This commit is contained in:
rmn 2017-05-05 12:17:30 +03:00
parent 9fcff1bd9a
commit 366fe0d394
4 changed files with 19 additions and 10 deletions

View file

@ -135,6 +135,8 @@ If you want get a scaled version of an image, you can add some params:
``` ```
http://localhost:8080/3/01637037d6.jpg?height=200&width=200 http://localhost:8080/3/01637037d6.jpg?height=200&width=200
http://localhost:8080/3/01637037d6.jpg?height=200&width=200&mode=fit
http://localhost:8080/3/01637037d6.jpg?height=200&width=200&mode=fill
``` ```
### Rack-Aware and Data Center-Aware Replication ### ### Rack-Aware and Data Center-Aware Replication ###

View file

@ -18,10 +18,10 @@ func MaybePreprocessImage(filename string, data []byte, width, height int) (resi
ext = strings.ToLower(ext) ext = strings.ToLower(ext)
switch ext { switch ext {
case ".png", ".gif", ".webp": case ".png", ".gif", ".webp":
return Resized(ext, data, width, height) return Resized(ext, data, width, height, "")
case ".jpg", ".jpeg": case ".jpg", ".jpeg":
data = FixJpgOrientation(data) data = FixJpgOrientation(data)
return Resized(ext, data, width, height) return Resized(ext, data, width, height, "")
} }
return data, 0, 0 return data, 0, 0
} }

View file

@ -12,7 +12,7 @@ import (
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
) )
func Resized(ext string, data []byte, width, height int) (resized []byte, w int, h int) { func Resized(ext string, data []byte, width, height int, mode string) (resized []byte, w int, h int) {
if width == 0 && height == 0 { if width == 0 && height == 0 {
return data, 0, 0 return data, 0, 0
} }
@ -21,11 +21,18 @@ func Resized(ext string, data []byte, width, height int) (resized []byte, w int,
bounds := srcImage.Bounds() bounds := srcImage.Bounds()
var dstImage *image.NRGBA var dstImage *image.NRGBA
if bounds.Dx() > width && width != 0 || bounds.Dy() > height && height != 0 { if bounds.Dx() > width && width != 0 || bounds.Dy() > height && height != 0 {
if width == height && bounds.Dx() != bounds.Dy() { switch mode {
dstImage = imaging.Thumbnail(srcImage, width, height, imaging.Lanczos) case "fit":
w, h = width, height dstImage = imaging.Fit(srcImage, width, height, imaging.Lanczos)
} else { case "fill":
dstImage = imaging.Resize(srcImage, width, height, imaging.Lanczos) dstImage = imaging.Fill(srcImage, width, height, imaging.Center, imaging.Lanczos)
default:
if width == height && bounds.Dx() != bounds.Dy() {
dstImage = imaging.Thumbnail(srcImage, width, height, imaging.Lanczos)
w, h = width, height
} else {
dstImage = imaging.Resize(srcImage, width, height, imaging.Lanczos)
}
} }
} else { } else {
return data, bounds.Dx(), bounds.Dy() return data, bounds.Dx(), bounds.Dy()

View file

@ -144,7 +144,7 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
if r.FormValue("height") != "" { if r.FormValue("height") != "" {
height, _ = strconv.Atoi(r.FormValue("height")) height, _ = strconv.Atoi(r.FormValue("height"))
} }
n.Data, _, _ = images.Resized(ext, n.Data, width, height) n.Data, _, _ = images.Resized(ext, n.Data, width, height, r.FormValue("mode"))
} }
if e := writeResponseContent(filename, mtype, bytes.NewReader(n.Data), w, r); e != nil { if e := writeResponseContent(filename, mtype, bytes.NewReader(n.Data), w, r); e != nil {