2016-05-25 21:03:07 +01:00
|
|
|
import fs from 'mz/fs';
|
2018-06-02 15:50:39 +00:00
|
|
|
import jimp from 'jimp';
|
2015-07-09 23:01:43 +01:00
|
|
|
import debugname from 'debug';
|
|
|
|
const debug = debugname('hostr-api:resize');
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
const types = {
|
|
|
|
jpg: jimp.MIME_JPEG,
|
|
|
|
png: jimp.MIME_PNG,
|
|
|
|
gif: jimp.MIME_JPEG,
|
|
|
|
}
|
|
|
|
|
2016-05-25 21:03:07 +01:00
|
|
|
function cover(path, type, size) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-06-02 15:50:39 +00:00
|
|
|
jimp.read(path, (errIn, image) => {
|
2016-05-25 21:03:07 +01:00
|
|
|
debug('Image Opened');
|
|
|
|
if (errIn) {
|
|
|
|
reject(errIn);
|
|
|
|
}
|
2015-07-09 23:01:43 +01:00
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
image.quality(80).cover(size.width, size.height, (errOut, resized) => {
|
2016-05-25 21:03:07 +01:00
|
|
|
debug('Image Resized');
|
|
|
|
if (errOut) {
|
|
|
|
reject(errOut);
|
|
|
|
}
|
2016-05-24 21:33:09 +01:00
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
resized.getBuffer(types[type], (errBuf, buffer) => {
|
2016-05-25 21:03:07 +01:00
|
|
|
debug('Image Buffered');
|
|
|
|
if (errBuf) {
|
|
|
|
reject(errBuf);
|
|
|
|
}
|
|
|
|
resolve(buffer);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-05-24 21:33:09 +01:00
|
|
|
|
2016-05-25 21:03:07 +01:00
|
|
|
function scale(path, type, size) {
|
2016-05-24 21:33:09 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-06-02 15:50:39 +00:00
|
|
|
jimp.read(path, (errIn, image) => {
|
2016-05-25 21:03:07 +01:00
|
|
|
debug('Image Opened');
|
2016-05-24 21:33:09 +01:00
|
|
|
if (errIn) {
|
2016-05-25 21:03:07 +01:00
|
|
|
reject(errIn);
|
2016-05-24 21:33:09 +01:00
|
|
|
}
|
2016-05-25 21:03:07 +01:00
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
image.quality(80).cover(size.width, size.height, (errOut, resized) => {
|
2016-05-25 21:03:07 +01:00
|
|
|
debug('Image Resized');
|
2016-05-24 21:33:09 +01:00
|
|
|
if (errOut) {
|
2016-05-25 21:03:07 +01:00
|
|
|
reject(errOut);
|
2016-05-24 21:33:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-02 15:50:39 +00:00
|
|
|
resized.getBuffer(types[type], (errBuf, buffer) => {
|
2016-05-25 21:03:07 +01:00
|
|
|
debug('Image Buffered');
|
|
|
|
if (errBuf) {
|
|
|
|
reject(errBuf);
|
|
|
|
}
|
2016-05-24 21:33:09 +01:00
|
|
|
resolve(buffer);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
2016-05-25 21:03:07 +01:00
|
|
|
|
|
|
|
export default function resize(path, type, currentSize, newSize) {
|
|
|
|
debug('Resizing');
|
|
|
|
const ratio = 970 / currentSize.width;
|
2016-06-06 17:34:04 +01:00
|
|
|
debug(newSize.width, ratio);
|
2016-05-25 21:03:07 +01:00
|
|
|
if (newSize.width <= 150) {
|
|
|
|
debug('Cover');
|
|
|
|
return cover(path, type, newSize);
|
2016-06-06 17:34:04 +01:00
|
|
|
} else if (newSize.width >= 970 && ratio < 1) {
|
2016-05-25 21:03:07 +01:00
|
|
|
debug('Scale');
|
2016-06-06 15:37:00 +01:00
|
|
|
newSize.height = currentSize.height * ratio; // eslint-disable-line no-param-reassign
|
2016-05-25 21:03:07 +01:00
|
|
|
return scale(path, type, newSize);
|
|
|
|
}
|
|
|
|
debug('Copy');
|
|
|
|
return fs.readFile(path);
|
|
|
|
}
|