UPDATE 2020-12-30
With sharp
after you've loaded the image (sharp(contents)
) make sure to add .rotate()
so it automatically rotates the image correctly based on EXIF data.
UPDATE 2020-12-13
I discovered that sharp
is much better than jimp
. It's order of maginitude faster. And it's actually what the Firebase Resize Images extension uses. Code updated below.
I have a Firebase app that uses the Firebase Cloud Storage to upload images. But now I need thumbnails. So I wrote a cloud function that can generate thumbnails on-the-fly.
There's a Firebase Extension called Resize Images which is nicely done but I just don't like that strategy. At least not for my app. Firstly, I'm forced to pick the right size(s) for thumbnails and I can't really go back on that. If I pick 50x50, 1000x1000
as my sizes, and depend on that in the app, and then realize that I actually want it to be 150x150, 500x500
then I'm quite stuck.
Instead, I want to pick any thumbnail sizes dynamically. One option would be a third-party service like imgix, CloudImage, or Cloudinary but these are not free and besides, I'll need to figure out how to upload the images there. There are other Open Source options like picfit which you install yourself but that's not an attractive option with its implicit complexity for a side-project. I want to stay in the Google Cloud. Another option would be this AppEngine function by Albert Chen which looks nice but then I need to figure out the access control between that and my Firebase Cloud Storage. Also, added complexity.
As part of your app initialization in Firebase, it automatically has access to the appropriate storage bucket. If I do:
const storageRef = storage.ref();
uploadTask = storageRef.child('images/photo.jpg').put(file, metadata);
...
...in the Firebase app, it means I can do:
admin
.storage()
.bucket()
.file('images/photo.jpg')
.download()
.then((downloadData) => {
const contents = downloadData[0];
...in my cloud function and it just works!
And to do the resizing I use Jimp which is TypeScript aware and easy to use. Now, remember this isn't perfect or mature but it works. It solves my needs and perhaps it will solve your needs too. Or, at least it might be a good start for your application that you can build on. Here's the function (in functions/src/index.ts
):
interface StorageErrorType extends Error {
code: number;
}
const codeToErrorMap: Map<number, string> = new Map();
codeToErrorMap.set(404, "not found");
codeToErrorMap.set(403, "forbidden");
codeToErrorMap.set(401, "unauthenticated");
export const downloadAndResize = functions
.runWith({ memory: "1GB" })
.https.onRequest(async (req, res) => {
const imagePath = req.query.image || "";
if (!imagePath) {
res.status(400).send("missing 'image'");
return;
}
if (typeof imagePath !== "string") {
res.status(400).send("can only be one 'image'");
return;
}
const widthString = req.query.width || "";
if (!widthString || typeof widthString !== "string") {
res.status(400).send("missing 'width' or not a single string");
return;
}
const extension = imagePath.toLowerCase().split(".").slice(-1)[0];
if (!["jpg", "png", "jpeg"].includes(extension)) {
res.status(400).send(`invalid extension (${extension})`);
return;
}
let width = 0;
try {
width = parseInt(widthString);
if (width < 0) {
throw new Error("too small");
}
if (width > 1000) {
throw new Error("too big");
}
} catch (error) {
res.status(400).send(`width invalid (${error.toString()}`);
return;
}
admin
.storage()
.bucket()
.file(imagePath)
.download()
.then((downloadData) => {
const contents = downloadData[0];
console.log(
`downloadAndResize (${JSON.stringify({
width,
imagePath,
})}) downloadData.length=${humanFileSize(contents.length)}\n`
);
const contentType = extension === "png" ? "image/png" : "image/jpeg";
sharp(contents)
.rotate()
.resize(width)
.toBuffer()
.then((buffer) => {
res.setHeader("content-type", contentType);
// TODO increase some day
res.setHeader("cache-control", `public,max-age=${60 * 60 * 24}`);
res.send(buffer);
})
.catch((error: Error) => {
console.error(`Error reading in with sharp: ${error.toString()}`);
res
.status(500)
.send(`Unable to read in image: ${error.toString()}`);
});
})
.catch((error: StorageErrorType) => {
if (error.code && codeToErrorMap.has(error.code)) {
res.status(error.code).send(codeToErrorMap.get(error.code));
} else {
res.status(500).send(error.message);
}
});
});
function humanFileSize(size: number): string {
if (size < 1024) return `${size} B`;
const i = Math.floor(Math.log(size) / Math.log(1024));
const num = size / Math.pow(1024, i);
const round = Math.round(num);
const numStr: string | number =
round < 10 ? num.toFixed(2) : round < 100 ? num.toFixed(1) : round;
return `${numStr} ${"KMGTPEZY"[i - 1]}B`;
}
Here's what a sample URL looks like.
I hope it helps!
I think the next thing for me to consider is to extend this so it uploads the thumbnail back and uses the getDownloadURL()
of the created thumbnail as a redirect instead. It would be transparent to the app but saves on repeated views. That'd be a good optimization.