-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate blur data URIs for songs
Signed-off-by: Matt Gleich <[email protected]>
- Loading branch information
Showing
4 changed files
with
75 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package images | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"fmt" | ||
"image/png" | ||
|
||
"github.com/buckket/go-blurhash" | ||
"github.com/gleich/lumber/v3" | ||
) | ||
|
||
func BlurImage(data []byte) []byte { | ||
reader := bytes.NewReader(data) | ||
parsedPNG, err := png.Decode(reader) | ||
if err != nil { | ||
lumber.Error(err, "decoding PNG failed") | ||
return nil | ||
} | ||
|
||
width := parsedPNG.Bounds().Dx() | ||
height := parsedPNG.Bounds().Dy() | ||
blurData, err := blurhash.Encode(4, 3, parsedPNG) | ||
if err != nil { | ||
lumber.Error(err, "encoding png into blurhash failed") | ||
return nil | ||
} | ||
|
||
scaleDownFactor := 25 | ||
blurImage, err := blurhash.Decode(blurData, width/scaleDownFactor, height/scaleDownFactor, 1) | ||
if err != nil { | ||
lumber.Error(err, "decoding blurhash data into img failed") | ||
return nil | ||
} | ||
blurImageBuffer := new(bytes.Buffer) | ||
err = png.Encode(blurImageBuffer, blurImage) | ||
if err != nil { | ||
lumber.Error(err, "creating png based off blurred image failed") | ||
return nil | ||
} | ||
return blurImageBuffer.Bytes() | ||
} | ||
|
||
func BlurDataURI(data []byte) string { | ||
return fmt.Sprintf("data:image/png;base64,%s", base64.StdEncoding.EncodeToString(data)) | ||
} |