我想制作一个 Google Cloud Function 来正确设置上传文件的内容类型。我知道如何使用 GraphicsMagick 或 ImageMagick 执行此操作,但我不确定 Google Cloud Function 是否具有这些本机库。我如何知道他们是否有它们,或者如果没有,我该如何安装它们?
Google Cloud Functions 在安装了 ImageMagick 的容器中运行。不知何故Firebase 文档似乎有最好的文档。从那里:
Cloud Functions 提供了一个名为的图像处理程序ImageMagick
可以对图形图像文件进行操作。以下是如何为上传的图像文件创建缩略图的示例:
// 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(/(\/)?([^\/]*)$/, `$1thumb_$2`);
// Uploading the thumbnail.
return bucket.upload(tempFilePath, {
destination: thumbFilePath
});
});
});
此代码执行 ImageMagick 命令行程序 Convert 为保存在临时目录中的图像创建 200x200 缩略图,然后将其上传回 Cloud Storage。
另请参阅 Firebase 函数示例存储库,了解如何使用它的示例:https://github.com/firebase/functions-samples/tree/master/generate-thumbnail
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)