照片是自己用单反拍的,5M多,压缩下面3张分别是600多kb,400多kb,300kb的最后那张失真度很大了,压缩效率蛮高的。
<!DOCTYPE html>
<html><head> <meta charset="utf-8"/> <title>File API Test</title> <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="js/JIC.js"></script> <style> #test{ display: none } </style></head><body><input type="file" id="fileImg" ><form> <img src="" id="test" alt=""></form><script> function handleFileSelect (evt) { // var filebtn = document.getElementById(id) // console.log(filebtn) // var files = filebtn.target.files // console.log(filebtn.target) // console.log(files) var files = evt.target.files for (var i = 0, ff = files[i]i++) { // Only process image files. if (!f.type.match('image.*')) { continue } var reader = new FileReader() // Closure to capture the file information. reader.onload = (function(theFile) { return function(e) { // Render thumbnail. // console.log(evt.target.files[0]) // console.log(e.target) console.log(e.target.result) var i = document.getElementById("test") i.src = event.target.result console.log($(i).width()) console.log($(i).height()) $(i).css('width',$(i).width()/10+'px') //$(i).css('height',$(i).height()/10+'px') console.log($(i).width()) console.log($(i).height()) var quality = 50 i.src = jic.compress(i,quality).src console.log(i.src) i.style.display = "block" } })(f) // Read in the image file as a data URL. reader.readAsDataURL(f) } } document.getElementById('fileImg').addEventListener('change', handleFileSelect, false)</script></body></html>
var jic = {/** * Receives an Image Object (can be JPG OR PNG) and returns a new Image Object compressed * @param {Image} source_img_obj The source Image Object * @param {Integer} quality The output quality of Image Object * @return {Image} result_image_obj The compressed Image Object */ compress: function(source_img_obj, quality, output_format){ var mime_type = "image/jpeg"if(output_format!=undefined &&output_format=="png"){mime_type = "image/png"} var cvs = document.createElement('canvas')//naturalWidth真实图片的宽度 cvs.width = source_img_obj.naturalWidthcvs.height = source_img_obj.naturalHeightvar ctx = cvs.getContext("2d").drawImage(source_img_obj, 0, 0)var newImageData = cvs.toDataURL(mime_type, quality/100)var result_image_obj = new Image()result_image_obj.src = newImageDatareturn result_image_obj }, function ****(***)
一般压缩都经过混淆,如果你看到变量名都是A,B,C,D之类的无规则的命名,那就是被混淆过的,一般来说也很难阅读,就算你 还原了格式。如果是没有混淆的,你可以试试用js的格式化工具来重新格式化一下的,比如:
/* 美化:格式化代码,使之容易阅读 */
/* 净化:去掉代码中多余的注释、换行、空格等 */
/* 压缩:将代码压缩为更小体积,便于传输 */
/* 解压:将压缩后的代码转换为人可以阅读的格式 */
/* 混淆:将代码的中变量名简短化以减小体积,但可读性差,经混淆后的代码无法还原 */
/* 如果有用,请别忘了推荐给你的朋友: */
/* javascript在线美化、净化、压缩、解压:http://tool.lu/js */
/* 以下是演示代码 */
var Inote = {}
Inote.JSTool = function(options) {
this.options = options || {}
}
Inote.JSTool.prototype = {
_name: 'Javascript工具',
_history: {
'v1.0': ['2011-01-18', 'javascript工具上线'],
'v1.1': ['2012-03-23', '增加混淆功能'],
'v1.2': ['2012-07-21', '升级美化功能引擎'],
'v1.3': ['2014-03-01', '升级解密功能,支持eval,window.eval,window["eval"]等的解密'],
'v1.4': ['2014-08-05', '升级混淆功能引擎'],
'v1.5': ['2014-08-09', '升级js压缩引擎'],
'v1.6': ['2015-04-11', '升级js混淆引擎']
},
options: {},
getName: function() {return this._name},
getHistory: function() {
return this._history}
}
var jstool = new Inote.JSTool()