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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
var messages = {
is_processing: "Running semantic segmentation...",
upload_failed: "Error attempting to upload the file.",
upload_cancelled: "Upload cancelled or browser dropped connection.",
unable_to_compute: "We're sorry! We were unable to compute your image.",
pending: "Sending to Generative Adversarial Network...",
complete: "Processing complete!",
}
var upload = (function(){
var upload = {}
var uploading = false
var MAX_SIDE = 512
upload.init = function(){
upload.bind()
}
upload.bind = function(){
$("input[type=file]").on('change', upload.change)
$("#upload_btn").on('click', upload.go)
document.body.addEventListener("dragover", upload.dragover)
document.body.addEventListener("dragleave", upload.dragover)
document.body.addEventListener("drop", upload.change)
}
upload.dragover = function(e){
e.stopPropagation()
e.preventDefault()
}
upload.change = function(e){
e.preventDefault()
var files = e.dataTransfer ? e.dataTransfer.files : e.target.files
if (files.length) {
var file = files[files.length - 1]
if (!file.type.match('image.*'))
return
var reader = new FileReader()
reader.onload = onReaderLoad
reader.readAsDataURL(file)
}
function onReaderLoad(e) {
// Don't leak!
reader.onload = null
var img = new Image
img.onload = function(){
img.onload = null
upload.ready(img)
}
img.src = e.target.result
}
}
upload.ready = function(img){
var resized = renderToCanvas(img, { correctOrientation: true })
var canvas = document.querySelector('#user_photo_canvas')
ctx = canvas.getContext('2d')
ctx.fillStyle = 'black'
ctx.fillRect(0, 0, MAX_SIDE, MAX_SIDE)
var x_offset = (MAX_SIDE - resized.width) / 2
var y_offset = (MAX_SIDE - resized.height) / 2
ctx.drawImage(resized, x_offset, y_offset)
app.didPickPhoto()
}
upload.go = function(){
if (uploading) return
uploading = true
app.didClickUpload()
try {
var canvas = document.querySelector('#user_photo_canvas')
var cb = canvas.toBlob(function(blob){
upload.send(blob)
}, 'image/jpeg', 89)
} catch(e){
app.updateProgress(messages.unable_to_compute)
}
}
upload.send = function(blob){
console.log("sending upload...")
var fd = new FormData()
fd.append('user_image', blob)
fd.append('ext', 'jpg')
fd.append('style', $("#dropdown").val())
fd.append('agree', $("#agree").val() || 0)
var xhr = new XMLHttpRequest()
xhr.upload.addEventListener("progress", upload.progress, false)
xhr.addEventListener("load", upload.complete, false)
xhr.addEventListener("error", upload.failed, false)
xhr.addEventListener("abort", upload.canceled, false)
xhr.open("POST", "/upload")
xhr.send(fd)
}
upload.progress = function (e) {
if (e.lengthComputable) {
var percentComplete = Math.round(e.loaded * 100 / e.total)
if (percentComplete > 99) {
app.updateProgress(messages.is_processing)
} else {
app.updateProgress("Uploaded " + percentComplete.toString() + '%')
}
}
else {
app.updateProgress(messages.unable_to_compute)
}
}
upload.complete = function (e) {
uploading = false
try {
var data = JSON.parse(e.target.responseText)
} catch (e) {
return app.updateProgress(messages.upload_failed)
}
app.uploadDidComplete()
upload.data = data
upload.task_progress(data.task_url)
}
upload.failed = function (evt) {
uploading = false
app.updateProgress(messages.upload_failed)
}
upload.cancelled = function (evt) {
uploading = false
app.updateProgress(messages.upload_cancelled)
}
upload.task_progress = function (status_url) {
var is_public = $("#agree").val() || 0
var uuid = upload.data.uuid
$.getJSON(status_url, function(data){
console.log(data)
var alive = true
var delay = 500
switch(data.state) {
case 'PENDING':
app.updateProgress(messages.pending)
delay = 2000
break
case 'PROCESSING':
app.updateProgress(data.message, data.percent)
delay = 500
break
case 'SUCCESS':
app.updateProgress(messages.complete)
if (is_public) {
history.pushState({}, 'DullDream', '/d/' + uuid)
} else {
history.pushState({}, 'DullDream', '/p/' + uuid)
}
app.processingComplete(uuid, is_public) // truthy if private
alive = false
break
default:
// NB: error state
alive = false
break
}
if (alive) {
setTimeout(function() {
upload.task_progress(status_url)
}, delay)
}
})
}
function renderToCanvas(img, options) {
if (!img) return
options = options || {}
// Canvas max size for any side
var maxSize = MAX_SIDE
var canvas = document.createElement('canvas')
var ctx = canvas.getContext('2d')
var initialScale = options.scale || 1
// Scale to needed to constrain canvas to max size
var scale = getScale(img.width * initialScale, img.height * initialScale, maxSize, maxSize, true)
// Still need to apply the user defined scale
scale *= initialScale
var width = canvas.width = Math.round(img.width * scale)
var height = canvas.height = Math.round(img.height * scale)
var correctOrientation = options.correctOrientation
var jpeg = !!img.src.match(/data:image\/jpeg|\.jpeg$|\.jpg$/i)
var hasDataURI = !!img.src.match(/^data:/)
ctx.save()
// Can only correct orientation on JPEGs represented as dataURIs
// for the time being
if (correctOrientation && jpeg && hasDataURI) {
applyOrientationCorrection(canvas, ctx, img.src)
}
// Resize image if too large
if (scale !== 1) {
ctx.scale(scale, scale)
}
ctx.drawImage(img, 0, 0)
ctx.restore()
return canvas
}
function getScale(width, height, viewportWidth, viewportHeight, fillViewport) {
fillViewport = !!fillViewport
var landscape = (width / height) > (viewportWidth / viewportHeight)
if (landscape) {
if (fillViewport) {
return fitVertical()
} else if (width > viewportWidth) {
return fitHorizontal()
}
} else {
if (fillViewport) {
return fitHorizontal()
} else if (height > viewportHeight) {
return fitVertical()
}
}
return 1
function fitHorizontal() {
return viewportWidth / width
}
function fitVertical() {
return viewportHeight / height
}
}
function applyOrientationCorrection(canvas, ctx, uri) {
var orientation = getOrientation(uri)
// Only apply transform if there is some non-normal orientation
if (orientation && orientation !== 1) {
var transform = orientationToTransform[orientation]
var rotation = transform.rotation
var mirror = transform.mirror
var flipAspect = rotation === 90 || rotation === 270
if (flipAspect) {
// Fancy schmancy swap algo
canvas.width = canvas.height + canvas.width
canvas.height = canvas.width - canvas.height
canvas.width -= canvas.height
}
if (rotation > 0) {
applyRotation(canvas, ctx, rotation)
}
}
}
function applyRotation(canvas, ctx, deg) {
var radians = deg * (Math.PI / 180)
if (deg === 90) {
ctx.translate(canvas.width, 0)
} else if (deg === 180) {
ctx.translate(canvas.width, canvas.height)
} else if (deg == 270) {
ctx.translate(0, canvas.height)
}
ctx.rotate(radians)
}
function getOrientation (uri) {
var exif = new ExifReader
// Split off the base64 data
var base64String = uri.split(',')[1]
// Read off first 128KB, which is all we need to
// get the EXIF data
var arr = base64ToUint8Array(base64String, 0, Math.pow(2, 17))
try {
exif.load(arr.buffer)
return exif.getTagValue('Orientation')
} catch (err) {
return 1
}
}
function base64ToUint8Array(string, start, finish) {
var start = start || 0
var finish = finish || string.length
// atob that shit
var binary = atob(string)
var buffer = new Uint8Array(binary.length)
for (var i = start; i < finish; i++) {
buffer[i] = binary.charCodeAt(i)
}
return buffer
}
/**
* Mapping from EXIF orientation values to data
* regarding the rotation and mirroring necessary to
* render the canvas correctly
* Derived from:
* http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/
*/
var orientationToTransform = {
1: { rotation: 0, mirror: false },
2: { rotation: 0, mirror: true },
3: { rotation: 180, mirror: false },
4: { rotation: 180, mirror: true },
5: { rotation: 90, mirror: true },
6: { rotation: 90, mirror: false },
7: { rotation: 270, mirror: true },
8: { rotation: 270, mirror: false }
}
return upload
})()
|