【问题标题】:Use imagemin in Cloud Functions for Firebase在 Cloud Functions for Firebase 中使用 imagemin
【发布时间】:2017-06-01 23:31:15
【问题描述】:

我有一个用于 Firebase 的云功能,它在存储桶写入时触发:

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 });

我遇到的问题是,当我在存储对象上调用 .download 方法时,它会转到 /tmp/ 文件夹,但我终生无法弄清楚如何获取 @987654323 @ 抓取文件。

这可能与我构建路径的方式有关,也可能与不支持 imagemin 的 Cloud Functions 之类的其他东西有关。

我这样做的原因是因为 ImageMagick 的本地实例不支持 webp。

另外:这个文件是一个 .ts 文件,我正在使用 typescript 编译器来创建它的 js 文件。它编译为 ES5 版本。

【问题讨论】:

  • 当我在等待解决这个问题时,我决定切换到imagemin.buffer(),只使用内存缓冲区来下载、转换和重新上传图像。

标签: firebase google-cloud-functions


【解决方案1】:

在这段代码中:

await imagemin(
      [`tmp/${fileSlug}.${fileType}`], 
      '/tmp',
      { use: [ imageminWebp({quality: 50})] });

看起来您正在为 tmp 文件构建一个与构建 tmpFilePath 时不同的字符串。它缺少前导斜杠。有什么理由不使用它吗?

await imagemin(
      [tmpFilePath], 
      '/tmp',
      { use: [ imageminWebp({quality: 50})] });

【讨论】:

  • 这主要是为了进行一些调试,以便我可以使用 ./tmp../tmp/tmp
  • 你应该使用 /tmp。这些其他选项不一定对读写有效,因为无法保证它们与什么相关。
猜你喜欢
  • 2018-01-14
  • 2017-08-02
  • 2017-09-07
  • 2018-01-03
  • 2022-12-01
  • 2017-08-07
  • 2017-09-27
  • 2017-08-02
  • 1970-01-01
相关资源
最近更新 更多