monchickey
2 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 116 additions and 6 deletions
-
47weed/images/cropping.go
-
22weed/images/cropping_test.go
-
53weed/server/volume_server_handlers_read.go
@ -0,0 +1,47 @@ |
|||||
|
package images |
||||
|
|
||||
|
import ( |
||||
|
"bytes" |
||||
|
"image" |
||||
|
"image/gif" |
||||
|
"image/jpeg" |
||||
|
"image/png" |
||||
|
"io" |
||||
|
|
||||
|
"github.com/disintegration/imaging" |
||||
|
|
||||
|
"github.com/seaweedfs/seaweedfs/weed/glog" |
||||
|
) |
||||
|
|
||||
|
func Cropped(ext string, read io.ReadSeeker, x1, y1, x2, y2 int) (cropped io.ReadSeeker, err error) { |
||||
|
srcImage, _, err := image.Decode(read) |
||||
|
if err != nil { |
||||
|
glog.Error(err) |
||||
|
return read, err |
||||
|
} |
||||
|
|
||||
|
bounds := srcImage.Bounds() |
||||
|
if x2 > bounds.Dx() || y2 > bounds.Dy() { |
||||
|
read.Seek(0, 0) |
||||
|
return read, nil |
||||
|
} |
||||
|
|
||||
|
rectangle := image.Rect(x1, y1, x2, y2) |
||||
|
dstImage := imaging.Crop(srcImage, rectangle) |
||||
|
var buf bytes.Buffer |
||||
|
switch ext { |
||||
|
case ".jpg", ".jpeg": |
||||
|
if err = jpeg.Encode(&buf, dstImage, nil); err != nil { |
||||
|
glog.Error(err) |
||||
|
} |
||||
|
case ".png": |
||||
|
if err = png.Encode(&buf, dstImage); err != nil { |
||||
|
glog.Error(err) |
||||
|
} |
||||
|
case ".gif": |
||||
|
if err = gif.Encode(&buf, dstImage, nil); err != nil { |
||||
|
glog.Error(err) |
||||
|
} |
||||
|
} |
||||
|
return bytes.NewReader(buf.Bytes()), err |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
package images |
||||
|
|
||||
|
import ( |
||||
|
"bytes" |
||||
|
"os" |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/seaweedfs/seaweedfs/weed/util" |
||||
|
) |
||||
|
|
||||
|
func TestCropping(t *testing.T) { |
||||
|
fname := "sample1.jpg" |
||||
|
|
||||
|
dat, _ := os.ReadFile(fname) |
||||
|
|
||||
|
cropped, _ := Cropped(".jpg", bytes.NewReader(dat), 1072, 932, 1751, 1062) |
||||
|
buf := new(bytes.Buffer) |
||||
|
buf.ReadFrom(cropped) |
||||
|
|
||||
|
util.WriteFile("cropped1.jpg", buf.Bytes(), 0644) |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue