1

I am resizing images in node using the sharp library. The function doing the resizing is in a separate module:

import sharp from "sharp";

const resize = async (sizes, sourceImageBuffer) => {
  const resizeResults = await Promise.allSettled(
    sizes.map(({ width, height, filename }) => ({
      filename,
      image: sharp(sourceImageBuffer, { limitInputPixels: false })
        .withMetadata()
        .resize({
          width,
          height,
          fit: "inside",
        }),
    }))
  );

  const rejectedResizes = resizeResults.filter(
    ({ status }) => status === "rejected"
  );

  rejectedResizes.forEach((i) => console.error(`Failed to resize image: ${i}`));

  return resizeResults.filter(
    ({ status }) => status === "fulfilled"
  );
};

export default resize;

Note, that the above function uses sharp's withMetadata function. This ensures that the metadata is passed from the source image to the output image.

I call this above resizing function by:

  • reading in an image to a buffer
  • resizing it to a given size
  • print out the original image metadata and the output image metadata
import * as fs from 'fs';
import { piexif } from "piexifjs";
import resize from './resizer.mjs';

const targetSizes = [
  {
    width: 120,
    height: 120,
    filename: "something.jpg",
  },
];

console.log('targetSizes');
console.log(targetSizes);

const image = fs.readFileSync("./source/image-with-metadata.jpg");
const originalImageMetadata = piexif.load(image.toString("binary"));

const results = await resize(targetSizes, image);
const resultBuffer = await results[0].value.image.toBuffer();

const resizedImageMetadata = piexif.load(resultBuffer.toString("binary"));

console.log("originalImageMetadata");
console.log(originalImageMetadata);
console.log("resizedImageMetadata");
console.log(resizedImageMetadata);

The output image's metadata differ's from the source image's metadata in several places, for example in this field:

"305": "Adobe Photoshop Lightroom 5.7.1 (Macintosh)"
"305": "Adobe Photoshop Lightroom 5.7.1 (Macintosh) (Adobe Photoshop Lightroom 5.7.1"

Here is the whole output:

targetSizes
[ { width: 120, height: 120, filename: 'something.jpg' } ]
originalImageMetadata
{
  '0th': {
    '271': 'NIKON CORPORATION',
    '272': 'NIKON D850',
    '274': 1,
    '282': [ 240, 1 ],
    '283': [ 240, 1 ],
    '296': 2,
    '305': 'Adobe Photoshop Lightroom 5.5 (Macintosh)',
    '306': '2019:08:09 15:46:27',
    '34665': 230
  },
  Exif: {
    '33434': [ 1, 160 ],
    '33437': [ 9, 1 ],
    '34850': 1,
    '34855': 200,
    '34864': 2,
    '34866': 200,
    '36864': '0231',
    '36867': '2019:08:08 08:46:39',
    '36868': '2019:08:08 08:46:39',
    '37377': [ 24609, 3361 ],
    '37378': [ 51562, 8133 ],
    '37380': [ 0, 1 ],
    '37381': [ 3, 1 ],
    '37383': 255,
    '37384': 0,
    '37385': 0,
    '37386': [ 70, 1 ],
    '37521': '19',
    '40961': 1,
    '40962': 1000,
    '40963': 1250,
    '41495': 2,
    '41728': '\x03',
    '41729': '\x01',
    '41985': 0,
    '41986': 1,
    '41987': 1,
    '41989': 70,
    '41990': 0,
    '41991': 0,
    '41992': 0,
    '41993': 0,
    '41994': 0,
    '41996': 0,
    '42034': [ [Array], [Array], [Array], [Array] ],
    '42036': '24.0-70.0 mm f/2.8'
  },
  GPS: {},
  Interop: {},
  '1st': {},
  thumbnail: null
}
resizedImageMetadata
{
  '0th': {
    '271': 'NIKON CORPORATION',
    '272': 'NIKON D850',
    '274': 1,
    '282': [ 240, 1 ],
    '283': [ 240, 1 ],
    '296': 2,
    '305': 'Adobe Photoshop Lightroom 5.5 (Macintosh) (Adobe Photoshop Lightroom 5.5',
    '306': '2019:08:09 15:46:27',
    '34665': 262
  },
  Exif: {
    '33434': [ 1, 160 ],
    '33437': [ 9, 1 ],
    '34850': 1,
    '34855': 200,
    '36864': '0231',
    '36867': '2019:08:08 08:46:39',
    '36868': '2019:08:08 08:46:39',
    '37377': [ 24609, 3361 ],
    '37378': [ 51562, 8133 ],
    '37380': [ 0, 1 ],
    '37381': [ 3, 1 ],
    '37383': 255,
    '37384': 0,
    '37385': 0,
    '37386': [ 70, 1 ],
    '37521': '19',
    '40960': '0100',
    '40961': 1,
    '40962': 96,
    '40963': 120,
    '41495': 2,
    '41728': '\x03',
    '41729': '\x01',
    '41985': 0,
    '41986': 1,
    '41987': 1,
    '41989': 70,
    '41990': 0,
    '41991': 0,
    '41992': 0,
    '41993': 0,
    '41994': 0,
    '41996': 0,
    '42034': [ [Array], [Array], [Array], [Array] ],
    '42036': '24.0-70.0 mm f/2.8'
  },
  GPS: {},
  Interop: {},
  '1st': {},
  thumbnail: null
}

I created a repository where all the above can be found. To run the example, just run npm install and npm start.

Why does the above metadata field change? I was under the impression that the fields will stay the same.

handris
  • 1,999
  • 8
  • 27
  • 41

0 Answers0