Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

canvas-compress

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

canvas-compress - npm Package Compare versions

Comparing version 1.1.2 to 1.1.3

2

bower.json
{
"name": "canvas-compress",
"version": "1.1.1",
"version": "1.1.3",
"description": "image compress with canvas",

@@ -5,0 +5,0 @@ "main": "dist/canvas-compress.js",

@@ -1,1 +0,1 @@

!function(e,t){"function"==typeof define&&define.amd?define(["exif-js"],t):"object"==typeof exports?module.exports=t(require("exif-js")):e.CanvasCompress=t(e.EXIF)}(this,function(e){"use strict";function t(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){var r=e.width,i=e.height;switch(t){case 1:return{width:r,height:i,matrix:[1,0,0,1,0,0]};case 2:return{width:r,height:i,matrix:[-1,0,0,1,r,0]};case 3:return{width:r,height:i,matrix:[-1,0,0,-1,r,i]};case 4:return{width:r,height:i,matrix:[1,0,0,-1,0,i]};case 5:return{width:i,height:r,matrix:[0,1,1,0,0,0]};case 6:return{width:i,height:r,matrix:[0,1,-1,0,i,0]};case 7:return{width:i,height:r,matrix:[0,-1,-1,0,i,r]};case 8:return{width:i,height:r,matrix:[0,-1,1,0,0,r]}}}var n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var i in r)Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i])}return e},o=function(){function e(e,t){for(var r=0;r<t.length;r++){var i=t[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,r,i){return r&&e(t.prototype,r),i&&e(t,i),t}}(),a=window.URL||window.webkitURL,h=Math.log(2),u=4,s=.9,c="image/jpeg",f={width:1e3,height:618},g={_Promise:window.Promise,get Promise(){if("function"!=typeof this._Promise)throw new Error("canvas-compress requires Promise");return this._Promise},set Promise(e){if("function"!=typeof e)throw new TypeError("Promise should be a function");this._Promise=e}},d=function m(){var e=this;r(this,m),this.promise=new g.Promise(function(t,r){e.resolve=t,e.reject=r})},l=function(){function l(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.type,i=void 0===t?c:t,n=e.width,o=void 0===n?f.width:n,a=e.height,h=void 0===a?f.height:a,u=e.quality,g=void 0===u?s:u;r(this,l),g=parseFloat(g);var d={width:parseFloat(o),height:parseFloat(h)};Object.defineProperties(this,{outputType:{get:function(){return i}},outputSize:{get:function(){return d}},outputQuality:{get:function(){return g}}})}return o(l,null,[{key:"usePromise",value:function(e){g.Promise=e}}]),o(l,[{key:"_getOriginalImage",value:function(e){var t=a.createObjectURL(e),r=new Image,i=new d;return r.onload=function(){i.resolve(r)},r.onerror=function(){i.reject("image load error")},r.src=t,i.promise}},{key:"_drawOriginalImage",value:function(r){var n=document.createElement("canvas"),o=n.getContext("2d"),h=new d;return e.getData(r,function(){var u=e.getTag(r,"Orientation")||1,s=i(r,u),c=s.width,f=s.height,g=s.matrix;n.width=c,n.height=f,o.save(),o.transform.apply(o,t(g)),o.drawImage(r,0,0),o.restore(),a.revokeObjectURL(r.src),h.resolve(n)}),h.promise}},{key:"_resizeImage",value:function(e){var t=this.outputSize,r=e.width,i=e.height,n=Math.min(1,t.width/r,t.height/i);return g.Promise.resolve({source:e,scale:n})}},{key:"_drawImage",value:function(e){var t=e.source,r=e.scale,i=document.createElement("canvas"),n=i.getContext("2d"),o=t.getContext("2d"),a=Math.min(u,Math.ceil(1/r/h));r=Math.pow(r,1/a);var s=t.width,c=t.height;i.width=s,i.height=c;for(var f=0;f<a;f++){var d=s*r|0,l=c*r|0,m=void 0,w=void 0;f%2===0?(m=t,w=n):(m=i,w=o),w.clearRect(0,0,s,c),w.drawImage(m,0,0,s,c,0,0,d,l),s=d,c=l}var v=document.createElement("canvas"),p=v.getContext("2d");return v.width=s,v.height=c,p.drawImage(t,0,0,s,c,0,0,s,c),g.Promise.resolve(v)}},{key:"_compress",value:function(e){var t=this.outputType,r=this.outputQuality,i=e.width,n=e.height,o=e.toDataURL(t,r),a=atob(o.split(",")[1]).split("").map(function(e){return e.charCodeAt(0)}),h=new Blob([new Uint8Array(a)],{type:t});return g.Promise.resolve({blob:h,width:i,height:n})}},{key:"process",value:function(e){if(!e)return g.Promise.reject(new ReferenceError("file blob is required"));if(!e.type.match(/^image/))return g.Promise.reject(new TypeError("unsupport file type: "+e.type));var t={};return this._getOriginalImage(e).then(function(e){return t.width=e.width,t.height=e.height,e}).then(this._drawOriginalImage.bind(this)).then(this._resizeImage.bind(this)).then(this._drawImage.bind(this)).then(this._compress.bind(this)).then(function(r){return{source:n({blob:e},t),result:n({},r)}})}}]),l}();return l});
!function(e,t){"function"==typeof define&&define.amd?define(["exif-js"],t):"object"==typeof exports?module.exports=t(require("exif-js")):e.CanvasCompress=t(e.EXIF)}(this,function(e){"use strict";function t(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){var r=e.width,i=e.height;switch(t){case 1:return{width:r,height:i,matrix:[1,0,0,1,0,0]};case 2:return{width:r,height:i,matrix:[-1,0,0,1,r,0]};case 3:return{width:r,height:i,matrix:[-1,0,0,-1,r,i]};case 4:return{width:r,height:i,matrix:[1,0,0,-1,0,i]};case 5:return{width:i,height:r,matrix:[0,1,1,0,0,0]};case 6:return{width:i,height:r,matrix:[0,1,-1,0,i,0]};case 7:return{width:i,height:r,matrix:[0,-1,-1,0,i,r]};case 8:return{width:i,height:r,matrix:[0,-1,1,0,0,r]}}}var n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var i in r)Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i])}return e},o=function(){function e(e,t){for(var r=0;r<t.length;r++){var i=t[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,r,i){return r&&e(t.prototype,r),i&&e(t,i),t}}(),a=window.URL||window.webkitURL,h=Math.log(2),u=4,s=.9,c="image/jpeg",f={width:1e3,height:618},g={_Promise:window.Promise,get Promise(){if("function"!=typeof this._Promise)throw new Error("canvas-compress requires Promise");return this._Promise},set Promise(e){if("function"!=typeof e)throw new TypeError("Promise should be a function");this._Promise=e}},l=function d(){var e=this;r(this,d),this.promise=new g.Promise(function(t,r){e.resolve=t,e.reject=r})},m=function(){function m(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.type,i=void 0===t?c:t,n=e.width,o=void 0===n?f.width:n,a=e.height,h=void 0===a?f.height:a,u=e.quality,g=void 0===u?s:u;r(this,m),g=parseFloat(g);var l={width:parseFloat(o),height:parseFloat(h)};Object.defineProperties(this,{outputType:{get:function(){return i}},outputSize:{get:function(){return l}},outputQuality:{get:function(){return g}}})}return o(m,null,[{key:"usePromise",value:function(e){g.Promise=e}}]),o(m,[{key:"_getOriginalImage",value:function(e){var t=a.createObjectURL(e),r=new Image,i=new l;return r.onload=function(){i.resolve(r)},r.onerror=function(){i.reject("image load error")},r.src=t,i.promise}},{key:"_drawOriginalImage",value:function(r){var n=document.createElement("canvas"),o=n.getContext("2d"),h=new l;return e.getData(r,function(){var u=e.getTag(r,"Orientation")||1,s=i(r,u),c=s.width,f=s.height,g=s.matrix;n.width=c,n.height=f,o.save(),o.transform.apply(o,t(g)),o.drawImage(r,0,0),o.restore(),a.revokeObjectURL(r.src),h.resolve(n)}),h.promise}},{key:"_resizeImage",value:function(e){var t=this.outputSize,r=e.width,i=e.height,n=Math.min(1,t.width/r,t.height/i);return g.Promise.resolve({source:e,scale:n})}},{key:"_drawImage",value:function(e){var t=e.source,r=e.scale;if(1===r)return g.Promise.resolve(t);var i=t.getContext("2d"),n=Math.min(u,Math.ceil(1/r/h)),o=Math.pow(r,1/n),a=document.createElement("canvas"),s=a.getContext("2d"),c=t.width,f=t.height;a.width=c,a.height=f;for(var l=0;;l++){var m=c*o|0,d=f*o|0,w=void 0,p=void 0;if(l%2===0?(w=t,p=s):(w=a,p=i),p.clearRect(0,0,c,f),p.drawImage(w,0,0,c,f,0,0,m,d),c=m,f=d,l===n){var v=w===t?a:t,y=p.getImageData(0,0,c,f);return v.width=c,v.height=f,p.putImageData(y,0,0),g.Promise.resolve(v)}}}},{key:"_compress",value:function(e){var t=this.outputType,r=this.outputQuality,i=e.width,n=e.height,o=e.toDataURL(t,r),a=atob(o.split(",")[1]).split("").map(function(e){return e.charCodeAt(0)}),h=new Blob([new Uint8Array(a)],{type:t});return g.Promise.resolve({blob:h,width:i,height:n})}},{key:"process",value:function(e){if(!e)return g.Promise.reject(new ReferenceError("file blob is required"));if(!e.type.match(/^image/))return g.Promise.reject(new TypeError("unsupport file type: "+e.type));var t={};return this._getOriginalImage(e).then(function(e){return t.width=e.width,t.height=e.height,e}).then(this._drawOriginalImage.bind(this)).then(this._resizeImage.bind(this)).then(this._drawImage.bind(this)).then(this._compress.bind(this)).then(function(r){return{source:n({blob:e},t),result:n({},r)}})}}]),m}();return m});
{
"name": "canvas-compress",
"version": "1.1.2",
"version": "1.1.3",
"description": "image compress with canvas",

@@ -5,0 +5,0 @@ "main": "dist/canvas-compress.js",

@@ -218,4 +218,5 @@ const URL = window.URL || window.webkitURL;

_drawImage({ source, scale }) {
const tempCanvas = document.createElement('canvas');
const tctx = tempCanvas.getContext('2d');
if (scale === 1) {
return GLOBAL_ENV.Promise.resolve(source);
}

@@ -225,20 +226,23 @@ const sctx = source.getContext('2d');

scale = Math.pow(scale, 1 / steps);
const factor = Math.pow(scale, 1 / steps);
const mirror = document.createElement('canvas');
const mctx = mirror.getContext('2d');
let { width, height } = source;
tempCanvas.width = width;
tempCanvas.height = height;
mirror.width = width;
mirror.height = height;
for (let i = 0; i < steps; i++) {
const dw = width * scale | 0;
const dh = height * scale | 0;
for (let i = 0; ; i++) {
const dw = width * factor | 0;
const dh = height * factor | 0;
let canvas, context;
let src, context;
if (i % 2 === 0) {
canvas = source;
context = tctx;
src = source;
context = mctx;
} else {
canvas = tempCanvas;
src = mirror;
context = sctx;

@@ -248,15 +252,24 @@ }

context.clearRect(0, 0, width, height);
context.drawImage(canvas, 0, 0, width, height, 0, 0, dw, dh);
context.drawImage(src, 0, 0, width, height, 0, 0, dw, dh);
width = dw;
height = dh;
}
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = width;
canvas.height = height;
context.drawImage(source, 0, 0, width, height, 0, 0, width, height);
if (i === steps) {
// get current working canvas
const canvas = src === source ? mirror : source;
return GLOBAL_ENV.Promise.resolve(canvas);
// save data
const data = context.getImageData(0, 0, width, height);
// resize
canvas.width = width;
canvas.height = height;
// restore image data
context.putImageData(data, 0, 0);
return GLOBAL_ENV.Promise.resolve(canvas);
}
}
}

@@ -263,0 +276,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc