Firebase Cloud function imagemagick 复合命令叠加图片

Firebase Cloud function imagemagick composite command to overlay images

我正在尝试使用 Imagemagick on cloud 功能将一张图片放在另一张图片上,然后像这样查看 Imagemagick 命令:

convert a.png b.png -gravity center -composite result.png

我知道它不像 firebase 云函数那样工作,所以我找了一个例子,结果是这样的:

return spawn('convert', [tmpFilePath, tmpFilePath2, '-gravity', 'center', '-composite', tmpFilePath3]);


我收到这样的错误:

ChildProcessError: convert /tmp/default.jpg /tmp/IMG_4947.JPG -gravity center -composite /tmp/newimage.jpg failed with code 1

我刚试过你的代码,它有效。

首先导入os和路径:

const os = require('os');
const path = require('path');

然后你可以声明 tmp 路径:

const filename1 = 'default.jpg';
const filename2 = 'IMG_4947.JPG';
const filename3 = 'destination.jpg';

const tmpFilePath = path.join(os.tmpdir(), filename1);
const tmpFilePath2 = path.join(os.tmpdir(), filename2);
const tmpFilePath3 = path.join(os.tmpdir(), filename3);

之后您可以使用您的存储桶下载图像(如果您没有存储桶,您可以从您的活动中获取)。

bucket.file(filename1).download({
            destination: tmpFilePath
        }).then(()=>{
            bucket.file(filename2).download({
                destination: tmpFilePath2
            }).then(()=>{
                return spawn('convert', [tmpFilePath, tmpFilePath2, '-gravity', 'center', '-composite', tmpFilePath3]);
            }).then(()=>{
                //Upload the image (bucket.upload(tmpFilePath3, ....)
            })
        }).catch(()=>{
            //error handling..
        });

它也是 pos 可以并行而不是顺序地进行(Promise.all[a, b] 而不是 then().then())。