我可以在 Google 云函数中访问 GraphicsMagicks 或 ImageMagicks 吗?
Do I have access to GraphicsMagicks or ImageMagicks in a Google Cloud Function?
我想创建一个 Google Cloud Function 来正确设置上传文件的内容类型。我知道如何使用 GraphicsMagick 或 ImageMagick 执行此操作,但我不确定 Google Cloud Function 是否具有这些本机库。我如何知道他们是否有或没有我如何安装它们?
Google Cloud Functions 运行 在安装了 ImageMagick 的容器中。 Firebase documentation 似乎有最好的文档。从那里:
Cloud Functions provides an image-processing program called ImageMagick
that can perform manipulations on graphical image files. The following is an example of how to create a thumbnail image for an uploaded image file:
// Download file from bucket.
const bucket = gcs.bucket(fileBucket);
const tempFilePath = `/tmp/${fileName}`;
return bucket.file(filePath).download({
destination: tempFilePath
}).then(() => {
console.log('Image downloaded locally to', tempFilePath);
// Generate a thumbnail using ImageMagick.
return exec(`convert "${tempFilePath}" -thumbnail '200x200>' "${tempFilePath}"`).then(() => {
console.log('Thumbnail created at', tempFilePath);
// We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail.
const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, `thumb_`);
// Uploading the thumbnail.
return bucket.upload(tempFilePath, {
destination: thumbFilePath
});
});
});
This code executes the ImageMagick command line program convert to create a 200x200 thumbnail for the image saved in a temporary directory, then uploads it back to Cloud Storage.
另请参阅 Firebase 函数示例存储库,了解如何使用它的示例:https://github.com/firebase/functions-samples/tree/master/generate-thumbnail
我想创建一个 Google Cloud Function 来正确设置上传文件的内容类型。我知道如何使用 GraphicsMagick 或 ImageMagick 执行此操作,但我不确定 Google Cloud Function 是否具有这些本机库。我如何知道他们是否有或没有我如何安装它们?
Google Cloud Functions 运行 在安装了 ImageMagick 的容器中。 Firebase documentation 似乎有最好的文档。从那里:
Cloud Functions provides an image-processing program called
ImageMagick
that can perform manipulations on graphical image files. The following is an example of how to create a thumbnail image for an uploaded image file:// Download file from bucket. const bucket = gcs.bucket(fileBucket); const tempFilePath = `/tmp/${fileName}`; return bucket.file(filePath).download({ destination: tempFilePath }).then(() => { console.log('Image downloaded locally to', tempFilePath); // Generate a thumbnail using ImageMagick. return exec(`convert "${tempFilePath}" -thumbnail '200x200>' "${tempFilePath}"`).then(() => { console.log('Thumbnail created at', tempFilePath); // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail. const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, `thumb_`); // Uploading the thumbnail. return bucket.upload(tempFilePath, { destination: thumbFilePath }); }); });
This code executes the ImageMagick command line program convert to create a 200x200 thumbnail for the image saved in a temporary directory, then uploads it back to Cloud Storage.
另请参阅 Firebase 函数示例存储库,了解如何使用它的示例:https://github.com/firebase/functions-samples/tree/master/generate-thumbnail