mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-10-14 23:44:53 +08:00
A hidden feature: dynamically resize image.
Adding width=xxx or height=xxx, or both, can dynamically resize a gif,jpg,png. But the performance is bad. So, not recommending, but you can use it if you insist. :)
This commit is contained in:
@@ -1,11 +1,17 @@
|
|||||||
package weed_server
|
package weed_server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"code.google.com/p/weed-fs/go/glog"
|
"code.google.com/p/weed-fs/go/glog"
|
||||||
"code.google.com/p/weed-fs/go/operation"
|
"code.google.com/p/weed-fs/go/operation"
|
||||||
"code.google.com/p/weed-fs/go/stats"
|
"code.google.com/p/weed-fs/go/stats"
|
||||||
"code.google.com/p/weed-fs/go/storage"
|
"code.google.com/p/weed-fs/go/storage"
|
||||||
"code.google.com/p/weed-fs/go/topology"
|
"code.google.com/p/weed-fs/go/topology"
|
||||||
|
"github.com/disintegration/imaging"
|
||||||
|
"image"
|
||||||
|
"image/gif"
|
||||||
|
"image/jpeg"
|
||||||
|
"image/png"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -118,6 +124,36 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ext == ".png" || ext == ".jpg" || ext == ".gif" {
|
||||||
|
if srcImage, _, err := image.Decode(bytes.NewReader(n.Data)); err == nil {
|
||||||
|
width, height := 0, 0
|
||||||
|
if r.FormValue("width") != "" {
|
||||||
|
width, _ = strconv.Atoi(r.FormValue("width"))
|
||||||
|
}
|
||||||
|
if r.FormValue("height") != "" {
|
||||||
|
height, _ = strconv.Atoi(r.FormValue("height"))
|
||||||
|
}
|
||||||
|
if width != 0 || height != 0 {
|
||||||
|
bounds := srcImage.Bounds()
|
||||||
|
var dstImage *image.NRGBA
|
||||||
|
if width == height && bounds.Dx() != bounds.Dy() {
|
||||||
|
dstImage = imaging.Thumbnail(srcImage, width, height, imaging.Lanczos)
|
||||||
|
} else {
|
||||||
|
dstImage = imaging.Resize(srcImage, width, height, imaging.Lanczos)
|
||||||
|
}
|
||||||
|
var buf bytes.Buffer
|
||||||
|
switch ext {
|
||||||
|
case ".png":
|
||||||
|
png.Encode(&buf, dstImage)
|
||||||
|
case ".jpg":
|
||||||
|
jpeg.Encode(&buf, dstImage, nil)
|
||||||
|
case ".gif":
|
||||||
|
gif.Encode(&buf, dstImage, nil)
|
||||||
|
}
|
||||||
|
n.Data = buf.Bytes()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
w.Header().Set("Content-Length", strconv.Itoa(len(n.Data)))
|
w.Header().Set("Content-Length", strconv.Itoa(len(n.Data)))
|
||||||
if isGetMethod {
|
if isGetMethod {
|
||||||
if _, e = w.Write(n.Data); e != nil {
|
if _, e = w.Write(n.Data); e != nil {
|
||||||
|
Reference in New Issue
Block a user