程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Firebase 函数 - 使用 ImageMagick 模糊图像失败,错误代码 4大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Firebase 函数 - 使用 ImageMagick 模糊图像失败,错误代码 4?

开发过程中遇到Firebase 函数 - 使用 ImageMagick 模糊图像失败,错误代码 4的问题如何解决?下面主要结合日常开发的经验,给出你关于Firebase 函数 - 使用 ImageMagick 模糊图像失败,错误代码 4的解决方法建议,希望对你解决Firebase 函数 - 使用 ImageMagick 模糊图像失败,错误代码 4有所启发或帮助;

我正在尝试编写一个模糊图像的基本 firebase 函数。代码主要基于firebase函数示例:

async function blurImage(filePath,bucketname,Metadata) {
  const templocalfile = path.join(os.tmpdir(),filePath);
  const templocalDir = path.dirname(templocalfilE);
  const bucket = admin.storage().bucket(bucketName);

  // Create the temp directory where the storage file will be downloaded.
  await mkdirp(templocalDir);

  // Download file from bucket.
  await bucket.file(filePath).download({desTination: templocalfilE});

  // Blur the image using ImageMagick.
  await spawn('convert',[templocalfile,'-chAnnel','RGBA','-blur','0x8',templocalfile]);

  // Uploading the Blurred image.
  await bucket.upload(templocalfile,{
    desTination: `${BLURRED_FolDER}/${filePath}`,Metadata: {Metadata: Metadata},// KeePing custom Metadata.
  });

  // Clean up the local file
  fS.UnlinkSync(templocalfilE);
}

我正在尝试使用 firebase functions:sHelltestData.Json 文件在本对此进行测试,但是不断收到错误消息

ChildProcessError: "convert C:\filePath -chAnnel RGBA -blur 0x8 C:\filePath" Failed with code 4

谁能告诉我如何解决这个问题?

解决方法

如果您使用此代码而不是 spawn 来模糊图像,您将看到更详细的错误版本:

const gm = require('gm').subClass({imageMagick: truE});

...

await new Promise((resolve,reject) => {
    gm(templocalFilE)
      .blur(0,16)
      .write(templocalFile,(err,stdout) => {
        if (err) {
          console.error('Failed to blur image.',err);
          reject(err);
        } else {
          console.log(`Blurred image: ${filePath}`);
          resolve(stdout);
        }
    });
  });

该错误会告诉您找不到 gm/convert 二进制文件。由于您使用的是 Windows,请安装 binaries 和 follow the instructions from this answer。我会在这里注明:

如果您在 Windows 上使用 gm,您应该在此处下载 Windows 二进制文件并将 gm.exe 添加到您的 Windows 环境 PATH 变量中。之后,您必须重新启动PC。然后用 npm install gm 安装对应的node包就可以了。

其他参资料:

Another code example to blur images

大佬总结

以上是大佬教程为你收集整理的Firebase 函数 - 使用 ImageMagick 模糊图像失败,错误代码 4全部内容,希望文章能够帮你解决Firebase 函数 - 使用 ImageMagick 模糊图像失败,错误代码 4所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。