Skip to content

Instantly share code, notes, and snippets.

Created November 29, 2014 13:25
Show Gist options
  • Save anonymous/d4a4a7998dc7a0f3f140 to your computer and use it in GitHub Desktop.
Save anonymous/d4a4a7998dc7a0f3f140 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<style id="jsbin-css">
canvas {
filter: url(#svgBlur);
filter: blur(1px);
}
</style>
</head>
<body>
<input id="input" type="file" />
<svg height="0" xmlns="http://www.w3.org/2000/svg">
<filter id="svgBlur" x="-5%" y="-5%" width="110%" height="110%">
<feGaussianBlur in="SourceGraphic" stdDeviation="1"/>
</filter>
</svg>
<script id="jsbin-javascript">
var input = document.getElementById("input");
// Wait for input
input.addEventListener("change", function(e) {
var file = e.target.files[0];
// Only process image files.
if (!file.type.match('image.*')) {
return;
}
// Create image from file (blob)
var img = new Image();
img.src = URL.createObjectURL(file);
img.onload = function() {
var maxHeight = 200,
maxWidth = 300,
origRatio = img.height / img.width,
newWidth = maxWidth,
newHeight = maxHeight;
if(origRatio > 1) {
// Height > Width
newWidth = (img.width / img.height) * newHeight;
} else if(origRatio < 1) {
// Width > Height
newHeight = (img.height / img.width) * newWidth;
} else {
// Width = Height
}
var scaleHeight = newHeight / img.height,
scaleWidth = newWidth / img.width;
// Create canvas with img's height and width
var canvas = document.createElement("canvas");
canvas.height = newHeight;
canvas.width = newWidth;
var ctx = canvas.getContext("2d");
//ctx.scale(scaleWidth, scaleHeight);
ctx.transform(scaleWidth, 0, 0, scaleHeight, 0, 0);
ctx.drawImage(img, 0, 0);
document.body.appendChild(canvas);
};
});
</script>
<script id="jsbin-source-css" type="text/css">canvas {
filter: url(#svgBlur);
filter: blur(1px);
}</script>
<script id="jsbin-source-javascript" type="text/javascript">var input = document.getElementById("input");
// Wait for input
input.addEventListener("change", function(e) {
var file = e.target.files[0];
// Only process image files.
if (!file.type.match('image.*')) {
return;
}
// Create image from file (blob)
var img = new Image();
img.src = URL.createObjectURL(file);
img.onload = function() {
var maxHeight = 200,
maxWidth = 300,
origRatio = img.height / img.width,
newWidth = maxWidth,
newHeight = maxHeight;
if(origRatio > 1) {
// Height > Width
newWidth = (img.width / img.height) * newHeight;
} else if(origRatio < 1) {
// Width > Height
newHeight = (img.height / img.width) * newWidth;
} else {
// Width = Height
}
var scaleHeight = newHeight / img.height,
scaleWidth = newWidth / img.width;
// Create canvas with img's height and width
var canvas = document.createElement("canvas");
canvas.height = newHeight;
canvas.width = newWidth;
var ctx = canvas.getContext("2d");
//ctx.scale(scaleWidth, scaleHeight);
ctx.transform(scaleWidth, 0, 0, scaleHeight, 0, 0);
ctx.drawImage(img, 0, 0);
document.body.appendChild(canvas);
};
});</script></body>
</html>
canvas {
filter: url(#svgBlur);
filter: blur(1px);
}
var input = document.getElementById("input");
// Wait for input
input.addEventListener("change", function(e) {
var file = e.target.files[0];
// Only process image files.
if (!file.type.match('image.*')) {
return;
}
// Create image from file (blob)
var img = new Image();
img.src = URL.createObjectURL(file);
img.onload = function() {
var maxHeight = 200,
maxWidth = 300,
origRatio = img.height / img.width,
newWidth = maxWidth,
newHeight = maxHeight;
if(origRatio > 1) {
// Height > Width
newWidth = (img.width / img.height) * newHeight;
} else if(origRatio < 1) {
// Width > Height
newHeight = (img.height / img.width) * newWidth;
} else {
// Width = Height
}
var scaleHeight = newHeight / img.height,
scaleWidth = newWidth / img.width;
// Create canvas with img's height and width
var canvas = document.createElement("canvas");
canvas.height = newHeight;
canvas.width = newWidth;
var ctx = canvas.getContext("2d");
//ctx.scale(scaleWidth, scaleHeight);
ctx.transform(scaleWidth, 0, 0, scaleHeight, 0, 0);
ctx.drawImage(img, 0, 0);
document.body.appendChild(canvas);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment