【问题标题】:use -auto-orient with imagemagick in Firebase functions在 Firebase 函数中使用 -auto-orient 和 imagemagick
【发布时间】:2018-03-05 10:33:43
【问题描述】:

我按照 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 并尝试上传图片时,我收到以下错误消息,但它并没有提供很多关于它为什么不工作的信息

Function execution took 6227 ms, finished with status: 'connection error'

有什么想法吗?

【问题讨论】:

  • 你解决过这个问题吗?我遇到了同样的错误。
  • 不,还没有时间调查这个问题。
  • 我昨晚解决了这个问题。当您有时间调查时,请在下面查看我的答案!

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


【解决方案1】:

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

  • 未解决的承诺
  • 缺乏使用GCF提供的callback()函数
  • 使用require('child-process-promise').exec 代替require('child-process-promise').spawn

这是我的代码,它已经以 2x/秒的速度运行了 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);
        })
    }
}

【讨论】:

    【解决方案2】:

    正确的 args 顺序很重要。

     await spawn("convert", [src, "-auto-orient", "-thumbnail",  "200x200>", `${dir}/thumb_001_${name}${ext}`]);
    

    会工作,如果我有"-thumbnail" 然后"-auto-orient" 不会。

    imagemagick documentation

    此操作员读取并重置 EXIF 图像配置文件设置“方向”,然后对图像执行适当的 90 度旋转以定位图像,以便正确查看。

    And

    这类似于 -resize,除了它针对速度和任何 图像配置文件,而不是一个颜色配置文件,被删除以减少 缩略图大小。要剥离颜色配置文件,只需添加 -strip 此选项之前或之后

    所以问题是首先运行 -thubnail,剥离所有方向信息,并且当 -auto-orient 在没有方向设置和设置 EONNT 错误的情况下运行时。

    【讨论】:

      猜你喜欢
      • 2020-01-06
      • 2011-12-04
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      相关资源
      最近更新 更多