Scale and crop avatars to 256 square thumbnails.

Fixes #213 // FREEBIE
pull/749/head
lilia 10 years ago
parent cdb7fcfbad
commit f4a206b266

@ -85,7 +85,7 @@
// loadImage.scale -> components/blueimp-load-image // loadImage.scale -> components/blueimp-load-image
var canvas = loadImage.scale(img, { var canvas = loadImage.scale(img, {
canvas: true, maxWidth: 1920, maxHeight: 1920 canvas: true, maxWidth: maxWidth, maxHeight: maxHeight
}); });
var quality = 0.95; var quality = 0.95;
@ -163,6 +163,41 @@
return this.autoScale(file).then(this.readFile); return this.autoScale(file).then(this.readFile);
}, },
getThumbnail: function() {
// Scale and crop an image to 256px square
var size = 256;
var file = this.file || this.$input.prop('files')[0];
if (file.type.split('/')[0] !== 'image' || file.type === 'image/gif') {
// nothing to do
return Promise.resolve();
}
return new Promise(function(resolve, reject) {
var url = URL.createObjectURL(file);
var img = document.createElement('img');
img.onerror = reject;
img.onload = function () {
URL.revokeObjectURL(url);
// loadImage.scale -> components/blueimp-load-image
// scale, then crop.
var canvas = loadImage.scale(img, {
canvas: true, maxWidth: size, maxHeight: size,
cover: true, minWidth: size, minHeight: size
});
canvas = loadImage.scale(canvas, {
canvas: true, maxWidth: size, maxHeight: size,
crop: true, minWidth: size, minHeight: size
});
// dataURLtoBlob -> components/blueimp-canvas-to-blob
var blob = dataURLtoBlob(canvas.toDataURL('image/png'));
resolve(blob);
};
img.src = url;
}).then(this.readFile);
},
readFile: function(file) { readFile: function(file) {
var contentType = file.type; var contentType = file.type;
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {

@ -114,7 +114,7 @@
return; return;
} }
return this.avatarInput.getFile().then(function(avatarFile) { return this.avatarInput.getThumbnail().then(function(avatarFile) {
var members = this.getRecipients().pluck('id'); var members = this.getRecipients().pluck('id');
textsecure.storage.groups.createNewGroup(members).then(function(group) { textsecure.storage.groups.createNewGroup(members).then(function(group) {
return group.id; return group.id;

@ -46,10 +46,10 @@
}; };
}, },
send: function() { send: function() {
return this.avatarInput.getFiles().then(function(avatarFiles) { return this.avatarInput.getThumbnail().then(function(avatarFile) {
this.model.save({ this.model.save({
name: this.$('.name').val(), name: this.$('.name').val(),
avatar: avatarFiles[0], avatar: avatarFile,
members: _.union(this.model.get('members'), this.recipients_view.recipients.pluck('id')) members: _.union(this.model.get('members'), this.recipients_view.recipients.pluck('id'))
}); });
textsecure.messaging.updateGroup( textsecure.messaging.updateGroup(

Loading…
Cancel
Save