Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PNG compression - larger then before #88

Open
loeffel-io opened this issue Mar 30, 2021 · 2 comments
Open

PNG compression - larger then before #88

loeffel-io opened this issue Mar 30, 2021 · 2 comments

Comments

@loeffel-io
Copy link

Hey, i am currently try to compress png and jpg images with lilliput.

While i am uploading pngs the png file sizes are larger then before - with all kind of compression levels (1 to 9):

var EncodeOptions = map[string]map[int]int{
	matchers.TypeJpeg.Extension: {lilliput.JpegQuality: 85},
	matchers.TypePng.Extension:  {lilliput.PngCompression: 9},
}

var fileType types.Type
if fileType = filetype.MatchMap(buf.Bytes(), matchers.Map{
    matchers.TypeJpeg: matchers.Jpeg,
    matchers.TypePng:  matchers.Png,
}); fileType == filetype.Unknown {
    c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(fmt.Errorf("filetype unsupported"), nil))
    return
}

if decoder, err = lilliput.NewDecoder(buf.Bytes()); err != nil {
    c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
    return
}

defer func() {
    decoder.Close()
}()

if header, err = decoder.Header(); err != nil {
    c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
    return
}

var ops = lilliput.NewImageOps(8192)

defer func() {
    ops.Close()
}()

var opts = &lilliput.ImageOptions{
    FileType:             fmt.Sprintf(".%s", fileType.Extension),
    Width:                header.Width(),
    Height:               header.Height(),
    ResizeMethod:         lilliput.ImageOpsResize,
    NormalizeOrientation: false,
    EncodeOptions:        EncodeOptions[fileType.Extension],
}

var data = make([]byte, maxSize)

if data, err = ops.Transform(decoder, opts, data); err != nil {
    c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
    return
}

log.Printf("%d:%d", len(buf.Bytes()), len(data)) // 112319:270094

Before (112 KB)

wonder-day-among-us-21

After (270 KB)

profile (2)

pngCompression 1

the result is 112319:408167

@yyong37
Copy link

yyong37 commented Apr 1, 2021

the origin image is png8 to png32.. so the image is larger. so if have some func keep png8~ everything will be ok.

@loeffel-io
Copy link
Author

so there is no support for png8?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants