-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
size.go
96 lines (79 loc) Β· 3.54 KB
/
size.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package thumb
import (
"image"
)
// Size represents a standard media resolution.
type Size struct {
Name Name `json:"name"` // Name of the thumbnail size.
Source Name `json:"-"` // Larger size this size can be generated from.
Usage string `json:"usage"` // Usage example.
Width int `json:"w"` // Width in pixels.
Height int `json:"h"` // Height in pixels.
Public bool `json:"-"` // Size is visible in client applications.
Fit bool `json:"-"` // Image is fitted to fill this size.
Optional bool `json:"-"` // Size must not be generated by default.
Required bool `json:"-"` // Size must always be generated.
Options Options `json:"-"`
}
// Bounds returns the thumb size as image.Rectangle.
func (s Size) Bounds() image.Rectangle {
return image.Rectangle{Min: image.Point{}, Max: image.Point{X: s.Width, Y: s.Height}}
}
// Uncached tests if thumbnail type exceeds the cached thumbnails size limit.
func (s Size) Uncached() bool {
return s.Width > SizeCached || s.Height > SizeCached
}
// ExceedsLimit tests if thumbnail type is too large, and can not be rendered at all.
func (s Size) ExceedsLimit() bool {
return s.Width > MaxSize() || s.Height > MaxSize()
}
// FromCache returns the filename if a thumbnail image with the matching size is in the cache.
func (s Size) FromCache(fileName, fileHash, cachePath string) (string, error) {
return FromCache(fileName, fileHash, cachePath, s.Width, s.Height, s.Options...)
}
// FromFile generates a new thumbnail with the requested size, if it does not already exist, and returns its filename.
func (s Size) FromFile(fileName, fileHash, cachePath string, fileOrientation int) (string, error) {
return FromFile(fileName, fileHash, cachePath, s.Width, s.Height, fileOrientation, s.Options...)
}
// Create creates a thumbnail with the matching size and returns it as image.Image.
func (s Size) Create(img image.Image, fileName string) (image.Image, error) {
return Create(img, fileName, s.Width, s.Height, s.Options...)
}
// FileName returns the file name of the thumbnail for the matching size.
func (s Size) FileName(hash, thumbPath string) (string, error) {
return FileName(hash, thumbPath, s.Width, s.Height, s.Options...)
}
// ResolvedName returns the file name of the thumbnail for the matching size with all symlinks resolved.
func (s Size) ResolvedName(hash, thumbPath string) (string, error) {
return ResolvedName(hash, thumbPath, s.Width, s.Height, s.Options...)
}
// Skip tests if this size can be skipped when generating thumbnails, e.g. because it is larger than the original.
func (s Size) Skip(img image.Image) bool {
return Skip(s, img.Bounds())
}
// Skip tests if the size can be skipped when generating thumbnails, e.g. because it is larger than the original.
func Skip(s Size, bounds image.Rectangle) bool {
// Always return false if this thumbnail size is always required.
if s.Required {
return false
}
// Optional sizes can be skipped by default.
if s.Optional {
return true
}
// Skip square thumbnails that show a crop on the left or right if the image is square as well.
if bounds.Max.X == bounds.Max.Y && s.Width == s.Height {
if s.Options.Contains(ResampleFillTopLeft) || s.Options.Contains(ResampleFillBottomRight) {
return true
}
}
// Check if image is within the bounds of this thumbnail size or is fitted to it.
if !s.Fit || !bounds.In(s.Bounds()) {
return false
}
// Skip if the image is smaller than this thumbnail size.
if newSize := FitBounds(bounds); newSize.Width < s.Width {
return true
}
return false
}