-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
209 lines (158 loc) · 5.05 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
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>Profile Picture Overlay</title>
<style>
#overlay-img, .overlay-img {
width:500px;
height:auto;
display:block;
margin: 30px auto;
}
input {
display: block;
margin: 10px auto;
}
.inputfile {
width: 0.1px;
height: 0.1px;
opacity: 0;
overflow: hidden;
position: absolute;
z-index: -1;
}
.inputfile + label, #download {
border-radius: 2px;
margin-top:40px;
display: inline-block;
padding: 10px 30px;
font-weight: 700;
text-transform: uppercase;
vertical-align: middle;
color: white;
background-color: #b88f59;
-webkit-tap-highlight-color: transparent;
}
.inputfile:focus + label,
.inputfile + label:hover,
#download:hover
{
box-shadow: 0 5px 11px 0 rgba(0, 0, 0, 0.18), 0 4px 15px 0 rgba(0, 0, 0, 0.15);
}
.inputfile + label,
#download
{
cursor: pointer; /* "hand" cursor */
}
.inputfile:focus + label {
outline: 1px dotted #000;
outline: -webkit-focus-ring-color auto 5px;
}
.overlayDiv {
text-align:center;
}
.madeBy {
text-align:center;
font-size: .7em;
margin: 50px auto 10px auto;
}
</style>
</head>
<body>
<script type="text/javascript" src="caman.full.js"></script>
<div class="container">
<div>
<h2></h2>
<p></p>
</div>
<div class="overlayDiv">
<input type="file" accept="image/*" onchange="loadFile(event)" id="file" class="inputfile" />
<label for="file">Загрузить фото</label>
<img id="overlay-img" src="plain.png" onclick="doSaveAs()" />
<label id="download" onclick="doSaveAs()">Скачать</label>
</div>
<script>
function crop(url, aspectRatio) {
// we return a Promise that gets resolved with our canvas element
return new Promise((resolve) => {
// this image will hold our source image data
const inputImage = new Image();
// we want to wait for our image to load
inputImage.onload = () => {
// let's store the width and height of our image
const inputWidth = inputImage.naturalWidth;
const inputHeight = inputImage.naturalHeight;
// get the aspect ratio of the input image
const inputImageAspectRatio = inputWidth / inputHeight;
// if it's bigger than our target aspect ratio
let outputWidth = inputWidth;
let outputHeight = inputHeight;
if (inputImageAspectRatio > aspectRatio) {
outputWidth = inputHeight * aspectRatio;
} else if (inputImageAspectRatio < aspectRatio) {
outputHeight = inputWidth / aspectRatio;
}
// calculate the position to draw the image at
const outputX = (outputWidth - inputWidth) * 0.5;
const outputY = (outputHeight - inputHeight) * 0.5;
// create a canvas that will present the output image
const outputImage = document.createElement('canvas');
// set it to the same size as the image
outputImage.width = outputWidth;
outputImage.height = outputHeight;
// draw our image at position 0, 0 on the canvas
const ctx = outputImage.getContext('2d');
ctx.drawImage(inputImage, outputX, outputY);
resolve(outputImage);
};
// start loading our image
inputImage.src = url;
});
}
var loadFile = function(event) {
var reader = new FileReader();
reader.onload = function() {
createOverlay(reader.result);
};
reader.readAsDataURL(event.target.files[0]);
};
var createOverlay = function(uploadImage) {
crop(uploadImage, 1).then((cropped) => {
const croppedImg = new Image();
croppedImg.src = cropped.toDataURL();
Caman("#overlay-img", function () {
this.brightness(5).render();
this.newLayer(function() {
this.setBlendingMode("normal");
this.opacity(100);
this.overlayImage(croppedImg);
});
this.newLayer(function() {
this.setBlendingMode("normal");
this.opacity(100);
this.overlayImage('overlay.png');
});
$('#overlay-img').removeAttr('style');
});
$('#overlay-img').removeAttr('style');
});
};
var doSaveAs = function(){
var e;
var lnk = document.createElement('a');
lnk.download = 'photo.png';
lnk.href = ($('#overlay-img')[0]).toDataURL();
if (document.createEvent) {
e = document.createEvent("MouseEvents");
e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
lnk.dispatchEvent(e);
}
else if (lnk.fireEvent) {
lnk.fireEvent("onclick");
}
};
</script>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
</body>
</html>