Skip to content

Commit

Permalink
feat: 增加了imgurl的图片压缩
Browse files Browse the repository at this point in the history
  • Loading branch information
clearmann committed Nov 6, 2024
1 parent ed3fe5c commit f310457
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
Binary file modified _examples/document/image/image.docx
Binary file not shown.
2 changes: 1 addition & 1 deletion _examples/document/image/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
if err != nil {
log.Fatalf("unable to create image: %s", err)
}
img3, err := common.ImageFromURL("https://test-ccnerf-1251908240.cos.ap-beijing.myqcloud.com/user/avatar/20241023-21-rS20F82/6d46ff86dfd449a7116f8acdc18a867.png")
img3, err := common.ImageFromURL("https://test-ccnerf-1251908240.cos.ap-beijing.myqcloud.com/user/general/20241105-21-IZwqAc5/Capture001.png")

img1ref, err := doc.AddImage(img1)
if err != nil {
Expand Down
23 changes: 21 additions & 2 deletions common/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import (
"bytes"
"fmt"
"image"
"image/jpeg"
"image/png"
"io"
"net/http"
"os"

"github.com/clearmann/gooxml/measurement"

// Add image format support
_ "image/gif"
_ "image/jpeg"
Expand Down Expand Up @@ -151,8 +154,24 @@ func ImageFromURL(url string) (Image, error) {
if err != nil {
return r, fmt.Errorf("unable to parse image: %s", err)
}

r.Data = &data
// Encode the image back to a byte slice with optimized compression
var buf bytes.Buffer
switch ifmt {
case "jpeg":
// Use the standard JPEG encoder with specified quality
err = jpeg.Encode(&buf, imgDec, &jpeg.Options{Quality: 50})
case "png":
// Use the standard PNG encoder with default compression
err = png.Encode(&buf, imgDec)
default:
return r, fmt.Errorf("unsupported image format: %s", ifmt)
}
if err != nil {
return r, fmt.Errorf("error encoding image: %s", err)
}
// Update the image data
compressedData := buf.Bytes()
r.Data = &compressedData
r.Format = ifmt
r.Size = imgDec.Bounds().Size()
return r, nil
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/clearmann/gooxml

go 1.13

require (
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/pixiv/go-libjpeg v0.0.0-20190822045933-3da21a74767d
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pixiv/go-libjpeg v0.0.0-20190822045933-3da21a74767d h1:ls+7AYarUlUSetfnN/DKVNcK6W8mQWc6VblmOm4XwX0=
github.com/pixiv/go-libjpeg v0.0.0-20190822045933-3da21a74767d/go.mod h1:DO7ixpslN6XfbWzeNH9vkS5CF2FQUX81B85rYe9zDxU=

0 comments on commit f310457

Please sign in to comment.