javascript - 在 Firebase 函数中使用 -auto-orient 和 imagemagick

标签 javascript firebase imagemagick google-cloud-functions imagemagick-convert

我遵循了 firecasts 中的 Firebase 函数教程:https://www.youtube.com/watch?v=pDLpEn3PbmE&t=338s在我上传图片时使用 firebase 创建缩略图。

这一切都很好,但是当我上传一张用 iPhone 拍摄的图像时,它被旋转了(垂直图像被水平保存)。所以我对此做了一些研究,然后我发现了 ImageMagick ( http://magick.imagemagick.org/script/command-line-options.php#auto-orient ) 的 -auto-orient 参数

但我不确定如何将此参数添加到 spawn 函数以将此参数考虑在内

没有 -auto-orient 我的工作代码(只是相关的部分)

...
return bucket.file(filePath).download({
        destination: tempFilePath
    })
    .then(() => {
        console.log('Image downloaded locally to', tempFilePath);
        return spawn('convert', [tempFilePath, '-thumbnail', '200x200>', tempFilePath]);
    })
    .then(() => {
        console.log('Thumbnail created!');
        const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, '$1thumb_$2');
        console.log(`thumbFilePath: ${thumbFilePath}`);
        return bucket.upload(tempFilePath, {
            destination: thumbFilePath
        });
    }) 
...

我尝试使用 -auto-orient 参数的代码

...
return bucket.file(filePath).download({
        destination: tempFilePath
    })
    .then(() => {
        console.log('Image downloaded locally to', tempFilePath);
        return spawn('convert', ['-auto-orient', tempFilePath, '-thumbnail', '200x200>', tempFilePath]);
    })
    .then(() => {
        console.log('Thumbnail created!');
        const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, '$1thumb_$2');
        console.log(`thumbFilePath: ${thumbFilePath}`);
        return bucket.upload(tempFilePath, {
            destination: thumbFilePath
        });
    })
...

但是当我将它部署到 firebase 并尝试上传图像时,我收到以下错误消息,它没有给我很多关于为什么它不起作用的信息

函数执行耗时 6227 毫秒,完成状态为:“连接错误”

有什么想法吗?

最佳答案

我在 GCF 中也遇到了“连接错误”消息。我做了 3 件事来修复它,但我不完全确定是只有 1 个原因还是全部 3 个原因。它们是:

  • Unresolved promise
  • 没有使用GCF提供的callback()函数
  • 使用 require('child-process-promise').exec 代替 require('child-process-promise').spawn

这是我的代码,它已经以每秒 2 倍的速度运行了 12 个小时,没有遇到“连接错误”消息。

const Storage = require('@google-cloud/storage');
const exec    = require('child-process-promise').exec;
const uuidv1  = require('uuid/v1');
const _       = require('lodash');

exports.processFile = (event, callback) => {
    const file         = event.data;

    if(file.contentType.indexOf('image/') !== 0) {
        console.log(file.name + ' is not an image');
        callback();
    } else if(!_.isUndefined(file.metadata) && !_.isUndefined(file.metadata['x-processed'])) {
        console.log(file.name + ' was already processed');
        callback();
    } else if(file.resourceState === 'not_exists') {
        console.log('This is a deletion event.');
        callback();
    } else if (file.resourceState === 'exists' && file.metageneration > 1) {
        console.log('This is a metadata change event.');
        callback();
    } else {
        const storage       = new Storage();
        const bucket        = storage.bucket(file.bucket);
        const parts         = file.name.split('.');
        const tempFilePath  = '/tmp/' + uuidv1() + '.' + _.last(parts);
        const tempFinalPath = '/tmp/' + uuidv1() + '.' + _.last(parts);

        console.log('Processing file: ' + file.name);

        return bucket.file(file.name).download({
            destination: tempFilePath
        })
        .then(() => {
            console.log('Image downloaded locally to ', tempFilePath);

            return exec(`convert -auto-orient "${tempFilePath}" "${tempFinalPath}"`)
        })
        .then(() => {
            console.log('uploading modified file to ' + file.name);

            return bucket.upload(tempFinalPath, {
                destination: file.name,
                contentType: file.contentType,
                metadata: {
                    metadata: {
                        "x-processed": "yes"
                    }
                }
            })
        })
        .then(() => {
            console.log('file uploaded successfully to ' + file.name);
            callback()
        })
        .catch((err) => {
            callback(err);
        })
    }
}

关于javascript - 在 Firebase 函数中使用 -auto-orient 和 imagemagick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49108402/

相关文章:

javascript - PHP Javascript 动态指示单击的菜单项

firebase - 如何在 https 的 firebase 函数中获取托管 url 而不是函数 url

javascript - 为什么 Ractive.js 的 reset() 和 update() 方法在有一个带有 array.prototype.sort() 的助手和一个空对象的数据时复制模板

javascript - 如何遍历对象并将键插入数组?

arrays - 当仅按下一个 Swift 时,阵列中的多个开关被激活

node.js - TypeError : admin. firestore.collection不是函数

node.js - Nodejs 文件附件处理程序

jQuery 将图像附加到 div 不显示,但可以在 Firebug 上看到它

ruby-on-rails - 使用延迟作业时自动定位图像

javascript - 如何使用div从右向左滑动显示内容