mirror of
https://github.com/strapi/strapi.git
synced 2025-07-21 16:10:18 +00:00
46 lines
840 B
JavaScript
46 lines
840 B
JavaScript
![]() |
'use strict';
|
||
|
/**
|
||
|
* Image manipulation functions
|
||
|
*/
|
||
|
|
||
|
const sharp = require('sharp');
|
||
|
|
||
|
const getDimensions = buffer =>
|
||
|
sharp(buffer)
|
||
|
.metadata()
|
||
|
.then(({ width, height }) => ({ width, height }))
|
||
|
.catch(err => {
|
||
|
// ignore invali formats
|
||
|
console.log(err);
|
||
|
return {};
|
||
|
});
|
||
|
|
||
|
const ThUMBNAIL_RESIZE_OPTIONS = {
|
||
|
width: 245,
|
||
|
height: 156,
|
||
|
fit: 'inside',
|
||
|
};
|
||
|
|
||
|
const generateThumbnail = file => {
|
||
|
return sharp(file.buffer)
|
||
|
.resize(ThUMBNAIL_RESIZE_OPTIONS)
|
||
|
.toBuffer()
|
||
|
.then(buffer => {
|
||
|
return getDimensions(buffer).then(dimensions => ({
|
||
|
...dimensions,
|
||
|
hash: `thumb_${file.hash}`,
|
||
|
ext: file.ext,
|
||
|
buffer,
|
||
|
}));
|
||
|
})
|
||
|
.catch(err => {
|
||
|
console.log(err);
|
||
|
return null;
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports = {
|
||
|
getDimensions,
|
||
|
generateThumbnail,
|
||
|
};
|