feat(backgrounds): resize images to a maximum of 4K
Resolves https://kolaente.dev/vikunja/vikunja/issues/1373#issuecomment-43491
This commit is contained in:
@ -32,6 +32,8 @@ type Image struct {
|
||||
Info interface{} `json:"info,omitempty"`
|
||||
}
|
||||
|
||||
const MaxBackgroundImageHeight = 3840
|
||||
|
||||
// Provider represents something that is able to get a project of images and set one of them as background
|
||||
type Provider interface {
|
||||
// Search is used to either return a pre-defined project of Image or let the user search for an image
|
||||
|
@ -17,10 +17,13 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "image/gif" // To make sure the decoder used for generating blurHashes recognizes gifs
|
||||
_ "image/jpeg" // To make sure the decoder used for generating blurHashes recognizes jpgs
|
||||
_ "image/png" // To make sure the decoder used for generating blurHashes recognizes pngs
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
|
||||
_ "golang.org/x/image/bmp" // To make sure the decoder used for generating blurHashes recognizes bmps
|
||||
_ "golang.org/x/image/tiff" // To make sure the decoder used for generating blurHashes recognizes tiffs
|
||||
_ "golang.org/x/image/webp" // To make sure the decoder used for generating blurHashes recognizes tiffs
|
||||
@ -220,7 +223,30 @@ func (bp *BackgroundProvider) UploadBackground(c echo.Context) error {
|
||||
|
||||
func SaveBackgroundFile(s *xorm.Session, auth web.Auth, project *models.Project, srcf io.ReadSeeker, filename string, filesize uint64) (err error) {
|
||||
_, _ = srcf.Seek(0, io.SeekStart)
|
||||
f, err := files.Create(srcf, filename, filesize, auth)
|
||||
src, err := imaging.Decode(srcf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, _ = srcf.Seek(0, io.SeekStart)
|
||||
imgConfig, _, err := image.DecodeConfig(srcf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
height := imgConfig.Height
|
||||
if imgConfig.Height > background.MaxBackgroundImageHeight {
|
||||
height = background.MaxBackgroundImageHeight
|
||||
}
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
dst := imaging.Resize(src, 0, height, imaging.Lanczos)
|
||||
err = imaging.Encode(&buf, dst, imaging.JPEG, imaging.JPEGQuality(80))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f, err := files.Create(&buf, filename, filesize, auth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -256,7 +256,7 @@ func (p *Provider) Set(s *xorm.Session, image *background.Image, project *models
|
||||
|
||||
// Download the photo from unsplash
|
||||
// The parameters crop the image to a max width of 2560 and a max height of 2048 to save bandwidth and storage.
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, photo.Urls.Raw+"&w=2560&h=2048&q=90", nil)
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, photo.Urls.Raw+"&fm=jpg&h="+strconv.FormatInt(background.MaxBackgroundImageHeight, 10)+"&q=80", nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user