mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: use ratio instead of max width
This commit is contained in:
@@ -408,8 +408,8 @@ func (s *APIV1Service) GetResourceBlob(resource *store.Resource) ([]byte, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// thumbnailMaxWidth is the maximum width of the thumbnail image.
|
// thumbnailRatio is the ratio of the thumbnail image.
|
||||||
thumbnailMaxWidth = 700
|
thumbnailRatio = 0.8
|
||||||
)
|
)
|
||||||
|
|
||||||
// getOrGenerateThumbnail returns the thumbnail image of the resource.
|
// getOrGenerateThumbnail returns the thumbnail image of the resource.
|
||||||
@@ -434,13 +434,9 @@ func (s *APIV1Service) getOrGenerateThumbnail(resource *store.Resource) ([]byte,
|
|||||||
return nil, errors.Wrap(err, "failed to decode thumbnail image")
|
return nil, errors.Wrap(err, "failed to decode thumbnail image")
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the image is smaller than the thumbnailMaxWidth, return the original image.
|
thumbnailWidth := int(float64(img.Bounds().Dx()) * thumbnailRatio)
|
||||||
if img.Bounds().Max.X < thumbnailMaxWidth {
|
// Resize the image to the thumbnailWidth.
|
||||||
return blob, nil
|
thumbnailImage := imaging.Resize(img, thumbnailWidth, 0, imaging.Lanczos)
|
||||||
}
|
|
||||||
|
|
||||||
// Resize the image to the thumbnailMaxWidth.
|
|
||||||
thumbnailImage := imaging.Resize(img, thumbnailMaxWidth, 0, imaging.Lanczos)
|
|
||||||
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")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user