Browse Source

增加chunk图片文件支持width和height

pull/695/head
zhangmingfeng 7 years ago
parent
commit
79d18c69b4
  1. 10
      weed/images/preprocess.go
  2. 13
      weed/images/resizing.go
  3. 23
      weed/server/volume_server_handlers_read.go

10
weed/images/preprocess.go

@ -3,6 +3,8 @@ package images
import (
"path/filepath"
"strings"
"bytes"
"io"
)
/*
@ -13,15 +15,15 @@ import (
* Call this function on any file uploaded to SeaweedFS
*
*/
func MaybePreprocessImage(filename string, data []byte, width, height int) (resized []byte, w int, h int) {
func MaybePreprocessImage(filename string, data []byte, width, height int) (resized io.ReadSeeker, w int, h int) {
ext := filepath.Ext(filename)
ext = strings.ToLower(ext)
switch ext {
case ".png", ".gif":
return Resized(ext, data, width, height, "")
return Resized(ext, bytes.NewReader(data), width, height, "")
case ".jpg", ".jpeg":
data = FixJpgOrientation(data)
return Resized(ext, data, width, height, "")
return Resized(ext, bytes.NewReader(data), width, height, "")
}
return data, 0, 0
return bytes.NewReader(data), 0, 0
}

13
weed/images/resizing.go

@ -9,13 +9,14 @@ import (
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/disintegration/imaging"
"io"
)
func Resized(ext string, data []byte, width, height int, mode string) (resized []byte, w int, h int) {
func Resized(ext string, read io.ReadSeeker, width, height int, mode string) (resized io.ReadSeeker, w int, h int) {
if width == 0 && height == 0 {
return data, 0, 0
return read, 0, 0
}
srcImage, _, err := image.Decode(bytes.NewReader(data))
srcImage, _, err := image.Decode(read)
if err == nil {
bounds := srcImage.Bounds()
var dstImage *image.NRGBA
@ -34,7 +35,7 @@ func Resized(ext string, data []byte, width, height int, mode string) (resized [
}
}
} else {
return data, bounds.Dx(), bounds.Dy()
return read, bounds.Dx(), bounds.Dy()
}
var buf bytes.Buffer
switch ext {
@ -45,9 +46,9 @@ func Resized(ext string, data []byte, width, height int, mode string) (resized [
case ".gif":
gif.Encode(&buf, dstImage, nil)
}
return buf.Bytes(), dstImage.Bounds().Dx(), dstImage.Bounds().Dy()
return bytes.NewReader(buf.Bytes()), dstImage.Bounds().Dx(), dstImage.Bounds().Dy()
} else {
glog.Error(err)
}
return data, 0, 0
return read, 0, 0
}

23
weed/server/volume_server_handlers_read.go

@ -135,7 +135,8 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
}
}
}
if ext == ".png" || ext == ".jpg" || ext == ".gif" {
var rs io.ReadSeeker
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
width, height := 0, 0
if r.FormValue("width") != "" {
width, _ = strconv.Atoi(r.FormValue("width"))
@ -143,10 +144,10 @@ 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, r.FormValue("mode"))
rs, _, _ = images.Resized(ext, bytes.NewReader(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, rs, w, r); e != nil {
glog.V(2).Infoln("response write error:", e)
}
}
@ -164,6 +165,9 @@ func (vs *VolumeServer) tryHandleChunkedFile(n *storage.Needle, fileName string,
if fileName == "" && chunkManifest.Name != "" {
fileName = chunkManifest.Name
}
ext := path.Ext(fileName)
mType := ""
if chunkManifest.Mime != "" {
mt := chunkManifest.Mime
@ -179,7 +183,18 @@ func (vs *VolumeServer) tryHandleChunkedFile(n *storage.Needle, fileName string,
Master: vs.GetMaster(),
}
defer chunkedFileReader.Close()
if e := writeResponseContent(fileName, mType, chunkedFileReader, w, r); e != nil {
var rs io.ReadSeeker
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
width, height := 0, 0
if r.FormValue("width") != "" {
width, _ = strconv.Atoi(r.FormValue("width"))
}
if r.FormValue("height") != "" {
height, _ = strconv.Atoi(r.FormValue("height"))
}
rs, _, _ = images.Resized(ext, chunkedFileReader, width, height, r.FormValue("mode"))
}
if e := writeResponseContent(fileName, mType, rs, w, r); e != nil {
glog.V(2).Infoln("response write error:", e)
}
return true

Loading…
Cancel
Save