Skip to content

Commit

Permalink
Add option to change fit (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
leibowitz authored Jul 15, 2020
1 parent f5d2ec0 commit a9f70a6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ try {
- __height__ [number] - image thumbnail height.
- __responseType__ ['buffer' || 'base64'] - response output type. Default = 'buffer'
- __jpegOptions__ [0-100] - Example: { force:true, quality:100 }
- __fit__ [string] - method by which the image should fit the width/height. Default = contain

### Examples
```js
Expand Down
37 changes: 19 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const util = require('./util');
const PERCENTAGE = 10;
const RESPONSE_TYPE = 'buffer';

const fromBase64 = async (source, percentage, width, height, responseType, jpegOptions) => {
const fromBase64 = async (source, percentage, width, height, responseType, jpegOptions, fit) => {
const imageBuffer = Buffer.from(source, 'base64');
const dimensions = getDimensions(imageBuffer, percentage, { width, height });
const thumbnailBuffer = await sharpResize(imageBuffer, dimensions, jpegOptions);
const thumbnailBuffer = await sharpResize(imageBuffer, dimensions, jpegOptions, fit);

if (responseType === 'base64') {
return thumbnailBuffer.toString('base64');
Expand All @@ -23,12 +23,12 @@ const fromBase64 = async (source, percentage, width, height, responseType, jpegO
return thumbnailBuffer;
};

const fromUri = async (source, percentage, width, height, responseType, jpegOptions) => {
const fromUri = async (source, percentage, width, height, responseType, jpegOptions, fit) => {
const response = await axios.get(source.uri, { responseType: 'arraybuffer' });
const imageBuffer = Buffer.from(response.data, 'binary');

const dimensions = getDimensions(imageBuffer, percentage, { width, height });
const thumbnailBuffer = await sharpResize(imageBuffer, dimensions, jpegOptions);
const thumbnailBuffer = await sharpResize(imageBuffer, dimensions, jpegOptions, fit);


if (responseType === 'base64') {
Expand All @@ -38,11 +38,11 @@ const fromUri = async (source, percentage, width, height, responseType, jpegOpti
return thumbnailBuffer;
};

const fromPath = async (source, percentage, width, height, responseType, jpegOptions) => {
const fromPath = async (source, percentage, width, height, responseType, jpegOptions, fit) => {
const imageBuffer = fs.readFileSync(source);

const dimensions = getDimensions(imageBuffer, percentage, { width, height });
const thumbnailBuffer = await sharpResize(imageBuffer, dimensions, jpegOptions);
const thumbnailBuffer = await sharpResize(imageBuffer, dimensions, jpegOptions, fit);

if (responseType === 'base64') {
return thumbnailBuffer.toString('base64');
Expand All @@ -51,10 +51,10 @@ const fromPath = async (source, percentage, width, height, responseType, jpegOpt
return thumbnailBuffer;
};

const fromReadStream = async (source, percentage, width, height, responseType, jpegOptions) => {
const fromReadStream = async (source, percentage, width, height, responseType, jpegOptions, fit) => {
const imageBuffer = await util.streamToBuffer(source);
const dimensions = getDimensions(imageBuffer, percentage, { width, height });
const thumbnailBuffer = await sharpResize(imageBuffer, dimensions, jpegOptions);
const thumbnailBuffer = await sharpResize(imageBuffer, dimensions, jpegOptions, fit);

if (responseType === 'base64') {
return thumbnailBuffer.toString('base64');
Expand All @@ -63,11 +63,11 @@ const fromReadStream = async (source, percentage, width, height, responseType, j
return thumbnailBuffer;
};

const fromBuffer = async (source, percentage, width, height, responseType, jpegOptions) => {
const fromBuffer = async (source, percentage, width, height, responseType, jpegOptions, fit) => {
const imageBuffer = source;

const dimensions = getDimensions(imageBuffer, percentage, { width, height });
const thumbnailBuffer = await sharpResize(imageBuffer, dimensions, jpegOptions);
const thumbnailBuffer = await sharpResize(imageBuffer, dimensions, jpegOptions, fit);

if (responseType === 'base64') {
return thumbnailBuffer.toString('base64');
Expand All @@ -82,24 +82,25 @@ module.exports = async (source, options) => {
const height = options && options.height ? options.height : undefined;
const responseType = options && options.responseType ? options.responseType : RESPONSE_TYPE;
const jpegOptions = options && options.jpegOptions ? options.jpegOptions : undefined;
const fit = options && options.fit ? options.fit : undefined;

try {
switch (typeof source) {
case 'object':
let response;
if (source instanceof fs.ReadStream || source instanceof stream.PassThrough) {
response = await fromReadStream(source, percentage, width, height, responseType, jpegOptions);
response = await fromReadStream(source, percentage, width, height, responseType, jpegOptions, fit);
} else if (source instanceof Buffer) {
response = await fromBuffer(source, percentage, width, height, responseType, jpegOptions);
response = await fromBuffer(source, percentage, width, height, responseType, jpegOptions, fit);
} else {
response = await fromUri(source, percentage, width, height, responseType, jpegOptions);
response = await fromUri(source, percentage, width, height, responseType, jpegOptions, fit);
}
return response;
case 'string':
if (validator.isBase64(source)) {
return await fromBase64(source, percentage, width, height, responseType, jpegOptions);
return await fromBase64(source, percentage, width, height, responseType, jpegOptions, fit);
} else {
return await fromPath(source, percentage, width, height, responseType, jpegOptions);
return await fromPath(source, percentage, width, height, responseType, jpegOptions, fit);
}
default:
throw new Error('unsupported source type');
Expand All @@ -122,11 +123,11 @@ const getDimensions = (imageBuffer, percentageOfImage, dimensions) => {
return { width, height };
}

const sharpResize = (imageBuffer, dimensions, jpegOptions) => {
const sharpResize = (imageBuffer, dimensions, jpegOptions, fit) => {
return new Promise((resolve, reject) => {
sharp(imageBuffer)
.resize({
...dimensions, withoutEnlargement: true, fit: 'contain',
...dimensions, withoutEnlargement: true, fit: fit ? fit : 'contain',
})
.jpeg(jpegOptions ? jpegOptions : { force: false })
.toBuffer((err, data) => {
Expand All @@ -137,4 +138,4 @@ const sharpResize = (imageBuffer, dimensions, jpegOptions) => {
}
});
});
};
};

0 comments on commit a9f70a6

Please sign in to comment.