-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproductimage.go
81 lines (73 loc) · 1.49 KB
/
productimage.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
package main
import (
"regexp"
"strconv"
"strings"
)
type ProductImage struct {
ImagePath string
ProductId string
ImageId string
ImageName string
Size string
Dpr bool
Ext string
}
type ImageSize struct {
Width int
Height int
Mode string
}
func ParseProductImageUrl(urlPath string) ProductImage {
var re = regexp.MustCompile(productRe)
matches := re.FindAllStringSubmatch(urlPath, -1)[0]
image := ProductImage{
ImagePath: matches[1],
ProductId: matches[2],
ImageId: matches[3],
ImageName: matches[4],
Size: matches[5],
//Dpr: matches[6],
Ext: matches[7],
}
if matches[6] != "" {
image.Dpr = true
}
return image
}
func GetOriginalProductImagePath(image ProductImage) string {
n := ""
if image.ImageId == image.ImageName {
n = image.ImageId
} else {
n = image.ImageId + "." + image.ImageName
}
file := "/wa-data/protected/shop/products/" + image.ImagePath + n + "." + image.Ext
return file
}
func ParseSize(size string, dpr bool) ImageSize {
mode := "unknown"
arSize := strings.Split(size, "x")
height := 0
width, _ := strconv.Atoi(arSize[0])
if len(arSize) == 2 {
height, _ = strconv.Atoi(arSize[1])
}
if len(arSize) == 1 {
mode = "max"
height = width
} else if width == height {
mode = "crop"
} else if width != 0 && height != 0 {
mode = "rectangle"
} else if width == 0 {
mode = "height"
} else if height == 0 {
mode = "width"
}
return ImageSize{
Width: width,
Height: height,
Mode: mode,
}
}