Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple image compress #145

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 46 additions & 49 deletions src/lib/canvas-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,40 @@
**/

export default {
_getImageType(str) {
_getImageType (str) {
let mimeType = 'image/jpeg';
const outputType = str.match(/(image\/[\w]+)\.*/)[0];
if (typeof outputType !== 'undefined'){
if (typeof outputType !== 'undefined') {
mimeType = outputType;
}
return mimeType;
},

compress (src, quality, callback) {
const reader = new FileReader();
const self = this;
reader.onload = function(event) {
const image = new Image();
image.src = event.target.result;
image.onload = function() {
const mimeType = self._getImageType(src.type);
const cvs = self._getCanvas(image.naturalWidth, image.naturalHeight);
const ctx = cvs.getContext("2d").drawImage(image, 0, 0);
const newImageData = cvs.toDataURL(mimeType, quality/100);
callback(newImageData);
}
};
reader.readAsDataURL(src);
for (var i = 0; i < src.length; i++) {
const reader = new FileReader();
const self = this;
(function (file) {
reader.onload = (function (event) {
const image = new Image();
image.src = event.target.result;
image.onload = function () {
const mimeType = self._getImageType(file.type);
const cvs = self._getCanvas(image.naturalWidth, image.naturalHeight);
const ctx = cvs.getContext('2d').drawImage(image, 0, 0);
const newImageData = cvs.toDataURL(mimeType, quality / 100);
file['base64Code'] = newImageData;
callback(file);
}
})
})(src[i])
reader.readAsDataURL(src[i]);
}
},
/**
* crop image via canvas and generate data
**/
crop(image, options, callback) {
const checkNumber = function(num) {
crop (image, options, callback) {
const checkNumber = function (num) {
return (typeof num === 'number');
};
// check crop options
Expand All @@ -43,79 +47,75 @@ export default {
options.toCropImgH > 0) {
let w = options.toCropImgW;
let h = options.toCropImgH;
if(options.maxWidth && options.maxWidth < w) {
if (options.maxWidth && options.maxWidth < w) {
w = options.maxWidth;
h = options.toCropImgH * w / options.toCropImgW;
}
if (options.maxHeight && options.maxHeight < h) {
h = options.maxHeight
}
const cvs = this._getCanvas(w, h);
const ctx = cvs.getContext('2d').drawImage(image, options.toCropImgX, options.toCropImgY, options.toCropImgW, options.toCropImgH, 0 , 0, w, h);
const ctx = cvs.getContext('2d').drawImage(image, options.toCropImgX, options.toCropImgY, options.toCropImgW, options.toCropImgH, 0, 0, w, h);
const mimeType = this._getImageType(image.src);
const data = cvs.toDataURL(mimeType, options.compress/100);
const data = cvs.toDataURL(mimeType, options.compress / 100);
callback(data);
}
},

/**
* init image for reset size and rotation
**/
init(src, callback) {
init (src, callback) {
let image = new Image();
image.src = src;
var self = this;
image.onload = function() {
image.onload = function () {
var mimeType = self._getImageType(image.src);
var cvs = self._getCanvas(image.naturalWidth, image.naturalHeight);
var ctx = cvs.getContext("2d");
var ctx = cvs.getContext('2d');
ctx.drawImage(image, 0, 0);
var newImageData = cvs.toDataURL(mimeType, 100);
callback(newImageData);
}
},

/**
* rotate image via canvas
**/
rotate(imageData, direction, callback) {
rotate (imageData, direction, callback) {
let image = new Image();
image.src = imageData.src;
var self = this;
image.onload = function() {
image.onload = function () {
var mimeType = self._getImageType(image.src);
var cvs = self._getCanvas(image.naturalHeight, image.naturalWidth);
var ctx = cvs.getContext("2d");
if (direction == 1) {
ctx.rotate(90 * Math.PI / 180);
ctx.translate(0, -cvs.width);
var ctx = cvs.getContext('2d');
if (direction === 1) {
ctx.rotate(90 * Math.PI / 180);
ctx.translate(0, -cvs.width);
} else {
ctx.rotate(-90 * Math.PI / 180);
ctx.translate(-cvs.height, 0);
ctx.rotate(-90 * Math.PI / 180);
ctx.translate(-cvs.height, 0);
}
ctx.drawImage(image, 0, 0);
var newImageData = cvs.toDataURL(mimeType, 100);
callback(newImageData);
}
},

resize(image, options, callback) {
const checkNumber = function(num) {
resize (image, options, callback) {
const checkNumber = function (num) {
return (typeof num === 'number');
};
if (checkNumber(options.toCropImgX) && checkNumber(options.toCropImgY) && options.toCropImgW > 0 && options.toCropImgH > 0) {
const w = options.toCropImgW * options.imgChangeRatio;
const w = options.toCropImgW * options.imgChangeRatio;
const h = options.toCropImgH * options.imgChangeRatio;
const cvs = this._getCanvas(w, h);
const ctx = cvs.getContext('2d').drawImage(image, 0, 0, options.toCropImgW, options.toCropImgH, 0, 0, w , h);
const ctx = cvs.getContext('2d').drawImage(image, 0, 0, options.toCropImgW, options.toCropImgH, 0, 0, w, h);
const mimeType = this._getImageType(image.src);
const data = cvs.toDataURL(mimeType, options.compress / 100);
callback(data);
}
},

rotate2(src, degrees, callback) {
this._loadImage(src, (image) => {
rotate2 (src, degrees, callback) {
this._loadImage(src, image => {
let w = image.naturalWidth;
let h = image.naturalHeight;
const canvasWidth = Math.max(w, h);
Expand Down Expand Up @@ -152,8 +152,7 @@ export default {
callback(data, w, h);
});
},

_loadImage(data, callback) {
_loadImage (data, callback) {
const image = new Image();
image.src = data;
image.onload = function () {
Expand All @@ -163,12 +162,10 @@ export default {
console.log('Error: image error!');
};
},

_getCanvas(width, height) {
_getCanvas (width, height) {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
return canvas;
},

}
};
13 changes: 8 additions & 5 deletions src/vue-core-image-upload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@
return;
}
this. __dispatch('imagechanged', this.files.length > 1 ? this.files : this.files[0]);
if (this.compress && this.files[0]['type'] !== 'image/png' && this.files[0]['type'] !== 'image/gif') {
canvasHelper.compress(this.files[0], 100 - this.compress, (code) => {
if (this.compress) {
canvasHelper.compress(this.files, 100 - this.compress, (code) => {
this.tryAjaxUpload('', true, code);
});
} else {
Expand Down Expand Up @@ -276,11 +276,14 @@
}
let data;
if (isBinary) {
var dataType = base64Code['type'] ? base64Code['type'] : this.files[0]['type'];
var dataName = base64Code['name'] ? encodeURI(base64Code['name']) : this.files[0]['name'];
var code = base64Code['base64Code'] ? base64Code['base64Code'] : base64Code;
data = {
type: this.files[0]['type'],
filename: this.files[0]['name'],
type: dataType,
filename: dataName,
filed: this.inputOfFile,
base64Code: base64Code
base64Code: code
};
if (typeof this.data === 'object') {
data = Object.assign(this.data, data);
Expand Down