From 87b98711f79b71760fe95f10cd32f7a0c21bf3d6 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 25 Apr 2014 22:28:01 -0700 Subject: [PATCH] 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. :) --- go/weed/weed_server/volume_server_handlers.go | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/go/weed/weed_server/volume_server_handlers.go b/go/weed/weed_server/volume_server_handlers.go index e2fd6e6f9..3c94a75f1 100644 --- a/go/weed/weed_server/volume_server_handlers.go +++ b/go/weed/weed_server/volume_server_handlers.go @@ -1,11 +1,17 @@ package weed_server import ( + "bytes" "code.google.com/p/weed-fs/go/glog" "code.google.com/p/weed-fs/go/operation" "code.google.com/p/weed-fs/go/stats" "code.google.com/p/weed-fs/go/storage" "code.google.com/p/weed-fs/go/topology" + "github.com/disintegration/imaging" + "image" + "image/gif" + "image/jpeg" + "image/png" "mime" "net/http" "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))) if isGetMethod { if _, e = w.Write(n.Data); e != nil {