Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Use source video dimensions for the thumbnail #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/bakape/thumbnailer/v2
module github.com/abukineyev/thumbnailer/v2

go 1.13

Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ type Options struct {
// Defaults to 150x150, if unset.
ThumbDims Dims

// Use source video dimensions for the thumbnail.
UseSourceDims bool

// MIME types to accept for thumbnailing.
// If nil, all MIME types will be processed.
//
Expand Down
19 changes: 18 additions & 1 deletion thumbnailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,26 @@ func processMedia(rs io.ReadSeeker, src *Source, opts Options,
if err != nil {
return
}
thumb, err = c.Thumbnail(opts.ThumbDims)

var scaleDims Dims
if opts.UseSourceDims {
scaleDims = scaleDimensions(src.Dims, src.Dims.Width)
} else {
scaleDims = scaleDimensions(src.Dims, opts.ThumbDims.Width)
}
thumb, err = c.Thumbnail(scaleDims)
} else {
err = ErrCantThumbnail
}
return
}

func scaleDimensions(dims Dims, scaleToWidth uint) Dims {
if dims.Width == 0 {
return dims
}
factor := float64(scaleToWidth) / float64(dims.Width)
dims.Width = uint(float64(dims.Width) * factor)
dims.Height = uint(float64(dims.Height) * factor)
return dims
}