I've got a cloud function for Firebase that triggers on a storage bucket write:
exports.convertToWebP = functions.storage.object().onChange(async event => {
const path = event.data.name;
const [pictureSlug, fileName] = path.split('/');
const [fileSlug, fileType] = fileName.split('.');
// Download the file
const tmpFilePath = `/tmp/${fileSlug}.${fileType}`;
const file = bucket.file(path);
await file.download({ destination: tmpFilePath });
await imagemin(
[`tmp/${fileSlug}.${fileType}`],
'/tmp',
{ use: [ imageminWebp({quality: 50})] });
const destination = `${pictureSlug}.webp`;
await bucket.upload(tmpFilePath, { destination });
The issue I'm having is that when I call the .download
method on the storage object, it goes to the /tmp/ folder, but I can't for the life of me figure out how to get imagemin
to grab the file.
It could have something to do with the way I'm constructing the path, or it could be something else like Cloud Functions not supporting imagemin.
The reason I'm doing this to being with is because the local instance of ImageMagick doesn't support webp.
Also: This file is a .ts file and I'm using the typescript compiler to create a js file of it. It compiles to an ES5 version.
imagemin.buffer()
and just use the memory buffer to download, convert, and reupload the image. – markgoho