Chris Lu
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with
29 additions and
1 deletions
-
weed/images/resizing.go
-
weed/images/resizing_test.go
-
BIN
weed/images/sample2.webp
-
weed/server/volume_server_handlers_read.go
|
|
@ -11,6 +11,8 @@ import ( |
|
|
|
"github.com/disintegration/imaging" |
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog" |
|
|
|
|
|
|
|
_ "golang.org/x/image/webp" |
|
|
|
) |
|
|
|
|
|
|
|
func Resized(ext string, read io.ReadSeeker, width, height int, mode string) (resized io.ReadSeeker, w int, h int) { |
|
|
@ -47,6 +49,9 @@ func Resized(ext string, read io.ReadSeeker, width, height int, mode string) (re |
|
|
|
jpeg.Encode(&buf, dstImage, nil) |
|
|
|
case ".gif": |
|
|
|
gif.Encode(&buf, dstImage, nil) |
|
|
|
case ".webp": |
|
|
|
// Webp does not have golang encoder.
|
|
|
|
png.Encode(&buf, dstImage) |
|
|
|
} |
|
|
|
return bytes.NewReader(buf.Bytes()), dstImage.Bounds().Dx(), dstImage.Bounds().Dy() |
|
|
|
} else { |
|
|
|
|
|
@ -0,0 +1,23 @@ |
|
|
|
package images |
|
|
|
|
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"io/ioutil" |
|
|
|
"os" |
|
|
|
"testing" |
|
|
|
) |
|
|
|
|
|
|
|
func TestResizing(t *testing.T) { |
|
|
|
fname := "sample2.webp" |
|
|
|
|
|
|
|
dat, _ := ioutil.ReadFile(fname) |
|
|
|
|
|
|
|
resized, _, _ := Resized(".webp", bytes.NewReader(dat), 100, 30, "") |
|
|
|
buf := new(bytes.Buffer) |
|
|
|
buf.ReadFrom(resized) |
|
|
|
|
|
|
|
ioutil.WriteFile("resized1.png", buf.Bytes(), 0644) |
|
|
|
|
|
|
|
os.Remove("resized1.png") |
|
|
|
|
|
|
|
} |
|
|
@ -264,7 +264,7 @@ func conditionallyResizeImages(originalDataReaderSeeker io.ReadSeeker, ext strin |
|
|
|
} |
|
|
|
|
|
|
|
func shouldResizeImages(ext string, r *http.Request) (width, height int, mode string, shouldResize bool) { |
|
|
|
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" { |
|
|
|
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".webp" { |
|
|
|
if r.FormValue("width") != "" { |
|
|
|
width, _ = strconv.Atoi(r.FormValue("width")) |
|
|
|
} |
|
|
|