DDS.Imagem_Aborta = function () { }; DDS.Imagem_Erro = function () { }; DDS.Imagem_Captura_Cancela = function () { }; DDS.Imagem_Captura_Antes = function () { }; DDS.Imagem_Captura_progress = function (progress) { }; DDS.Imagem_Captura_Depois = function () { }; DDS.Imagem_Vetifica_Tipo = function (Tipo, Extensao) { return true; }; DDS.Imagem_Carregada = false; DDS.Imagem_Atual = ""; DDS.Imagem_Nome = ""; DDS.Imagem_Ext = ""; DDS.Imagem_Tam = 0; DDS.Imagem_width = 0; DDS.Imagem_height = 0; DDS.Imagem_TamMax = 0; function Imagem_Captura(Arquivos) { DDS.Imagem_Carregada = false; DDS.Imagem_Atual = ""; DDS.Imagem_Nome = ""; DDS.Imagem_Ext = ""; DDS.Imagem_Tam = 0; DDS.Imagem_width = 0; DDS.Imagem_height = 0; if (Arquivos.files.length > 0) { var fileToLoad = Arquivos.files[0]; var fileReader = new FileReader(); DDS.Imagem_Nome = fileToLoad.name.trim(); if (DDS.Imagem_Nome.lastIndexOf('.') > 0) DDS.Imagem_Ext = DDS.Imagem_Nome.substring(fileToLoad.name.lastIndexOf('.') + 1).toLowerCase(); DDS.Imagem_Tam = fileToLoad.size; if (fileToLoad.fileSize > fileToLoad.size) DDS.Imagem_Tam = fileToLoad.fileSize; if (DDS.Imagem_Tam > 0) DDS.Imagem_Tam = parseInt(DDS.Imagem_Tam / 1024); fileReader.onabort = function () { DDS.Imagem_Captura_Cancela(); }; fileReader.onerror = function () { DDS.Imagem_Captura_Cancela(); }; fileReader.onloadstart = function (e) { DDS.Imagem_Captura_Antes(); }; fileReader.onprogress = function (data) { if (data.lengthComputable) { var progress = parseInt((data.loaded / data.total) * 100, 10); DDS.Imagem_Captura_progress(progress); } }; fileReader.onloadend = function (fileLoadedEvent) { DDS.Imagem_Atual = fileLoadedEvent.target.result; var _tipo = ""; if (DDS.Imagem_Atual.indexOf(';') >= 0) _tipo = DDS.Imagem_Atual.substring(5, DDS.Imagem_Atual.indexOf(';')); if (DDS.Imagem_Vetifica_Tipo(_tipo, DDS.Imagem_Ext) === false) { DDS.Imagem_Captura_Cancela(); } else { //reduz o tamanho para imagens if (DDS.Imagem_Atual.indexOf('data:image') >= 0 && DDS.Imagem_TamMax > 0 && DDS.Imagem_Tam > 0 && DDS.Imagem_Tam > DDS.Imagem_TamMax) { try { var image = new Image(); image.setAttribute('crossOrigin', 'anonymous'); var _novo_Tamanho = DDS.Imagem_Atual.length * (DDS.Imagem_TamMax / DDS.Imagem_Tam); image.onerror = function (e) { DDS.Imagem_Captura_Depois(); }; image.onload = function (e) { if (DDS.Imagem_Atual.length > _novo_Tamanho) { //faz loop ate chegar ao tamanho ideal. DDS.Imagem_width = image.width * .9; DDS.Imagem_height = image.height * .9; var canvas = document.createElement('canvas'); canvas.width = DDS.Imagem_width; canvas.height = DDS.Imagem_height; var ctx = canvas.getContext("2d"); ctx.drawImage(image, 0, 0, DDS.Imagem_width, DDS.Imagem_height); image.src = canvas.toDataURL('image/jpeg'); DDS.Imagem_Atual = image.src; } else { DDS.Imagem_Captura_Depois(); } }; image.src = fileReader.result; } catch (e) { DDS.Imagem_Captura_Depois(); } } else { DDS.Imagem_Captura_Depois(); } } }; fileReader.readAsDataURL(fileToLoad); } }