Skip to content

Commit

Permalink
chore: use ratio instead of max width
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Sep 9, 2024
1 parent 76a9a34 commit 4790e84
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions server/router/api/v1/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ func (s *APIV1Service) GetResourceBlob(resource *store.Resource) ([]byte, error)
}

const (
// thumbnailMaxWidth is the maximum width of the thumbnail image.
thumbnailMaxWidth = 700
// thumbnailRatio is the ratio of the thumbnail image.
thumbnailRatio = 0.8
)

// getOrGenerateThumbnail returns the thumbnail image of the resource.
Expand All @@ -434,13 +434,9 @@ func (s *APIV1Service) getOrGenerateThumbnail(resource *store.Resource) ([]byte,
return nil, errors.Wrap(err, "failed to decode thumbnail image")
}

// If the image is smaller than the thumbnailMaxWidth, return the original image.
if img.Bounds().Max.X < thumbnailMaxWidth {
return blob, nil
}

// Resize the image to the thumbnailMaxWidth.
thumbnailImage := imaging.Resize(img, thumbnailMaxWidth, 0, imaging.Lanczos)
thumbnailWidth := int(float64(img.Bounds().Dx()) * thumbnailRatio)
// Resize the image to the thumbnailWidth.
thumbnailImage := imaging.Resize(img, thumbnailWidth, 0, imaging.Lanczos)
if err := imaging.Save(thumbnailImage, filePath); err != nil {
return nil, errors.Wrap(err, "failed to save thumbnail file")
}
Expand Down

2 comments on commit 4790e84

@RoccoSmit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the ration be set lower than 0.8 (e.g. 0.2) or can we have a setting to change the ration manually.

The goal of creating thumbnails is to limit the amount of data going over the network to speed up the page/image loading.
If we only make the files a little bit smaller the page load time doesnt go down much and our storage usage increases at almost double the rate with these larger thumbnails

e.g.
in v0.25 uploading a 4000 x 3000px image (camera photo) would store a 170kb thumbnail and a 3.5mb image.
with the above change the same image now stores a 2.9mb thumbnail and a 3.5mb image.
That difference in total thumbnail size escalates quickly as more images are added

As an alternative, if the thumbnail max width is used again and is upped from 700px to 900px then the widest view area in the site is covered and the thumbnails stay small in size

@boojack
Copy link
Member Author

@boojack boojack commented on 4790e84 Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RoccoSmit Good points, but I feel like it could be implemented after someone gives feedback. Let's keep it simple first! And I will change the default ratio to 0.5.

Please sign in to comment.