Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Skip lilliput if below maxThumbnailSize #184

Merged
merged 4 commits into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Skip lilliput if image is below maxThumbnailSize. (#184)

## 1.2.0

- Breaking: YouTube environment variable has been renamed (`CHATTERINO_API_YOUTUBE_API_KEY`).
Expand Down
46 changes: 22 additions & 24 deletions pkg/thumbnail/thumbnail_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,43 +46,41 @@ func buildThumbnailByteArray(inputBuf []byte, resp *http.Response) ([]byte, erro
// If the final image does not fit within this buffer, then we fall back to providing a static thumbnail
outputImg := make([]byte, 2*1024*1024)

// We don't need to resize image nor does it need to be passed through lilliput.
// Only resize if the original image has bigger dimensions than maxThumbnailSize
if newWidth < maxThumbnailSize && newHeight < maxThumbnailSize {
return inputBuf, nil
}

// don't transcode (use existing type)
outputType := "." + strings.ToLower(decoder.Description())

// We want to default to no resizing.
resizeMethod := lilliput.ImageOpsNoResize

// Only trigger if original image has higher values than maxThumbnailSize
if newWidth > maxThumbnailSize || newHeight > maxThumbnailSize {
resizeMethod = lilliput.ImageOpsResize // We want to resize
/* Preserve aspect ratio is from previous module, thanks nfnt/resize.
* (https://github.com/nfnt/resize/blob/83c6a9932646f83e3267f353373d47347b6036b2/thumbnail.go#L27)
*/

/* Preserve aspect ratio is from previous module, thanks nfnt/resize.
* (https://github.com/nfnt/resize/blob/83c6a9932646f83e3267f353373d47347b6036b2/thumbnail.go#L27)
*/

// Preserve aspect ratio
if newWidth > maxThumbnailSize {
newHeight = newHeight * maxThumbnailSize / newWidth
if newHeight < 1 {
newHeight = 1
}
newWidth = maxThumbnailSize
// Preserve aspect ratio
if newWidth > maxThumbnailSize {
newHeight = newHeight * maxThumbnailSize / newWidth
if newHeight < 1 {
newHeight = 1
}
newWidth = maxThumbnailSize
}

if newHeight > maxThumbnailSize {
newWidth = newWidth * maxThumbnailSize / newHeight
if newWidth < 1 {
newWidth = 1
}
newHeight = maxThumbnailSize
if newHeight > maxThumbnailSize {
newWidth = newWidth * maxThumbnailSize / newHeight
if newWidth < 1 {
newWidth = 1
}
newHeight = maxThumbnailSize
}

opts := &lilliput.ImageOptions{
FileType: outputType,
Width: newWidth,
Height: newHeight,
ResizeMethod: resizeMethod,
ResizeMethod: lilliput.ImageOpsResize,
EncodeOptions: encodeOptions[outputType],
}

Expand Down