-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.php
164 lines (137 loc) · 4.68 KB
/
index.php
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<!--
copyright michael kimsal 2010
http://michaelkimsal.com/
released into public domain
-->
<script type="text/javascript">var _sf_startpt=(new Date()).getTime()</script>
<title>Client side JavaScript file resize and upload</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style>
body {
font-size: 16px;
font-family: Arial;
}
#loader {
display:none;
}
#preview {
display:none;
}
#base64 {
display:none;
}
</style>
<script type="text/javascript">
function upload() {
var photo = document.getElementById("photo");
var file = photo.files[0];
var preview = document.getElementById("preview");
preview.src = file.getAsDataURL();
return _resize(preview);
}
function _resize(img, maxWidth, maxHeight)
{
var ratio = 1;
var canvas = document.createElement("canvas");
canvas.style.display="none";
document.body.appendChild(canvas);
var canvasCopy = document.createElement("canvas");
canvasCopy.style.display="none";
document.body.appendChild(canvasCopy);
var ctx = canvas.getContext("2d");
var copyContext = canvasCopy.getContext("2d");
if(img.width > maxWidth)
ratio = maxWidth / img.width;
else if(img.height > maxHeight)
ratio = maxHeight / img.height;
canvasCopy.width = img.width;
canvasCopy.height = img.height;
try {
copyContext.drawImage(img, 0, 0);
} catch (e) {
document.getElementById('loader').style.display="none";
// alert("There was a problem - please reupload your image");
return false;
}
canvas.width = img.width * ratio;
canvas.height = img.height * ratio;
// the line to change
//ctx.drawImage(canvasCopy, 0, 0, canvasCopy.width, canvasCopy.height, 0, 0, canvas.width, canvas.height);
// the method signature you are using is for slicing
ctx.drawImage(canvasCopy, 0, 0, canvas.width, canvas.height);
var dataURL = canvas.toDataURL("image/png");
document.body.removeChild(canvas);
document.body.removeChild(canvasCopy);
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
};
function resize() {
var photo = document.getElementById("photo");
if(photo.files!=undefined){
var loader = document.getElementById("loader");
loader.style.display = "inline";
var file = photo.files[0];
document.getElementById("orig").value = file.fileSize;
var preview = document.getElementById("preview");
var r = new FileReader();
r.onload = (function(previewImage) {
return function(e) {
var maxx = document.getElementById('maxx').value;
var maxy = document.getElementById('maxy').value;
previewImage.src = e.target.result;
previewImage.onload = function() {
var k = _resize(previewImage, maxx, maxy);
if (k != false) {
document.getElementById('base64').value= k;
document.getElementById('upload').submit();
} else {
alert('problem - please attempt to upload again');
}
}
};
})(preview);
r.readAsDataURL(file);
} else {
alert("Seems your browser doesn't support resizing");
}
return false;
}
//document.getElementById('photo').addEventListener('change', handleFileSelect, false);
</script>
</head>
<body>
<h2>Shrink selected file client-side before uploading</h2>
<input type="file" name="photo" id="photo">
<p>Max width in pixels: <input type='text' name='maxx' id='maxx' value='200'/></p>
<p>Max height in pixels: <input type='text' name='maxy' id='maxy' value='200'/></p>
<input type="button" onClick="resize();" value="Shrink and upload">
<img src="loader.gif" id="loader" />
<img src="" alt="Image preview" id="preview">
<form name="upload" id="upload" method='post' action='show.php'>
<textarea name="base64" id="base64" rows='10' cols='90'></textarea>
<input type="hidden" id="orig" name="orig" value=""/>
</form>
<a href="shrinker.tgz">grab this code</a> or pull from <a href="https://github.com/mgkimsal/jsclientshrinker">github</a>
</body>
<script type="text/javascript">
var _sf_async_config={uid:9726,domain:"michaelkimsal.com"};
(function(){
function loadChartbeat() {
window._sf_endpt=(new Date()).getTime();
var e = document.createElement('script');
e.setAttribute('language', 'javascript');
e.setAttribute('type', 'text/javascript');
e.setAttribute('src',
(("https:" == document.location.protocol) ? "https://s3.amazonaws.com/" : "http://") +
"static.chartbeat.com/js/chartbeat.js");
document.body.appendChild(e);
}
var oldonload = window.onload;
window.onload = (typeof window.onload != 'function') ?
loadChartbeat : function() { oldonload(); loadChartbeat(); };
})();
</script>
</html>