-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageprocessing.go
162 lines (143 loc) · 3.93 KB
/
imageprocessing.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
package imageprocessing
import (
"context"
b64 "encoding/base64"
"encoding/json"
"fmt"
"image"
"image/jpeg"
"math"
"mime"
"net/http"
"cloud.google.com/go/storage"
"github.com/disintegration/imaging"
)
func validateResponse(resp *http.Response, url string) error {
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("Failed to download %v", url)
}
if resp.ContentLength < 0 {
return fmt.Errorf("Couldn't deduce content length from response")
}
sizeMb := float64(resp.ContentLength) / 1024 / 1024
maxSizeMb := 3.0
if sizeMb > maxSizeMb {
return fmt.Errorf("Image is greater than %v MB in size: %v", maxSizeMb, url)
}
return nil
}
func downloadImage(imgUrl string) (image.Image, error) {
resp, err := http.Head(imgUrl)
if err != nil {
return nil, err
}
if err = validateResponse(resp, imgUrl); err != nil {
return nil, err
}
defer resp.Body.Close()
resp, err = http.Get(imgUrl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err = validateResponse(resp, imgUrl); err != nil {
return nil, err
}
img, _, err := image.Decode(resp.Body)
if err != nil {
return nil, err
}
return img, nil
}
// PubSubMessage is the payload of a Pub/Sub event.
type PubSubMessage struct {
Data string `json:"data"`
}
type processingMessage struct {
Url string
EventID string
}
// ProcessImage processes an image found at provided URL.
//
// A thumbnail of the image gets produced.
func ProcessImage(ctx context.Context, m PubSubMessage) error {
payload, err := b64.StdEncoding.DecodeString(m.Data)
if err != nil {
return err
}
var message processingMessage
if err := json.Unmarshal(payload, &message); err != nil {
return err
}
fmt.Printf("received request to download %s\n", message.Url)
img, err := downloadImage(message.Url)
if err != nil {
return err
}
thumb, err := resizeImage(img)
if err != nil {
return err
}
if err := uploadImages(message.EventID, img, thumb); err != nil {
return err
}
return nil
}
func resizeImage(img image.Image) (*image.NRGBA, error) {
targetRatio := 1.5
width := img.Bounds().Dx()
height := img.Bounds().Dy()
sourceRatio := float64(width) / float64(height)
var cropRectangle image.Rectangle
if sourceRatio < targetRatio {
// Crop the image height wise
targetHeight := int(float64(width) / targetRatio)
offset := int(math.Floor(float64(height-targetHeight) / float64(2)))
cropRectangle = image.Rectangle{
Min: image.Point{X: 0, Y: offset},
Max: image.Point{X: width, Y: height - offset},
}
} else if sourceRatio > targetRatio {
// Crop the image width wise
targetWidth := int(targetRatio * float64(height))
offset := int(math.Floor(float64(width-targetWidth) / float64(2)))
cropRectangle = image.Rectangle{
Min: image.Point{X: offset, Y: 0},
Max: image.Point{X: width - offset, Y: height},
}
}
croppedImage := imaging.Crop(img, cropRectangle)
return imaging.Resize(croppedImage, 800, 534, imaging.Lanczos), nil
}
func uploadImage(ctx context.Context, name string, eventID string, bucket *storage.BucketHandle, img image.Image) error {
objPath := fmt.Sprintf("images/events/%s/%s.jpg", eventID, name)
fmt.Printf("Uploading %q\n", objPath)
obj := bucket.Object(objPath)
w := obj.NewWriter(ctx)
w.ACL = []storage.ACLRule{{Entity: storage.AllUsers, Role: storage.RoleReader}}
w.CacheControl = "public, max-age=86400"
w.ContentType = mime.TypeByExtension(".jpg")
if err := jpeg.Encode(w, img, &jpeg.Options{Quality: 100}); err != nil {
w.Close()
return err
}
if err := w.Close(); err != nil {
return err
}
return nil
}
func uploadImages(eventID string, img, thumb image.Image) error {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return err
}
bucket := client.Bucket("arve.experimental.berlin")
if err := uploadImage(ctx, "main", eventID, bucket, img); err != nil {
return err
}
if err := uploadImage(ctx, "thumb", eventID, bucket, thumb); err != nil {
return err
}
return nil
}