DDR爱好者之家 Design By 杰米
简单到延伸
最新需要js 文件压缩图片上传 以前没搞过,新手把学习过程分享
一.选择图片并显示
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <title>Title</title> </head> <body> <input id="imginput" type="file" accept="image/*" onchange="showImg(this)"> <div id="img1"> <canvas class="convas01"></canvas> </div> </body> <script> window.URL=window.URL||window.webkitURL; var divimg1 = document.getElementById("img1"); function showImg(obj){ var files = obj.files; var img = new Image(); img.width = 100; if(window.URL){ img.src = window.URL.createObjectURL(files[0]); }else{ //opera不支持createObjectURL/revokeObjectURL方法。需要用FileReader对象来处理 var reader = new FileReader(); reader.readAsDataURL(files[0]); reader.onload = function(e){ img.src = this.result; img.width = 200; img.id="img01"; divimg1.appendChild(img); } } img.id="img01"; divimg1.appendChild(img); } </script> </html>
二.使用canvas代码
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <title>Title</title> </head> <body> <input id="imginput" type="file" accept="image/*" onchange="showImg(this)"> <div id="img1"> <canvas id="convas01"></canvas> </div> </body> <script> window.URL=window.URL||window.webkitURL; var divimg1 = document.getElementById("img1"); var convas = document.getElementById("convas01"); var ht = convas01.getContext("2d"); convas.width = 200; convas.height = 200; function showImg(obj){ var files = obj.files; var img = new Image(); img.width = 200; if(window.URL){ img.src = window.URL.createObjectURL(files[0]); img.onload = function(e){ window.URL.revokeObjectURL(this.src);//方便引用无效回收 ht.clearRect(0, 0, convas.width, convas.width);//clearRect() 方法清空给定矩形内的指定像素。 ht.drawImage(img,0,0,convas.width,convas.height); convas.toDataURL("image/png"); } }else{ //opera不支持createObjectURL/revokeObjectURL方法。需要用FileReader对象来处理 var reader = new FileReader(); reader.readAsDataURL(files[0]); reader.onload = function(){ img.src = this.result; img.width = 200; img.id="img01"; divimg1.appendChild(img); } } } </script> </html>
三.压缩处理
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <title>Title</title> </head> <body> <input id="imginput" type="file" accept="image/*" onchange="showImg(this)"> <div id="img1"> canvas图片 <canvas id="convas01"></canvas> </div> <div id="img2"> 压缩图片 </div> </body> <script> window.URL=window.URL||window.webkitURL; var divimg1 = document.getElementById("img1"); var divimg2 = document.getElementById("img2"); var convas = document.getElementById("convas01"); var ht = convas01.getContext("2d"); convas.width = 200; convas.height = 200; function showImg(obj){ var files = obj.files; var img = new Image(); img.width = 200; if(window.URL){ img.src = window.URL.createObjectURL(files[0]); img.onload = function(e){ window.URL.revokeObjectURL(this.src);//方便引用无效回收 ht.drawImage(img,0,0,convas01.width,convas01.height); var imgdata = convas.toDataURL("image/png"); //将canvas转化成Blob对象 var imgobj = new Image(); var blob =dataURLtoBlob(imgdata); var url = window.URL.createObjectURL(blob); imgobj.onload = function(e){ window.URL.revokeObjectURL(this.src); } imgobj.src= url; divimg2.appendChild(imgobj); } }else{ //opera不支持createObjectURL/revokeObjectURL方法。需要用FileReader对象来处理 var reader = new FileReader(); reader.readAsDataURL(files[0]); reader.onload = function(){ img.src = this.result; img.width = 200; img.id="img01"; divimg1.appendChild(img); } } } function dataURLtoBlob(dataurl) { var arr = dataurl.split(','), mime = arr[0].match(/:(.*"color: #ff0000">四.分辨率调节尝试<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> *{ margin:0;padding:0; } body{ width:100%;height:100%;background:black; } canvas{ width:800px;height:480px;outline:1px solid pink;position:absolute; left:0;top:0;right:0;bottom:0;margin:auto; } </style> </head> <body> <button>240x120</button> <button>320x240</button> <button>480x320</button> <button>640x420</button> <button>800x480</button> <canvas id="canvasId" width="480" height="320"> Your browser does not support the canvas element. </canvas> <script> var canvas = document.getElementById('canvasId'); var context = canvas.getContext('2d'); var btns = document.getElementsByTagName('button'); var resolution = [ [240, 120], [320, 240], [480, 320], [640, 420], [800, 480] ]; //canvas分辨率 var resolutionW = canvas.getAttribute('width'); var resolutionH = canvas.getAttribute('height'); //canvas大小 var sizeW = canvas.offsetWidth; var sizeH = canvas.offsetHeight; console.log(resolutionW); console.log(resolutionH); console.log(sizeW); console.log(sizeH); var img = new Image(); img.src = "http://img5.niutuku.com/phone/1301/0920/0920-niutuku.com-483082.jpg"; //这张图片像素为800x480 img.onload = function() { draw(3); for (var i = 0; i < btns.length; i++) { (function(i) { btns[i].onclick = function() { draw(i); } })(i); } } function draw(i) { canvas.setAttribute('width', resolution[i][0]); canvas.setAttribute('height', resolution[i][1]); context.clearRect(0, 0, sizeW, sizeH); context.drawImage(img, 0, 0, sizeW, sizeH, 0, 0, resolution[i][0], resolution[i][1]); } </script> </body> </html>总结
以上所述是小编给大家介绍的JS HTML图片显示Canvas 压缩功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月27日
2024年11月27日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]