-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
131 lines (91 loc) · 3.26 KB
/
script.js
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
/**
* Created by ANICET ERIC KOUAME on 05/03/2017.
*/
//function to format bites bit.ly/19yoIPO
$(document).ready(function () {
$("#submit-btn").click(function () {
beforeSubmit();
});
});
//function to check file size before uploading.
function beforeSubmit() {
$('#output').html("<b class='text-center'><img src='images/ajax-loader.gif' alt='' /> In progress...</b>");
//check whether browser fully supports all File API
if (window.File && window.FileReader && window.FileList && window.Blob) {
if (!$('#imageInput').val()) //check empty input filed
{
$("#output").html("Select image !!!!!!");
return false
}
var fsize = $('#imageInput')[0].files[0].size; //get file size
var ftype = $('#imageInput')[0].files[0].type; // get file type
//allow only valid image file types
switch (ftype) {
case 'image/png': case 'image/gif': case 'image/jpeg': case 'image/pjpeg':
break;
default:
$("#output").html("<b>" + ftype + "</b> Unsupported file type!!");
return false
}
//Allowed file size is less than 1 MB (1048576)
if (fsize > 1048576) {
$("#output").html("<b>" + bytesToSize(fsize) + "</b> Too big Image file! <br />Please reduce the size of your photo using an image editor.");
return false
}
encodeImageFileAsURL(ftype);
}
else {
//Output error to older unsupported browsers that doesn't support HTML5 File API
$("#output").html("Please upgrade your browser, because your current browser lacks some new features we need!!");
return false;
}
}
function encodeImageFileAsURL(ftype){
var fileUpload = $('#imageInput').get(0);
var file = fileUpload.files;
// alert(file);
if (file.length > 0)
{
var fileToLoad = file[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent) {
var srcData = fileLoadedEvent.target.result; // <--- data: base64
// alert(srcData);
upload(srcData,ftype);
};
fileReader.readAsDataURL(fileToLoad);
}
}
function upload(base64Image,ftype){
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "Process.php",
data: {"img": base64Image, "ex": ftype},
cache: false,
success: function(result){
if(result){
var image = $("<img>", {
"src": result,
"width": "250px",
"height": "250px"
});
$("#output").empty();
$("#output").append(image);
}else{
$("#output").empty();
$("#output").html("Error to insert database!!");
}
},
error: function (r) {
$("#output").empty();
$("#output").html("Error to upload image!!");
}
});
}
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Bytes';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}