Skip to content

Commit 2b84046

Browse files
committed
update compress
1 parent fe2a31a commit 2b84046

File tree

6 files changed

+72
-59
lines changed

6 files changed

+72
-59
lines changed

site/client/components/doc/cn/CompressImage.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
@imageuploaded="imageUploded"
1212
:max-file-size="5242880"
1313
compress="50"
14-
url="http://101.198.151.190/api/crop.php" >
14+
url="http://101.198.151.190/api/upload.php" >
1515
</vue-core-image-upload>
1616
</div>
17-
18-
1917
</div>
2018
</template>
2119

@@ -35,10 +33,10 @@ export default {
3533
3634
methods: {
3735
imageChanged(files) {
38-
coreImageCompress(files, 'asd', 90);
36+
3937
},
4038
imageUploded(res) {
41-
39+
this.src = res.data.src;
4240
}
4341
}
4442

site/src/crop.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ export default {
251251
};
252252
const move = function (ev) {
253253
const newCropStyle = drag(ev, self.el, coor);
254-
console.log(newCropStyle);
255254
if (newCropStyle) {
256255
self.cropCSS.left = newCropStyle.left;
257256
self.cropCSS.top = newCropStyle.top;

site/src/lib/canvas-helper.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* compress image
3-
* @param src
3+
* reference https://github.com/brunobar79/J-I-C
44
**/
55

66
export default {
@@ -20,13 +20,11 @@ export default {
2020
let image = new Image();
2121
image.src = event.target.result;
2222
image.onload = function() {
23-
const outputType = self._getImageType(src.type);
23+
const mimeType = self._getImageType(src.type);
2424
const cvs = self._getCanvas(image.naturalWidth, image.naturalHeight);
2525
const ctx = cvs.getContext("2d").drawImage(image, 0, 0);
2626
const newImageData = cvs.toDataURL(mimeType, quality/100);
27-
const targetImage = new Image();
28-
targetImage.src = newImageData;
29-
return targetImage;
27+
callback(newImageData);
3028
}
3129
};
3230
reader.readAsDataURL(src);

site/src/lib/xhr.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
/**
22
* simple ajax handler
33
**/
4-
module.exports = function (method, url, headers, data, callback, err) {
4+
5+
//ADD sendAsBinary compatibilty to older browsers
6+
if (XMLHttpRequest.prototype.sendAsBinary === undefined) {
7+
XMLHttpRequest.prototype.sendAsBinary = function(string) {
8+
var bytes = Array.prototype.map.call(string, function(c) {
9+
return c.charCodeAt(0) & 0xff;
10+
});
11+
this.send(new Uint8Array(bytes).buffer);
12+
};
13+
}
14+
15+
module.exports = function (method, url, headers, data, callback, err, isBinary) {
16+
517
const r = new XMLHttpRequest();
618
const error = err || function () {
719
console.error('AJAX ERROR!');
820
};
21+
const boundary = 'vuecodeimageupload';
922
// Binary?
1023
let binary = false;
1124
if (method === 'blob') {
@@ -40,25 +53,9 @@ module.exports = function (method, url, headers, data, callback, err) {
4053
// Should we add the query to the URL?
4154
if (method === 'GET' || method === 'DELETE') {
4255
data = null;
43-
}
44-
else if (data && typeof (data) !== 'string' && !(data instanceof FormData) && !(data instanceof File) && !(data instanceof Blob)) {
45-
// Loop through and add formData
46-
var f = new FormData();
47-
for (x in data)
48-
if (data.hasOwnProperty(x)) {
49-
if (data[x] instanceof HTMLInputElement) {
50-
if ('files' in data[x] && data[x].files.length > 0) {
51-
f.append(x, data[x].files[0]);
52-
}
53-
}
54-
else if (data[x] instanceof Blob) {
55-
f.append(x, data[x], data.name);
56-
}
57-
else {
58-
f.append(x, data[x]);
59-
}
60-
}
61-
data = f;
56+
} else if (isBinary) {
57+
const code = data.base64Code.replace('data:' + data.type + ';base64,', '');
58+
data = ['--' + boundary, 'Content-Disposition: form-data; name="' + data.filed + '"; filename="' + data.filename + '"', 'Content-Type: ' + data.type, '', window.atob(code), '--' + boundary + '--'].join('\r\n');
6259
}
6360
// Open the path, async
6461
r.open(method, url, true);
@@ -75,6 +72,12 @@ module.exports = function (method, url, headers, data, callback, err) {
7572
for (x in headers) {
7673
r.setRequestHeader(x, headers[x]);
7774
}
75+
if (isBinary) {
76+
r.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
77+
}
78+
}
79+
if (isBinary) {
80+
return r.sendAsBinary(data);
7881
}
7982
r.send(data);
8083
return r;

site/src/props.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default {
2121
default: 'files'
2222
},
2323
crop: {
24-
type: [String],
24+
type: [String, Boolean],
2525
default: ''
2626
},
2727
cropBtn: {

site/src/vue-core-image-upload.vue

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,15 @@
9898
this.__showImage();
9999
return;
100100
}
101-
this. __dispatch('imagechanged',this.files[0]);
102-
this.tryAjaxUpload();
103-
101+
this. __dispatch('imagechanged', this.files[0]);
102+
if (this.compress) {
103+
canvasHelper.compress(this.files[0], 100 - this.compress, (code) => {
104+
this.tryAjaxUpload('', true, code);
105+
});
106+
} else {
107+
this.tryAjaxUpload();
108+
}
104109
},
105-
106-
107110
__showImage() {
108111
this.hasImage = true;
109112
this.__readFiles();
@@ -174,37 +177,49 @@
174177
},
175178
176179
// use ajax upload IE9+
177-
tryAjaxUpload(callback) {
180+
tryAjaxUpload(callback, isBinary, base64Code) {
181+
const self = this;
178182
this. __dispatch('imageuploading',this.files[0]);
183+
const done = function(res) {
184+
if(typeof callback === 'function') {
185+
callback();
186+
}
187+
self.uploading = false;
188+
self.cancel();
189+
self.__dispatch('imageuploaded',res);
190+
};
191+
const errorUpload = function(err) {
192+
self.__dispatch('errorhandle', err);
193+
};
179194
if (!this.isXhr) {
180195
if(this.crop) {
181196
this.hasImage = false;
182197
}
183198
return typeof callback === 'function' && callback();
184199
}
185-
const self = this;
186-
let data = new FormData();
187-
for(let i=0;i<this.files.length;i++) {
188-
data.append(self.inputOfFile, this.files[i]);
189-
}
190-
if (typeof this.data === 'object') {
191-
for(let k in this.data) {
192-
if(this.data[k] !== undefined) {
193-
data.append(k,this.data[k]);
200+
let data;
201+
if (isBinary) {
202+
data = {
203+
type: this.files[0]['type'],
204+
filename: this.files[0]['name'],
205+
filed: this.inputOfFile,
206+
base64Code: base64Code
207+
};
208+
} else {
209+
data = new FormData();
210+
for (let i=0;i<this.files.length;i++) {
211+
data.append(this.inputOfFile, this.files[i]);
212+
}
213+
console.log(data);
214+
if (typeof this.data === 'object') {
215+
for(let k in this.data) {
216+
if(this.data[k] !== undefined) {
217+
data.append(k,this.data[k]);
218+
}
194219
}
195-
}
196-
}
197-
xhr('POST',this.url, this.headers, data,function(res) {
198-
if(typeof callback === 'function') {
199-
callback();
200220
}
201-
self.uploading = false;
202-
if(self.crop) {
203-
self.hasImage = false;
204-
}
205-
document.querySelector("#g-core-upload-input-" + self.formID).value = '';
206-
self.__dispatch('imageuploaded',res);
207-
});
221+
}
222+
xhr('POST',this.url, this.headers, data, done, errorUpload, isBinary);
208223
},
209224
},
210225

0 commit comments

Comments
 (0)