-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
216 lines (161 loc) · 6.29 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script type="text/javascript" src="https://sites.google.com/site/southcoastbushwalkers/web/js/jquery/jquery.min.js"></script>
</head>
<body>
<h1>test image load</h1>
<input type="button" value="debug" onclick="javascript:test();"></input>
<section>
<input type="file" name="imagefileLoaded" id="the-photo-file-field">
<div id="preview" >
<!--image will be inserted here-->
<div id="dumy"></div>
</div>
<form id="myForm" enctype="multipart/form-data">
<h2>Load A Photo</h2>
<input type="text" id="name" name="name" /><br>
<input type="text" id="type" name="type" /><br>
<input type="file" id="fileinput" name="fileinput" /><br>
<input type="button" value="Submit picUploadJs" onclick="picUploadJs(this.parentNode)" />
</form>
</section>
<!--
<script type="text/javascript" src="https://sites.google.com/site/southcoastbushwalkers/web/js/img-szlib.js"></script>
<input id="fileinput" data-maxwidth="320" data-maxheight="240" type="file" name="imagefile[]" multiple/>
<input name="imagefile" type="file" data-maxwidth="320" data-maxheight="240" /><br/>
<input type="submit" value="Upload →" ONCLICK="javascript:cmdupload();">
-->
<div id="status" style="display: none">
<!-- div will be filled with innerHTML after form submission. -->
Uploading. Please wait...
</div>
</body>
<script>
var max_width=320;
var max_height=190;
var preview = document.getElementById('preview');
//check if browser supports file api and filereader features
if (window.File && window.FileReader && window.FileList && window.Blob) {
//this function is called when the input loads an image
function renderImage(file){
var reader = new FileReader();
reader.onload = function(event){
the_url = event.target.result
// blob stuff
var blob = new Blob([the_url]); // create blob...
window.URL = window.URL || window.webkitURL;
var blobURL = window.URL.createObjectURL(blob); // and get it's URL
var image = new Image();
//image.width = 320;
image.src = the_url;
//image.src = blobURL;
image.name="imagefileA";
image.onload = function() {
// have to wait till it's loaded
console.log('image.onload');
var resized = resizeMe(image); // send it to canvas
//upload the resized image file only
var newinput = document.createElement("input");
newinput.type = 'hidden';
newinput.name = 'imagefile';
newinput.value = resized; // put result from canvas into new hidden input
var form = document.getElementById("myForm");
form.appendChild(newinput);
newinput = document.createElement("input");
newinput.type = 'hidden';
newinput.name = 'imagefile_contentType';
var ar = /data:([^;]*)/.exec(resized);
newinput.value = ar[1];
form.appendChild(newinput);
newinput = document.createElement("input");
newinput.type = 'hidden';
newinput.name = 'imagefile_name';
newinput.value = file.name;
form.appendChild(newinput);
}
reader.onloadend = function(event) {
var wtf = event.target.result; // == "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAIAA.......
console.log('onloadend:'+event.target.result);
}
//var preview = document.getElementById('preview');
//preview.replaceChild(image,preview.childNodes[1]);
$('#name').val(file.name);
//$('#size').html(humanFileSize(image.size, "MB"));
$('#type').val(file.type);
}
//when the file is read it triggers the onload event above.
reader.readAsDataURL(file);
}
//watch for change on the
$( "#the-photo-file-field" ).change(function() {
console.log("photo file has been chosen")
//grab the first image in the fileList
//in this example we are only loading one file.
console.log(this.files[0].size)
renderImage(this.files[0])
});
} else {
alert('The File APIs are not fully supported in this browser.');
}
function cmdupload() {
alert("upload");
var canvas = document.getElementById('canvas');
if(canvas != undefined) {
var dataURL = canvas.toDataURL();
}
}
function picUploadJs(frmData) {
document.getElementById('status').style.display = 'inline';
google.script.run
.withSuccessHandler(updateOutput)
.withFailureHandler(updateError)
.processForm(frmData)
};
// Javascript function called by "submit" button handler,
// to show results.
function updateOutput() {
var outputDiv = document.getElementById('status');
outputDiv.innerHTML = "The File was UPLOADED!";
}
function updateError() {
var outputDiv = document.getElementById('status');
outputDiv.innerHTML = "The File error uploading....!";
}
// === RESIZE ====
function resizeMe(img) {
var canvas = document.createElement('canvas');
var width = img.width;
var height = img.height;
// calculate the width and height, constraining the proportions
if (width > height) {
if (width > max_width) {
//height *= max_width / width;
height = Math.round(height *= max_width / width);
width = max_width;
}
} else {
if (height > max_height) {
//width *= max_height / height;
width = Math.round(width *= max_height / height);
height = max_height;
}
}
// resize the canvas and draw the image data into it
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
var c = preview.childNodes;
if(preview.hasChildNodes()){
preview.replaceChild(canvas,c[1]);
}else
preview.appendChild(canvas); // do the actual resized preview
return canvas.toDataURL("image/jpeg",0.7); // get the data from canvas as 70% JPG (can be also PNG, etc.)
}
function test(){
debugger;
}
</script>
</html>