从PHP(Laravel)执行NodeJS脚本,在生产中找不到节点模块

发布于 2025-02-09 10:40:50 字数 2309 浏览 1 评论 0原文

我在API控制器中具有此功能,该功能执行节点文件,并且在Localhost上正常工作,这是脚本。

gifapicontroller.php

public function saveAndDissectGif(Request $request) {
        $gif = $request->file;
        //save the file temporarily in the asset-uploader directory
        Storage::disk('roblogif')->put('/',$gif);
        //dissect the gif into frames and get the frame count
        $output = $this->executeNodeFile('dissectGif.js');

        $frame_count = $output[0];

        //there's a 6.5 second delay between each frame, divide by 60 to get it in minutes
        //assume that 20 percent of the frames will fail, which means 20 percent will have a 1 minute delay
        $time_in_minutes = $frame_count * ( 6.5 / 60 ) + ( ( $frame_count * 0.2 ) );
        return ceil($time_in_minutes);
    }

private function executeNodeFile($javascript_file_name) {
        exec("cd ".__DIR__."; cd ../../asset-uploader; node ".$javascript_file_name, $output, $err);
        if($err)
            return $err;
        return $output;
    }

dissectgif.js从gif中获取所有帧,并将它们保存在文件夹中(使用GIF-Frames库)。为了从文件中获取输出,我正在登录它。

discectgif.js

const fs = require("fs")
const gifFrames = require('gif-frames');

async function startApp() {

    // get the gif
    let gif = fs.readdirSync("./gif");
    gif = gif[Object.keys(gif)[0]];

    // dissect gif into frames
    await gifFrames({ url: './gif/'+gif, frames: 'all', outputType: 'png', cumulative: true }).then(function (frameData) {
        frameData.forEach(async function (frame) {
            await frame.getImage().pipe(fs.createWriteStream('./gif-frames/'+frame.frameIndex+'.png'));
        });
    });
    //get and return the frame count so we can estimate loading time
    return fs.readdir('./gif-frames', (err, files) => { 
        console.log(files.length)
        return files.length
    });
}

startApp()

这对Localhost非常有用。 $ output [0]在gifapicontroller.php中获取dissectgif.js文件的files.length。但是,当我托管网站时,我会遇到一个错误,说 未定义的数组键0 ,这使我认为在生产模式下,它找不到FS和GIF-FRAMES库。

我试图将图书馆的完整目录放在:

const fs = require("../../node_modules/fs")

但这不起作用。我还尝试在执行脚本之前运行NPM安装,但这也不正常。

我认为问题可能来自GIF没有被保存,但是我检查了,这是。

有人对如何解决这个问题有想法吗?

I have this function in my API Controller that executes a node file and it works normally on localhost, here is the script.

GifApiController.php

public function saveAndDissectGif(Request $request) {
        $gif = $request->file;
        //save the file temporarily in the asset-uploader directory
        Storage::disk('roblogif')->put('/',$gif);
        //dissect the gif into frames and get the frame count
        $output = $this->executeNodeFile('dissectGif.js');

        $frame_count = $output[0];

        //there's a 6.5 second delay between each frame, divide by 60 to get it in minutes
        //assume that 20 percent of the frames will fail, which means 20 percent will have a 1 minute delay
        $time_in_minutes = $frame_count * ( 6.5 / 60 ) + ( ( $frame_count * 0.2 ) );
        return ceil($time_in_minutes);
    }

private function executeNodeFile($javascript_file_name) {
        exec("cd ".__DIR__."; cd ../../asset-uploader; node ".$javascript_file_name, $output, $err);
        if($err)
            return $err;
        return $output;
    }

dissectGif.js gets all the frames from a gif and saves them in a folder (using the gif-frames library). To get the output from the file, I am console logging it.

dissectGif.js

const fs = require("fs")
const gifFrames = require('gif-frames');

async function startApp() {

    // get the gif
    let gif = fs.readdirSync("./gif");
    gif = gif[Object.keys(gif)[0]];

    // dissect gif into frames
    await gifFrames({ url: './gif/'+gif, frames: 'all', outputType: 'png', cumulative: true }).then(function (frameData) {
        frameData.forEach(async function (frame) {
            await frame.getImage().pipe(fs.createWriteStream('./gif-frames/'+frame.frameIndex+'.png'));
        });
    });
    //get and return the frame count so we can estimate loading time
    return fs.readdir('./gif-frames', (err, files) => { 
        console.log(files.length)
        return files.length
    });
}

startApp()

This works great on localhost. $output[0] in GifApiController.php gets the files.length from the dissectGif.js file. But when I host the site, I get an error saying Undefined array key 0 which makes me think that in production mode, it's not finding the fs and the gif-frames libraries.

I tried to put the full directory of the libraries such as:

const fs = require("../../node_modules/fs")

but that didn't work. I also tried running npm install before executing the script but that didn't work as well.

I thought the issue could be from the gif not getting saved, but i checked and it was.

Does anybody have an idea on how to solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文