Skip to content

Instantly share code, notes, and snippets.

Created May 11, 2016 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b575f30a280ae9a52c090a3144d1f025 to your computer and use it in GitHub Desktop.
Save anonymous/b575f30a280ae9a52c090a3144d1f025 to your computer and use it in GitHub Desktop.
function checkCompatibility() {
var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
// At least Safari 3+: "[object HTMLElementConstructor]"
var isChrome = !!window.chrome && !isOpera; // Chrome 1+
var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6
return (isChrome || isFirefox) && !isIE && !isSafari;
}
var fileIndex=0;
var allFiles = 0;
function doUpload() {
fileIndex=0;
allFiles = document.getElementById('fileToUpload').files.length;
doUploadW(fileIndex);
sessionStorage.setItem("phName",document.getElementById("phName").value);
}
function doUploadW(fileID) {
var file = document.getElementById('fileToUpload').files[fileID];
var dataUrl = "";
var fileName = file.name;
document.getElementById('downloadImage').download = fileName.split(".")[0] + "_watermarked." + fileName.split(".")[1];
// Create an image
var img = document.createElement("img");
// Create a file reader
var reader = new FileReader();
// Set the image once loaded into file reader
reader.onload = function(e)
{
img.src = e.target.result;
img.onload = function() {
var canvas = document.createElement("canvas");
// Set Width and Height
var MAX_WIDTH = 1500;
var MAX_HEIGHT =1125;
var width = img.width;
var height = img.height;
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
ctx.fillStyle = "rgba(255, 255, 255, 1)";
ctx.font = "20pt Helvetica";
var waterMarkText = "Ph. " + document.getElementById("phName").value + " - #IJF16";
ctx.fillText(waterMarkText, width - (waterMarkText.length * 15), height - 20);
dataUrl = canvas.toDataURL("image/jpeg");
document.getElementById('image_preview').src = dataUrl;
document.getElementById('downloadImage').href = dataUrl;
document.getElementById('downloadImage').click();
if (fileIndex < allFiles-1) {
fileIndex ++;
doUploadW(fileIndex);
} else {
location.reload();
}
}
}
// Load files into file reader
reader.readAsDataURL(file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment