-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
165 lines (120 loc) · 3.15 KB
/
image.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package streams
import (
"github.com/benpate/hannibal/vocab"
"github.com/benpate/rosetta/convert"
)
// https://www.w3.org/ns/activitystreams#Image
type Image struct {
value any
}
// NewImage creates a new Image object from a JSON-LD value (string, map[string]any, or []any)
func NewImage(value any) Image {
switch typed := value.(type) {
case Document:
return NewImage(typed.value)
case Image:
return typed
case string:
return Image{value: typed}
case map[string]any:
return Image{value: typed}
case []any:
return Image{value: typed}
}
return Image{""}
}
// https://www.w3.org/TR/activitystreams-vocabulary/#dfn-href
func (image Image) URL() string {
switch typed := image.value.(type) {
case string:
return typed
case map[string]any:
if url := convert.String(typed[vocab.PropertyURL]); url != "" {
return url
}
if href := convert.String(typed[vocab.PropertyHref]); href != "" {
return href
}
case []any:
if len(typed) > 0 {
return NewImage(typed[0]).URL()
}
}
return ""
}
// https://www.w3.org/TR/activitystreams-vocabulary/#dfn-summary
func (image Image) Summary() string {
switch typed := image.value.(type) {
case map[string]any:
return convert.String(typed[vocab.PropertySummary])
case []any:
if len(typed) > 0 {
return NewImage(typed[0]).Summary()
}
}
return ""
}
// https://www.w3.org/TR/activitystreams-vocabulary/#dfn-mediatype
func (image Image) MediaType() string {
switch typed := image.value.(type) {
case map[string]any:
return convert.String(typed[vocab.PropertyMediaType])
case []any:
if len(typed) > 0 {
return NewImage(typed[0]).MediaType()
}
}
return ""
}
// https://www.w3.org/TR/activitystreams-vocabulary/#dfn-height
func (image Image) Height() int {
switch typed := image.value.(type) {
case map[string]any:
return convert.Int(typed[vocab.PropertyHeight])
case []any:
if len(typed) > 0 {
return NewImage(typed[0]).Height()
}
}
return 0
}
// https://www.w3.org/TR/activitystreams-vocabulary/#dfn-width
func (image Image) Width() int {
switch typed := image.value.(type) {
case map[string]any:
return convert.Int(typed[vocab.PropertyWidth])
case []any:
if len(typed) > 0 {
return NewImage(typed[0]).Width()
}
}
return 0
}
// IsNil returns TRUE if this image is nil (having no URL)
func (image Image) IsNil() bool {
return image.URL() == ""
}
// NotNil returns TRUE if this image has a URL
func (image Image) NotNil() bool {
return !image.IsNil()
}
// HasHeight returns TRUE if this image has a height defined
func (image Image) HasHeight() bool {
return image.Height() > 0
}
// HasWidth returns TRUE if this image has a width defined
func (image Image) HasWidth() bool {
return image.Width() > 0
}
// HasDimensions returns TRUE if this image has both a height and width defined
func (image Image) HasDimensions() bool {
return image.HasHeight() && image.HasWidth()
}
// AspectRatio calculates the aspect ratio of the image (width / height)
// If height and width are not available, then 0 is returned
func (image Image) AspectRatio() float64 {
if image.HasDimensions() {
return float64(image.Width()) / float64(image.Height())
}
return 0
}