Skip to content

Commit

Permalink
Adding conditional withMetaData on sharp (#49)
Browse files Browse the repository at this point in the history
* Added flag for sharp withMetaData

* Added changes to readme
  • Loading branch information
ShahoG authored Dec 20, 2020
1 parent 936c596 commit bf5f062
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ try {
- __jpegOptions__ [0-100] - Example: { force:true, quality:100 }
- __fit__ [string] - method by which the image should fit the width/height. Default = contain ([details](https://sharp.pixelplumbing.com/api-resize))
- __failOnError__ [boolean] - Set to false to avoid read problems for images from some phones (i.e Samsung) in the sharp lib. Default = true ([details](https://github.com/lovell/sharp/issues/1578))
- __withMetaData__ [boolean] - Keep metadata in the thumbnail (will increase file size)

### Examples
```js
Expand Down
42 changes: 24 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const { fail } = require('assert');
const PERCENTAGE = 10;
const RESPONSE_TYPE = 'buffer';

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

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

const fromUri = async (source, percentage, width, height, responseType, jpegOptions, fit, failOnError) => {
const fromUri = async (source, percentage, width, height, responseType, jpegOptions, fit, failOnError, withMetaData) => {
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, fit, failOnError);
const thumbnailBuffer = await sharpResize(imageBuffer, dimensions, jpegOptions, fit, failOnError, withMetaData);


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

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

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

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

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

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

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

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

if (responseType === 'base64') {
return thumbnailBuffer.toString('base64');
Expand All @@ -85,24 +85,25 @@ module.exports = async (source, options) => {
const jpegOptions = options && options.jpegOptions ? options.jpegOptions : undefined;
const fit = options && options.fit ? options.fit : undefined;
const failOnError = options && typeof(options.failOnError) !== 'undefined' ? options.failOnError : true;
const withMetaData = options && typeof(options.withMetaData) !== 'undefined' ? options.withMetaData : false;

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, fit, failOnError);
response = await fromReadStream(source, percentage, width, height, responseType, jpegOptions, fit, failOnError, withMetaData);
} else if (source instanceof Buffer) {
response = await fromBuffer(source, percentage, width, height, responseType, jpegOptions, fit, failOnError);
response = await fromBuffer(source, percentage, width, height, responseType, jpegOptions, fit, failOnError, withMetaData);
} else {
response = await fromUri(source, percentage, width, height, responseType, jpegOptions, fit, failOnError);
response = await fromUri(source, percentage, width, height, responseType, jpegOptions, fit, failOnError, withMetaData);
}
return response;
case 'string':
if (validator.isBase64(source)) {
return await fromBase64(source, percentage, width, height, responseType, jpegOptions, fit, failOnError);
return await fromBase64(source, percentage, width, height, responseType, jpegOptions, fit, failOnError, withMetaData);
} else {
return await fromPath(source, percentage, width, height, responseType, jpegOptions, fit, failOnError);
return await fromPath(source, percentage, width, height, responseType, jpegOptions, fit, failOnError, withMetaData);
}
default:
throw new Error('unsupported source type');
Expand All @@ -125,13 +126,18 @@ const getDimensions = (imageBuffer, percentageOfImage, dimensions) => {
return { width, height };
}

const sharpResize = (imageBuffer, dimensions, jpegOptions, fit, failOnError) => {
const sharpResize = (imageBuffer, dimensions, jpegOptions, fit, failOnError, withMetadata) => {
return new Promise((resolve, reject) => {
sharp(imageBuffer, { failOnError })
let result = sharp(imageBuffer, { failOnError })
.resize({
...dimensions, withoutEnlargement: true, fit: fit ? fit : 'contain',
})
.jpeg(jpegOptions ? jpegOptions : { force: false })

if(withMetadata){
result.withMetadata()
}

result.jpeg(jpegOptions ? jpegOptions : { force: false })
.toBuffer((err, data) => {
if (err) {
reject(err);
Expand Down

0 comments on commit bf5f062

Please sign in to comment.