Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firebase在调整图像大小时速度非常慢_Javascript_Firebase_Imagemagick_Firebase Storage_Google Cloud Functions - Fatal编程技术网

Javascript Firebase在调整图像大小时速度非常慢

Javascript Firebase在调整图像大小时速度非常慢,javascript,firebase,imagemagick,firebase-storage,google-cloud-functions,Javascript,Firebase,Imagemagick,Firebase Storage,Google Cloud Functions,按照前面提到的示例,我使用firebase函数: 1) 将上载的照片调整为两个不同的尺寸 2) 然后,该函数将检索url并将其写入数据库,以便客户端应用程序可以在需要时检索适当的缩略图 不幸的是,我的计划从未实现,因为firebase函数完成其操作的速度非常慢,客户端应用程序等待那么长时间是不允许的。 请记住,我不是在这里谈论功能冷启动…无论图像大小或上传的图像数量如何,该功能在每次执行时都会保持缓慢 有什么特别的原因吗??我做错什么了吗 这是我的密码: exports.generateThum

按照前面提到的示例,我使用firebase函数:

1) 将上载的照片调整为两个不同的尺寸

2) 然后,该函数将检索url并将其写入数据库,以便客户端应用程序可以在需要时检索适当的缩略图

不幸的是,我的计划从未实现,因为firebase函数完成其操作的速度非常慢,客户端应用程序等待那么长时间是不允许的。 请记住,我不是在这里谈论功能冷启动…无论图像大小或上传的图像数量如何,该功能在每次执行时都会保持缓慢

有什么特别的原因吗??我做错什么了吗

这是我的密码:

exports.generateThumbnailImages = storageRef.onChange(event => {

const object = event.data; 
const fileBucket = object.bucket; 
const filePath = object.name; 
const contentType = object.contentType; 
const resourceState = object.resourceState; 
const metageneration = object.metageneration; 

var folder=filePath.split('/')[0];
var fileName=filePath.split('/')[1];
// console.log(folder);
if (folder){
    if (folder.startsWith('images') && !fileName.includes('thumb')) {
        if(resourceState=='not_exists'){
            console.log('THIS IS A DELETION EVENT '+fileName);
        }
        else if(resourceState==='exists' && metageneration>1){
            console.log('THIS IS A METAGENERATION EVENT '+fileName);
        }   
        else if(resourceState==='exists'){
            console.log('THIS IS A CREATION EVENT '+fileName);
            const bucket = gcs.bucket(fileBucket);
            const tempFilePath = '/tmp/${fileName}';
            const tempFilePathBig = '/tmp/${fileName}'+'Big';   
            bucket.file(filePath).download({destination: tempFilePath}).then(function() {
                bucket.file(filePath).download({destination: tempFilePathBig}).then(function() {
                    console.log('IMAGE DOWNLOADED LOCALLY TO', tempFilePath); 
                spawn('convert', [tempFilePath, '-thumbnail', '200x200', tempFilePath]).then(function() {
                    spawn('convert', [tempFilePathBig, '-thumbnail', '400x400', tempFilePathBig]).then(function() {
                        console.log('THUMBNAIL CREATED AT', tempFilePath);  
                    const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, '$1thumb_$2');     
                    const thumbFilePathBig = filePath.replace(/(\/)?([^\/]*)$/, '$1thumb_Big$2');       
                    bucket.upload(tempFilePath, {destination: thumbFilePath}).then(function(){
                        bucket.upload(tempFilePathBig, {destination: thumbFilePathBig}).then(function(){
                            return;
                        });
                    });
                });
                });
            });
        });  
        }`

您可以尝试将两个
spawn
命令组合成一个命令,而不必下载/处理同一图像两次

bucket.file(filePath)。下载({destination:tempFilePath})。然后(function(){
spawn('convert',[tempFilePath,
“-缩略图”,“400x400”,
“-write”,$tempFilePath400,
“-缩略图”,“200x200”,
“-write”,$tempFilePath200,
“null:”)。然后(函数(){
// ...

我还建议探索基于队列的后台工作程序;这样,客户端应用程序将知道工作已被推迟。

您可以尝试将两个
spawn
命令组合成一个命令,而不是两次下载/处理同一图像

bucket.file(filePath)。下载({destination:tempFilePath})。然后(function(){
spawn('convert',[tempFilePath,
“-缩略图”,“400x400”,
“-write”,$tempFilePath400,
“-缩略图”,“200x200”,
“-write”,$tempFilePath200,
“null:”)。然后(函数(){
// ...

我还建议探索一种基于队列的后台工作程序;这样,客户端应用程序就会知道工作被推迟了。

你看到了什么性能?你正在调整什么类型的图像?输入和输出大小是多少?@Frank我测量的平均输出(调整大小完成)大约为2-3分钟(!)根据函数日志。我尝试过不同的图像格式,但主要是jpg和png。输入大小也会因图像而异,但为了测试起见,让我们假设一个jpg,1920x1080,大小240kb,我们需要两个200x200和400x400的缩略图(尽管在图像不是矩形时会发生缩放,因此会调整这些尺寸)你找到解决方法了吗?我也有同样的问题。从我能告诉你的情况来看,spawn返回的承诺不尊重解决方案并等待超时?@CodeKiwi帮助你解决这个问题的最简单方法是观看这两个Firecast:1:2:使用呈现的代码作为基础,然后对其进行详细说明。它按预期工作!@GiorgosS.Thanks表示感谢,但我没有发现任何有帮助的东西。我已经在做所有应该做的事情。任何其他建议都将不胜感激。你看到了什么性能?你正在调整什么类型的图像大小?输入和输出大小是多少?@Frank平均输出(调整大小完成)根据函数日志,我测量了大约2-3分钟(!)。我尝试了不同的图像格式,但主要是jpg和png。输入大小也会因图像而异,但为了测试起见,让我们假设一个jpg,1920x1080,大小240kb,我们需要两个200x200和400x400的缩略图(尽管在图像不是矩形时会发生缩放,因此会调整这些尺寸)你找到解决方法了吗?我也有同样的问题。从我能告诉你的情况来看,spawn返回的承诺不尊重解决方案并等待超时?@CodeKiwi帮助你解决这个问题的最简单方法是观看这两个Firecast:1:2:使用呈现的代码作为基础,然后对其进行详细说明。它按预期工作!@GiorgosS.ThANK感谢您的提醒,但我没有发现任何有帮助的地方。我已经在做所有应该需要做的事情。任何其他建议都将不胜感激。感谢您的建议,但问题不是是否会通知客户潜在的延迟(或者如果有什么事正在排队)但是关于使用firebase函数的实际速度。我也尝试过用一个spawn命令调整大小,结果很不幸是一样的。大约2-3分钟。欢迎任何其他建议!感谢您的建议,但问题不在于是否会通知客户可能的延迟(或者如果队列中有什么东西)但是关于使用firebase函数的实际速度。我也尝试过用一个spawn命令调整大小,结果很不幸是一样的。大约2-3分钟。欢迎任何其他建议!