js实现图片缩放
来源:互联网 发布:各种算法的比较 编辑:程序博客网 时间:2024/06/07 05:25
https://github.com/josefrichter/resize/blob/master/public/preprocess.js
var fileinput = document.getElementById('fileinput');var max_width = fileinput.getAttribute('data-maxwidth');var max_height = fileinput.getAttribute('data-maxheight');var preview = document.getElementById('preview');var form = document.getElementById('form');function processfile(file) { if( !( /image/i ).test( file.type ) ) { alert( "File "+ file.name +" is not an image." ); return false; } // read the files var reader = new FileReader(); reader.readAsArrayBuffer(file); reader.onload = function (event) { // blob stuff var blob = new Blob([event.target.result]); // create blob... window.URL = window.URL || window.webkitURL; var blobURL = window.URL.createObjectURL(blob); // and get it's URL // helper Image object var image = new Image(); image.src = blobURL; //preview.appendChild(image); // preview commented out, I am using the canvas instead image.onload = function() { // have to wait till it's loaded var resized = resizeMe(image); // send it to canvas var newinput = document.createElement("input"); newinput.type = 'hidden'; newinput.name = 'images[]'; newinput.value = resized; // put result from canvas into new hidden input form.appendChild(newinput); } };}function readfiles(files) { // remove the existing canvases and hidden inputs if user re-selects new pics var existinginputs = document.getElementsByName('images[]'); var existingcanvases = document.getElementsByTagName('canvas'); while (existinginputs.length > 0) { // it's a live list so removing the first element each time // DOMNode.prototype.remove = function() {this.parentNode.removeChild(this);} form.removeChild(existinginputs[0]); preview.removeChild(existingcanvases[0]); } for (var i = 0; i < files.length; i++) { processfile(files[i]); // process each file at once } fileinput.value = ""; //remove the original files from fileinput // TODO remove the previous hidden inputs if user selects other files}// this is where it starts. event triggered when user selects filesfileinput.onchange = function(){ if ( !( window.File && window.FileReader && window.FileList && window.Blob ) ) { alert('The File APIs are not fully supported in this browser.'); return false; } readfiles(fileinput.files);}// === RESIZE ====function resizeMe(img) { var canvas = document.createElement('canvas'); var width = img.width; var height = img.height; // calculate the width and height, constraining the proportions if (width > height) { if (width > max_width) { //height *= max_width / width; height = Math.round(height *= max_width / width); width = max_width; } } else { if (height > max_height) { //width *= max_height / height; width = Math.round(width *= max_height / height); height = max_height; } } // resize the canvas and draw the image data into it canvas.width = width; canvas.height = height; var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0, width, height); preview.appendChild(canvas); // do the actual resized preview return canvas.toDataURL("image/jpeg",0.7); // get the data from canvas as 70% JPG (can be also PNG, etc.)}
阅读全文
0 0
- js实现图片缩放
- js实现的图片缩放
- JS实现图片缩放功能
- Js实现图片等比缩放
- js实现图片等比例缩放
- CSS+JS实现图片无缩放旋转
- js实现图片的缩放问题
- js等比缩放图片
- JS按比例缩放图片
- js图片等比缩放
- js按比例缩放图片
- js图片等比缩放
- js按比例缩放图片
- js等比缩放图片
- js等比缩放图片
- js图片等比缩放
- js图片缩放
- js-图片缩放
- JavaScript--数组
- JS学习第5天
- Python的算法编程
- location.href和location.search区别
- 云计算中基于机器学习的调度研究综述
- js实现图片缩放
- 2003
- 7.31 I
- Java进阶之路——从初级程序员到架构师,从小工到专家
- 边界值三点分析法
- 同一个应用程序两个实例通过内存映射共享数据
- HDU 1754 I Hate It(线段树区间求最值)
- Android Studio 个性化设置
- PDF编辑之文字编辑以及页眉页脚的添加