aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lu <chrislusf@users.noreply.github.com>2017-05-05 22:05:31 -0700
committerGitHub <noreply@github.com>2017-05-05 22:05:31 -0700
commit68e2dee2cd40f09d28ffa96ad63af89e17154265 (patch)
treed412e8e4fcd356ede4564226673272e997f8fb87
parent9fcff1bd9a52b76eef6e1e56d3294b7efdecc439 (diff)
parent366fe0d394f7a0ede8cf3d7ef28e574c2f64a0b7 (diff)
downloadseaweedfs-68e2dee2cd40f09d28ffa96ad63af89e17154265.tar.xz
seaweedfs-68e2dee2cd40f09d28ffa96ad63af89e17154265.zip
Merge pull request #487 from r-m-n/fill_images
Scale images to fit or fill
-rw-r--r--README.md4
-rw-r--r--weed/images/preprocess.go4
-rw-r--r--weed/images/resizing.go19
-rw-r--r--weed/server/volume_server_handlers_read.go2
4 files changed, 19 insertions, 10 deletions
diff --git a/README.md b/README.md
index cbedafe1e..dab5ca6fb 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,7 @@
- [Wiki Documentation](https://github.com/chrislusf/seaweedfs/wiki)
-[![](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=EEECLJ8QGTTPC)
+[![](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=EEECLJ8QGTTPC)
## Introduction
@@ -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&mode=fit
+http://localhost:8080/3/01637037d6.jpg?height=200&width=200&mode=fill
```
### Rack-Aware and Data Center-Aware Replication ###
diff --git a/weed/images/preprocess.go b/weed/images/preprocess.go
index 968cda44f..4563df7f4 100644
--- a/weed/images/preprocess.go
+++ b/weed/images/preprocess.go
@@ -18,10 +18,10 @@ func MaybePreprocessImage(filename string, data []byte, width, height int) (resi
ext = strings.ToLower(ext)
switch ext {
case ".png", ".gif", ".webp":
- return Resized(ext, data, width, height)
+ return Resized(ext, data, width, height, "")
case ".jpg", ".jpeg":
data = FixJpgOrientation(data)
- return Resized(ext, data, width, height)
+ return Resized(ext, data, width, height, "")
}
return data, 0, 0
}
diff --git a/weed/images/resizing.go b/weed/images/resizing.go
index e644570cb..5af8e45c9 100644
--- a/weed/images/resizing.go
+++ b/weed/images/resizing.go
@@ -12,7 +12,7 @@ import (
"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 {
return data, 0, 0
}
@@ -21,11 +21,18 @@ func Resized(ext string, data []byte, width, height int) (resized []byte, w int,
bounds := srcImage.Bounds()
var dstImage *image.NRGBA
if bounds.Dx() > width && width != 0 || bounds.Dy() > height && height != 0 {
- 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)
+ switch mode {
+ case "fit":
+ dstImage = imaging.Fit(srcImage, width, height, imaging.Lanczos)
+ case "fill":
+ 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 {
return data, bounds.Dx(), bounds.Dy()
diff --git a/weed/server/volume_server_handlers_read.go b/weed/server/volume_server_handlers_read.go
index 3d15d790e..d457c77b9 100644
--- a/weed/server/volume_server_handlers_read.go
+++ b/weed/server/volume_server_handlers_read.go
@@ -144,7 +144,7 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
if 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 {