Skip to content

Commit

Permalink
fix: Skip lilliput if below maxThumbnailSize (#184)
Browse files Browse the repository at this point in the history
Co-authored-by: zneix <zneix@zneix.eu>
  • Loading branch information
KararTY and zneix authored Jul 3, 2021
1 parent 2bfa2f5 commit 292442b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
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

0 comments on commit 292442b

Please sign in to comment.