Used a more efficient image resizing library

This commit is contained in:
2024-02-29 10:31:26 -07:00
parent d52eec5241
commit 981ec34e8a
4 changed files with 34 additions and 52 deletions

View File

@ -4,35 +4,34 @@ import (
"bytes"
"errors"
"image"
_ "image/jpeg"
"image/png"
"io"
"mime/multipart"
"github.com/anthonynsimon/bild/transform"
"github.com/disintegration/imaging"
)
func ResizeImg(file multipart.File, width int, height int) ([]byte, error) {
// Read file content
fileContent, err := io.ReadAll(file)
if err != nil {
return nil, errors.New("Error reading image file")
return nil, errors.New("error reading image file")
}
// Decode image
img, _, err := image.Decode(bytes.NewReader(fileContent))
if err != nil {
println(err.Error())
return nil, errors.New("Error decoding image")
return nil, errors.New("error decoding image")
}
// Resize the image
resizedImg := transform.Resize(img, width, height, transform.Linear)
resizedImg := imaging.Resize(img, width, height, imaging.Lanczos)
// Encode the resized image as PNG
buf := new(bytes.Buffer)
if err := png.Encode(buf, resizedImg); err != nil {
return nil, errors.New("Error encoding image to PNG")
return nil, errors.New("error encoding image to PNG")
}
// Return the resized image as response

View File

@ -3,25 +3,37 @@ package lib
import (
"context"
"os"
"sync"
"atri.dad/lib/pubsub"
"github.com/zmb3/spotify"
"golang.org/x/oauth2"
)
var spotifyOAuth2Endpoint = oauth2.Endpoint{
TokenURL: "https://accounts.spotify.com/api/token",
AuthURL: "https://accounts.spotify.com/authorize",
var (
spotifyOAuth2Endpoint = oauth2.Endpoint{
TokenURL: "https://accounts.spotify.com/api/token",
AuthURL: "https://accounts.spotify.com/authorize",
}
config *oauth2.Config
once sync.Once
)
func GetOAuth2Config(clientID string, clientSecret string) *oauth2.Config {
once.Do(func() {
config = &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Scopes: []string{spotify.ScopeUserReadCurrentlyPlaying},
Endpoint: spotifyOAuth2Endpoint,
}
})
return config
}
func GetCurrentlyPlayingTrack(clientID string, clientSecret string, refreshToken string) (*spotify.CurrentlyPlaying, error) {
// OAuth2 config
config := &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Scopes: []string{spotify.ScopeUserReadCurrentlyPlaying},
Endpoint: spotifyOAuth2Endpoint,
}
config := GetOAuth2Config(clientID, clientSecret)
// Token source
tokenSource := config.TokenSource(context.Background(), &oauth2.Token{RefreshToken: refreshToken})