mirror of https://github.com/usememos/memos.git
chore: set max thumbnail width to home/explore image max width (#3852)
* Set max thumbnail width to timeline img max width * Prevent images less than thumbnail size from being scaled up * Apply suggestions from code review --------- Co-authored-by: boojack <24653555+boojack@users.noreply.github.com>
This commit is contained in:
parent
0156c7e11f
commit
bfe57b9202
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -434,11 +435,19 @@ func (s *APIV1Service) getOrGenerateThumbnail(resource *store.Resource) ([]byte,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to get resource blob")
|
return nil, errors.Wrap(err, "failed to get resource blob")
|
||||||
}
|
}
|
||||||
image, err := imaging.Decode(bytes.NewReader(blob), imaging.AutoOrientation(true))
|
img, err := imaging.Decode(bytes.NewReader(blob), imaging.AutoOrientation(true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to decode thumbnail image")
|
return nil, errors.Wrap(err, "failed to decode thumbnail image")
|
||||||
}
|
}
|
||||||
thumbnailImage := imaging.Resize(image, 512, 0, imaging.Lanczos)
|
|
||||||
|
thumbnailMaxWidth := 700 // equal to home/explore screen image max width
|
||||||
|
var thumbnailImage image.Image
|
||||||
|
if img.Bounds().Max.X > thumbnailMaxWidth {
|
||||||
|
thumbnailImage = imaging.Resize(img, thumbnailMaxWidth, 0, imaging.Lanczos)
|
||||||
|
} else {
|
||||||
|
thumbnailImage = img
|
||||||
|
}
|
||||||
|
|
||||||
if err := imaging.Save(thumbnailImage, filePath); err != nil {
|
if err := imaging.Save(thumbnailImage, filePath); err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to save thumbnail file")
|
return nil, errors.Wrap(err, "failed to save thumbnail file")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue