Skip to content

Commit

Permalink
Support decode blurhash
Browse files Browse the repository at this point in the history
  • Loading branch information
thasophearak committed Mar 25, 2023
1 parent 9dd36c8 commit 9fdf271
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
41 changes: 41 additions & 0 deletions actions/decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package actions

import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"image/jpeg"
"math"

"github.com/bbrks/go-blurhash"
"github.com/urfave/cli/v2"
)

func Decode(ctx *cli.Context) error {
bh := ctx.String("blurhash")

if bh != "" {
width := ctx.Float64("width")
height := ctx.Float64("height")

ratio := math.Min(32/float64(width), 32/float64(height))

img, err := blurhash.Decode(bh, int(width*ratio), int(height*ratio), 1)
if err != nil {
return err
}

xsb := new(bytes.Buffer)
jpeg.Encode(xsb, img, &jpeg.Options{Quality: 95})
fmt.Printf("data:image/jpeg;charset=utf-8;base64,%v", base64.StdEncoding.EncodeToString(xsb.Bytes()))

}

th := ctx.String("thumbhash")
if th != "" {
return errors.New("not support yet")
}

return nil
}
21 changes: 21 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ func main() {
},
},
},
{
Name: "decode",
Aliases: []string{"d"},
Action: actions.Decode,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "blurhash",
Value: "",
},
&cli.StringFlag{
Name: "thumbhash",
Value: "",
},
&cli.Float64Flag{
Name: "width",
},
&cli.Float64Flag{
Name: "height",
},
},
},
{
Name: "version",
Aliases: []string{"v"},
Expand Down

0 comments on commit 9fdf271

Please sign in to comment.