我想创建一个谷歌云函数来正确地设置上传文件的内容类型。我知道如何使用GraphicsMagick或ImageMagick实现这一点,但我不确定谷歌云函数是否拥有这些原生库。那么,我应该如何找出它们是否存在,如果不存在该怎么办?
我想创建一个谷歌云函数来正确地设置上传文件的内容类型。我知道如何使用GraphicsMagick或ImageMagick实现这一点,但我不确定谷歌云函数是否拥有这些原生库。那么,我应该如何找出它们是否存在,如果不存在该怎么办?
谷歌云函数在一个已安装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
convert
而不是带有magick
的完整软件包。 - alex88