Sharp: Image resize not working with Sharp in Firebase Cloud function using Firebase Storage

Created on 1 Jun 2019  路  5Comments  路  Source: lovell/sharp

What are you trying to achieve?

I was using ImageMagick to resize an image but it does not support HEIC type from iPhones. I heard Sharp is faster but when I implemented in my code, it does not work at all. Am I missing something in my code? The function does not fail but it doesn't resize my image and does not return any error either for HEIC type. My project is Firebase cloud functions with typescript and the image is store in Firebase Storage.

Have you searched for similar questions?

Yes, but found nothing

Are you able to provide a standalone code sample that demonstrates this question?

Code snippet is part of Firebase Storage event: functions.storage.object().onFinalize
I tried to convert straight from the temporary file instead of exporting to another temporary file.

import * as sharp from 'sharp'

await sharp(tempFilePath).resize(300, 300)

In package.json

"dependencies": {
    "@google-cloud/storage": "^2.5.0",
    "@types/sharp": "^0.22.2",
    "child-process-promise": "^2.2.1",
    "firebase-admin": "^5.13.1",
    "firebase-functions": "^2.0.5",
    "firebase-tools": "^4.2.1",
    "node-fetch": "^2.2.0",
    "secure-compare": "^3.0.1",
    "sharp": "^0.22.1",
}

Are you able to provide a sample image that helps explain the question?

Any image I tested did not resize.

question

Most helpful comment

Looks like you're creating an image pipeline without using it. sharp does not automatically overwrite the input file, see the api docs here: https://sharp.pixelplumbing.com/en/stable/api-output/

You need to explicitly write the resulting image to a file:

await sharp(tempFilePath).resize(300, 300).toFile(`result.jpg`);

You can also receive it as a buffer:

const resultImage = await sharp(tempFilePath).resize(300, 300).toBuffer();

All 5 comments

Looks like you're creating an image pipeline without using it. sharp does not automatically overwrite the input file, see the api docs here: https://sharp.pixelplumbing.com/en/stable/api-output/

You need to explicitly write the resulting image to a file:

await sharp(tempFilePath).resize(300, 300).toFile(`result.jpg`);

You can also receive it as a buffer:

const resultImage = await sharp(tempFilePath).resize(300, 300).toBuffer();

Please see #1105 for future possible HEIC support in sharp, which will require you to compile libvips and its dependencies from source due to patent liabilities.

What does the image buffer means? I am new to image processing. I know of the video buffer.

@Dara-To Buffer in this context refers to binary data stored in a Node.js Buffer object - see https://nodejs.org/dist/latest/docs/api/buffer.html

I hope this information helped. Please feel free to re-open with more information if further help is required.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

henbenla picture henbenla  路  3Comments

natural-law picture natural-law  路  3Comments

terbooter picture terbooter  路  3Comments

paulieo10 picture paulieo10  路  3Comments

OleVik picture OleVik  路  3Comments