forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrop_image_handler.go
127 lines (112 loc) · 3.81 KB
/
crop_image_handler.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
package uadmin
import (
"image"
"image/draw"
"image/gif"
"image/jpeg"
"image/png"
"net/http"
"os"
"path"
"strconv"
"strings"
)
func cropImageHandler(w http.ResponseWriter, r *http.Request, session *Session) {
img := "." + r.FormValue("img")
top, _ := strconv.ParseFloat(r.FormValue("top"), 32)
left, _ := strconv.ParseFloat(r.FormValue("left"), 32)
bottom, _ := strconv.ParseFloat(r.FormValue("bottom"), 32)
right, _ := strconv.ParseFloat(r.FormValue("right"), 32)
fType := strings.Split(img, ".")
// check if the user has edit permission for the model
modelNameParts := strings.Split(img, "/")
if len(modelNameParts) < 2 {
ReturnJSON(w, r, map[string]string{"status": "error", "err_msg": "The image path is too short"})
return
}
modelName := modelNameParts[len(modelNameParts)-2]
modelName = strings.Split(modelName, "_")[0]
if !session.User.GetAccess(modelName).Edit {
ReturnJSON(w, r, map[string]string{"status": "error", "err_msg": "You don't have permission to edit this model"})
return
}
// Open the file
img = path.Clean(img)
imageFile, err := os.Open(img)
if err != nil {
ReturnJSON(w, r, map[string]string{"status": "error", "err_msg": "Unable to open image. Check logs for details"})
return
}
defer imageFile.Close()
var myImage image.Image
if strings.ToLower(fType[len(fType)-1]) == "jpg" || strings.ToLower(fType[len(fType)-1]) == "jpeg" {
myImage, err = jpeg.Decode(imageFile)
if err != nil {
ReturnJSON(w, r, map[string]string{"status": "error", "err_msg": "Unable to decode JPEG image. Check logs for details"})
return
}
}
if strings.ToLower(fType[len(fType)-1]) == "png" {
myImage, err = png.Decode(imageFile)
if err != nil {
ReturnJSON(w, r, map[string]string{"status": "error", "err_msg": "Unable to decode PNG image. Check logs for details"})
return
}
}
if strings.ToLower(fType[len(fType)-1]) == "gif" {
myImage, err = gif.Decode(imageFile)
if err != nil {
ReturnJSON(w, r, map[string]string{"status": "error", "err_msg": "Unable to decode GIF image. Check logs for details"})
return
}
}
rect := image.Rect(int(left), int(top), int(right), int(bottom))
mySubImage := image.NewRGBA(rect)
draw.Draw(mySubImage, rect, myImage, image.Point{int(top), int(left)}, draw.Src)
f, err := os.Create(strings.Replace(img, "_raw", "", -1))
if err != nil {
ReturnJSON(w, r, map[string]string{"status": "error", "err_msg": "Unable to create image. Check logs for details"})
return
}
defer f.Close()
err = createImgFile(w, r, fType, f, mySubImage)
if err != nil {
return
}
rawFile, err := os.Create(img)
if err != nil {
ReturnJSON(w, r, map[string]string{"status": "error", "err_msg": "Unable to create raw image. Check logs for details"})
return
}
defer rawFile.Close()
err = createImgFile(w, r, fType, rawFile, mySubImage)
if err != nil {
return
}
ReturnJSON(w, r, map[string]string{"status": "ok"})
}
func createImgFile(w http.ResponseWriter, r *http.Request, fType []string, f *os.File, mySubImage image.Image) (err error) {
if strings.ToLower(fType[len(fType)-1]) == cJPG || strings.ToLower(fType[len(fType)-1]) == cJPEG {
err = jpeg.Encode(f, mySubImage, nil)
if err != nil {
ReturnJSON(w, r, map[string]string{"status": "error", "err_msg": "Unable to encode JPEG image. Check logs for details"})
return err
}
}
if strings.ToLower(fType[len(fType)-1]) == cPNG {
err = png.Encode(f, mySubImage)
if err != nil {
ReturnJSON(w, r, map[string]string{"status": "error", "err_msg": "Unable to encode PNG image. Check logs for details"})
return err
}
}
if strings.ToLower(fType[len(fType)-1]) == cGIF {
o := gif.Options{}
err = gif.Encode(f, mySubImage, &o)
if err != nil {
ReturnJSON(w, r, map[string]string{"status": "error", "err_msg": "Unable to encode GIF image. Check logs for details"})
return err
}
}
return nil
}