我可以在Google Cloud Function中访问GraphicsMagicks或ImageMagicks吗? [英] Do I have access to GraphicsMagicks or ImageMagicks in a Google Cloud Function?

查看:117
本文介绍了我可以在Google Cloud Function中访问GraphicsMagicks或ImageMagicks吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个Google Cloud Function,它将正确设置上传文件的内容类型.我知道如何使用GraphicsMagick或ImageMagick,但是我不确定Google Cloud Function是否具有这些本机库.我如何确定他们是否有它们?或者失败,如何安装它们?

I want to make a Google Cloud Function that will correctly set the content type of uploaded files. I know how to do this with GraphicsMagick or ImageMagick, but I'm not sure if Google Cloud Function has those native libraries. How do I find out if they have them or failing that how do I get them installed?

推荐答案

Google Cloud Functions在安装了ImageMagick的容器中运行.以某种方式 Firebase文档似乎是最好的文档.从那里:

Google Cloud Functions run in a container that has ImageMagick installed. Somehow the Firebase documentation seems to have best documentation for it. From there:

Cloud Functions提供了一个名为ImageMagick的图像处理程序,可以对图形图像文件执行操作.以下是如何为上传的图像文件创建缩略图的示例:

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(/(\/)?([^\/]*)$/, `$1thumb_$2`);
        // Uploading the thumbnail.
        return bucket.upload(tempFilePath, {
          destination: thumbFilePath
        });
      });
    });

此代码执行ImageMagick命令行程序转换,以为保存在临时目录中的图像创建200x200的缩略图,然后将其上传回Cloud Storage.

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函数示例存储库,以获取有关如何使用它的示例:

Also see the Firebase functions-sample repo for an example of how to use it: https://github.com/firebase/functions-samples/tree/master/generate-thumbnail

这篇关于我可以在Google Cloud Function中访问GraphicsMagicks或ImageMagicks吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆