我按照Firecast中的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'
任何想法?
答案 0 :(得分:1)
我也遇到了#34;连接错误" GCF内部的消息。我做了3件事来解决它,虽然我不完全确定是否只有1是原因或者它是全部3.它们是:
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);
})
}
}