Skip to content

Commit

Permalink
webp以外の変換処理もbimg使ってみる
Browse files Browse the repository at this point in the history
Signed-off-by: drumato <[email protected]>
  • Loading branch information
Drumato committed Apr 11, 2024
1 parent 7599e4d commit 75913bc
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@ package main

import (
"bytes"
"image/jpeg"
"io"

"github.com/disintegration/imaging"
"github.com/h2non/bimg"
)

func convert(src io.Reader, q int) (*bytes.Buffer, error) {
img, err := imaging.Decode(src, imaging.AutoOrientation(true))
out, err := io.ReadAll(src)
if err != nil {
return nil, err
}
img, err := bimg.NewImage(out).AutoRotate()
if err != nil {
return nil, err
}

buf := new(bytes.Buffer)

if err := jpeg.Encode(buf, img, &jpeg.Options{Quality: quality}); err != nil {
processed, err := bimg.NewImage(img).Process(bimg.Options{Quality: quality})
if err != nil {
return nil, err
}
jpegImg, err := bimg.NewImage(processed).Convert(bimg.JPEG)
if err != nil {
return nil, err
}

return buf, nil
return bytes.NewBuffer(jpegImg), nil
}

0 comments on commit 75913bc

Please sign in to comment.