|
|
@ -3,7 +3,18 @@ define([
|
|
|
|
], function () {
|
|
|
|
], function () {
|
|
|
|
var Nacl = window.nacl;
|
|
|
|
var Nacl = window.nacl;
|
|
|
|
var Thumb = {
|
|
|
|
var Thumb = {
|
|
|
|
dimension: 150, // thumbnails are all 150px
|
|
|
|
dimension: 75, // thumbnails are all 150px
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var supportedTypes = [
|
|
|
|
|
|
|
|
'image/png',
|
|
|
|
|
|
|
|
'image/jpeg',
|
|
|
|
|
|
|
|
'image/jpg',
|
|
|
|
|
|
|
|
'image/gif', // TODO confirm this is true
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thumb.isSupportedType = function (type) {
|
|
|
|
|
|
|
|
return supportedTypes.indexOf(type) !== -1;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// create thumbnail image from metadata
|
|
|
|
// create thumbnail image from metadata
|
|
|
@ -27,22 +38,67 @@ define([
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var getResizedDimensions = function (img) {
|
|
|
|
|
|
|
|
var h = img.height;
|
|
|
|
|
|
|
|
var w = img.width;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var dim = Thumb.dimension;
|
|
|
|
|
|
|
|
// if the image is too small, don't bother making a thumbnail
|
|
|
|
|
|
|
|
if (h <= dim || w <= dim) { return null; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the image is taller than it is wide, so scale to that.
|
|
|
|
|
|
|
|
var r = dim / (h > w? h: w); // ratio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var d;
|
|
|
|
|
|
|
|
if (h > w) {
|
|
|
|
|
|
|
|
d = Math.floor(((h * r) - dim) / 2);
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
x1: 0,
|
|
|
|
|
|
|
|
x2: dim,
|
|
|
|
|
|
|
|
y1: d,
|
|
|
|
|
|
|
|
y2: dim + d,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
d = Math.floor(((w * r) - dim) / 2);
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
x1: d,
|
|
|
|
|
|
|
|
x2: dim + d,
|
|
|
|
|
|
|
|
y1: 0,
|
|
|
|
|
|
|
|
y2: dim,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// assumes that your canvas is square
|
|
|
|
// assumes that your canvas is square
|
|
|
|
// nodeback returning blob
|
|
|
|
// nodeback returning blob
|
|
|
|
Thumb.fromCanvas = function (canvas, cb) {
|
|
|
|
Thumb.fromCanvas = Thumb.fromImage = function (canvas, cb) {
|
|
|
|
canvas = canvas;
|
|
|
|
|
|
|
|
var c2 = document.createElement('canvas');
|
|
|
|
var c2 = document.createElement('canvas');
|
|
|
|
var d = Thumb.dimension;
|
|
|
|
var D = getResizedDimensions(canvas);
|
|
|
|
c2.width = d;
|
|
|
|
if (!D) { return void cb('TOO_SMALL'); }
|
|
|
|
c2.height = 2;
|
|
|
|
|
|
|
|
|
|
|
|
c2.width = Thumb.dimension;
|
|
|
|
|
|
|
|
c2.height = Thumb.dimension;
|
|
|
|
|
|
|
|
|
|
|
|
var ctx = c2.getContext('2d');
|
|
|
|
var ctx = c2.getContext('2d');
|
|
|
|
ctx.drawImage(canvas, 0, 0, d, d);
|
|
|
|
ctx.drawImage(canvas, D.x1, D.y1, D.x2, D.y2);
|
|
|
|
c2.toBlob(function (blob) {
|
|
|
|
c2.toBlob(function (blob) {
|
|
|
|
cb(void 0, blob);
|
|
|
|
cb(void 0, blob);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thumb.fromImageBlob = function (blob, cb) {
|
|
|
|
|
|
|
|
var url = URL.createObjectURL(blob);
|
|
|
|
|
|
|
|
var img = new Image();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
img.onload = function () {
|
|
|
|
|
|
|
|
Thumb.fromImage(img, cb);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
img.onerror = function () {
|
|
|
|
|
|
|
|
cb('ERROR');
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
img.src = url;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Thumb.fromVideo = function (video, cb) {
|
|
|
|
Thumb.fromVideo = function (video, cb) {
|
|
|
|
cb = cb; // WIP
|
|
|
|
cb = cb; // WIP
|
|
|
|
};
|
|
|
|
};
|
|
|
|