-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
images.go
82 lines (67 loc) · 1.99 KB
/
images.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
// Pipe - A small and beautiful blogging platform written in golang.
// Copyright (C) 2017-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package util
import (
"math/rand"
"strconv"
"strings"
"time"
)
// CommunityFileURL is the community file service URL.
const CommunityFileURL = "https://img.hacpai.com"
// ImageSize returns image URL of Qiniu image processing style with the specified width and height.
func ImageSize(imageURL string, width, height int) string {
if strings.Contains(imageURL, "imageView") {
return imageURL
}
return imageURL + "?imageView2/1/w/" + strconv.Itoa(width) + "/h/" + strconv.Itoa(height) + "/interlace/1/q/100"
}
// RandImage returns an image URL randomly for article thumbnail.
// https://github.com/88250/bing
func RandImage() string {
min := time.Date(2017, 11, 04, 0, 0, 0, 0, time.UTC).Unix()
max := time.Now().Unix()
delta := max - min
sec := rand.Int63n(delta) + min
return time.Unix(sec, 0).Format(CommunityFileURL + "/bing/20060102.jpg")
}
// RandImages returns random image URLs.
func RandImages(n int) []string {
var ret []string
i := 0
for {
if i >= n*5 {
break
}
url := RandImage()
if !contains(url, ret) {
ret = append(ret, url)
}
if len(ret) >= n {
return ret
}
i++
}
return ret
}
func contains(str string, slice []string) bool {
for _, s := range slice {
if str == s {
return true
}
}
return false
}