Skip to content

Commit

Permalink
feat: specify image decoder for hash
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Dec 6, 2024
1 parent 623334e commit 31aaf85
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion internal/apis/applemusic/song.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package applemusic
import (
"bytes"
"fmt"
"image/jpeg"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -92,7 +93,7 @@ func songFromSongResponse(s songResponse) song {
ReleaseDate: s.Attributes.ReleaseDate,
DurationInMillis: s.Attributes.DurationInMillis,
AlbumArtURL: albumArtURL,
AlbumArtBlur: images.BlurDataURI(images.BlurImage(b.Bytes())),
AlbumArtBlur: images.BlurDataURI(images.BlurImage(b.Bytes(), jpeg.Decode)),
URL: s.Attributes.URL,
ID: s.ID,
}
Expand Down
3 changes: 2 additions & 1 deletion internal/apis/strava/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package strava

import (
"fmt"
"image/png"
"net/url"
"time"

Expand Down Expand Up @@ -107,7 +108,7 @@ func fetchActivities(minioClient minio.Client, tokens tokens) ([]activity, error
if a.HasMap {
mapData := fetchMap(stravaActivity.Map.SummaryPolyline)
uploadMap(minioClient, stravaActivity.ID, mapData)
mapBlurURI := images.BlurDataURI(images.BlurImage(mapData))
mapBlurURI := images.BlurDataURI(images.BlurImage(mapData, png.Decode))
a.MapBlurImage = &mapBlurURI
imgurl := fmt.Sprintf(
"https://minio-api.dev.mattglei.ch/mapbox-maps/%d.png",
Expand Down
14 changes: 8 additions & 6 deletions internal/images/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ import (
"bytes"
"encoding/base64"
"fmt"
"image"
"image/png"
"io"

"github.com/buckket/go-blurhash"
"github.com/gleich/lumber/v3"
)

func BlurImage(data []byte) []byte {
func BlurImage(data []byte, decoder func(r io.Reader) (image.Image, error)) []byte {
reader := bytes.NewReader(data)
parsedPNG, err := png.Decode(reader)
parsedImage, err := decoder(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)
width := parsedImage.Bounds().Dx()
height := parsedImage.Bounds().Dy()
blurData, err := blurhash.Encode(4, 3, parsedImage)
if err != nil {
lumber.Error(err, "encoding png into blurhash failed")
lumber.Error(err, "encoding image into blurhash failed")
return nil
}

Expand Down

0 comments on commit 31aaf85

Please sign in to comment.