You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/image.js

152 lines
3.8 KiB
JavaScript

11 years ago
'use strict';
11 years ago
8 years ago
var os = require('os');
9 years ago
var fs = require('fs');
8 years ago
var path = require('path');
9 years ago
var Jimp = require('jimp');
var async = require('async');
8 years ago
var crypto = require('crypto');
var file = require('./file');
9 years ago
var plugins = require('./plugins');
11 years ago
9 years ago
var image = module.exports;
11 years ago
image.resizeImage = function (data, callback) {
if (plugins.hasListeners('filter:image.resize')) {
plugins.fireHook('filter:image.resize', {
path: data.path,
9 years ago
target: data.target,
extension: data.extension,
width: data.width,
height: data.height,
}, function (err) {
callback(err);
});
} else {
new Jimp(data.path, function (err, image) {
10 years ago
if (err) {
return callback(err);
}
var w = image.bitmap.width;
var h = image.bitmap.height;
var origRatio = w / h;
var desiredRatio = data.width && data.height ? data.width / data.height : origRatio;
var x = 0;
var y = 0;
var crop;
if (image._exif && image._exif.tags && image._exif.tags.Orientation) {
image.exifRotate();
}
9 years ago
if (origRatio !== desiredRatio) {
if (desiredRatio > origRatio) {
desiredRatio = 1 / desiredRatio;
9 years ago
}
if (origRatio >= 1) {
y = 0; // height is the smaller dimension here
x = Math.floor((w / 2) - (h * desiredRatio / 2));
9 years ago
crop = async.apply(image.crop.bind(image), x, y, h * desiredRatio, h);
} else {
x = 0; // width is the smaller dimension here
y = Math.floor((h / 2) - (w * desiredRatio / 2));
9 years ago
crop = async.apply(image.crop.bind(image), x, y, w, w * desiredRatio);
}
} else {
9 years ago
// Simple resize given either width, height, or both
crop = async.apply(setImmediate);
}
async.waterfall([
crop,
function (_image, next) {
9 years ago
if (typeof _image === 'function' && !next) {
next = _image;
_image = image;
}
if ((data.width && data.height) || (w > data.width) || (h > data.height)) {
_image.resize(data.width || Jimp.AUTO, data.height || Jimp.AUTO, next);
} else {
next(null, image);
}
},
function (image, next) {
image.write(data.target || data.path, next);
},
], function (err) {
callback(err);
});
});
}
11 years ago
};
image.normalise = function (path, extension, callback) {
if (plugins.hasListeners('filter:image.normalise')) {
plugins.fireHook('filter:image.normalise', {
path: path,
extension: extension,
}, function (err) {
8 years ago
callback(err, path + '.png');
});
} else {
async.waterfall([
function (next) {
new Jimp(path, next);
},
function (image, next) {
image.write(path + '.png', function (err) {
next(err, path + '.png');
});
},
], callback);
11 years ago
}
};
image.size = function (path, callback) {
if (plugins.hasListeners('filter:image.size')) {
plugins.fireHook('filter:image.size', {
path: path,
}, function (err, image) {
callback(err, image);
});
} else {
new Jimp(path, function (err, data) {
callback(err, data ? data.bitmap : null);
});
}
9 years ago
};
9 years ago
image.convertImageToBase64 = function (path, callback) {
fs.readFile(path, function (err, data) {
11 years ago
callback(err, data ? data.toString('base64') : null);
});
11 years ago
};
8 years ago
image.mimeFromBase64 = function (imageData) {
return imageData.slice(5, imageData.indexOf('base64') - 1);
};
image.extensionFromBase64 = function (imageData) {
return file.typeToExtension(image.mimeFromBase64(imageData));
};
image.writeImageDataToTempFile = function (imageData, callback) {
var filename = crypto.createHash('md5').update(imageData).digest('hex');
var type = image.mimeFromBase64(imageData);
var extension = file.typeToExtension(type);
var filepath = path.join(os.tmpdir(), filename + extension);
var buffer = Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64');
8 years ago
fs.writeFile(filepath, buffer, {
encoding: 'base64',
8 years ago
}, function (err) {
callback(err, filepath);
});
};