javascript - Node.js 中是否需要清除 Buffer?如果是,如何?如果否,这里的问题是什么?

标签 javascript php node.js imagemagick gearman

我正在制作缩略图生成器,并且我正在使用 Gearman照片上传后立即在后台执行此操作。我有一个用 Node.js 编写的工作程序,它基本上执行缩略图处理(使用 imagemagick 作为 Node )并将其插入数据库。客户端是用 PHP 编写的,主要发送 Photo_id 和原始图像的 base64 编码字符串。 Node.js Worker 获取输入的 Base64 编码图像并对其进行处理。一切正常。我发现的问题是,当我通过以下方式调用 Node.js 工作程序时。 PHP第一次,生成了所有3个缩略图(我为每个图像生成3个缩略图),第二次和第三次也生成了所有3个缩略图,但第四次只生成了一个缩略图,之后无论我调用工作人员多少次,都不会生成缩略图。

我认为问题可能是缓冲区被填满,因为我正在使用 Node.js 缓冲区将 base64 字符串转换为二进制,反之亦然。

我搜索了清除缓冲区的方法,例如将缓冲区变量指向 null,在缓冲区变量上使用 js delete 函数。但是,这些似乎都没有帮助。要么缓冲区没有被清除,要么问题根本不在于缓冲区。

请仔细检查代码,并让我知道问题可能是什么。

PHP Gearman 客户端

<?php
$client= new GearmanClient();
$client->addServer('127.0.0.1',4731);
print $client->do("infinite", "[\"12246\", \"Base 64 image string in here\"]");
?>

Node.js Gearman 客户端

var fs = require('fs');
var db = require('./class.photo.js');
var im = require('imagemagick');
var Gearman = require('node-gearman/lib/gearman.js');
var gearman = new Gearman("127.0.0.1", 4731);
var db = new db();

function initializePic(id,base, req_type, callback) {
    console.log("Initializing thumbnail resize on " + id);

        var rawbase = base.split(",")[1];
        rawbase = rawbase.replace("\n", "");
        var buff = new Buffer(rawbase, 'base64');
        console.log(buff.length);
        var bin = buff.toString('binary');
        buff = null;

        console.time("image_" + req_type);
        imageResize(id, bin, properties[req_type].img_dest_width, properties[req_type].img_dest_height, imageRequestEnum[req_type], function (res) {
            if (res) {
                console.timeEnd(req_type);
                console.timeEnd("image_" + req_type);
            }
        });
}

function imageResize(id, bin, w, h, s, callback) {
    var fileName = id + '_' + w + '_' + h + '_thumb.jpg'
    console.log("Initializing resize process with filename " + fileName);
    im.resize({
        srcData: bin,
//        dstPath: fileName,
        width: w,
        height: h + '\!',
        quality: 0.7,
        strip: false,
        progressive: true,
    }, function (err, stdout, stderr) {
        if (err)
            throw err;
        if (stderr)
            throw stderr;

        var buff = new Buffer(stdout, "binary");
        console.log(buff.length);
        var b64 = buff.toString('base64');
        buff = null;

        if (b64) {
            b64 = "data:image/jpeg;base64," + b64;
            db.insertThumb(id, b64, s, function (res) {
                if (res) {
                    callback(true);
                }
            });
        }
    });
}


gearman.connect();

gearman.registerWorker("infinite", function (payload, worker) {
    var payload = payload.toString();
    var json = JSON.parse(payload);
    var id = json[0]; //getting the photo_id from the payload
    var base = json[1]; //Getting the base64 image data from the payload

    worker.end();
    console.time("user_image_small");
    initializePic(id,base, "user_image_small");
    console.time("user_image_medium");
    initializePic(id,base, "user_image_medium");
    console.time("profile_photo");
    initializePic(id,base, "profile_photo");
});

Node.js 控制台响应

Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
profile_photo: 151ms
image_profile_photo: 150ms
user_image_medium: 211ms
image_user_image_medium: 210ms
user_image_small: 218ms
image_user_image_small: 217ms
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
user_image_small: 98ms
image_user_image_small: 96ms
profile_photo: 142ms
image_profile_photo: 141ms
user_image_medium: 147ms
image_user_image_medium: 145ms
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
user_image_small: 90ms
image_user_image_small: 89ms
user_image_medium: 141ms
image_user_image_medium: 140ms
profile_photo: 138ms
image_profile_photo: 137ms
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
user_image_small: 94ms
image_user_image_small: 93ms
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246

请注意,在最近几次尝试中,它实际上并未创建任何缩略图。显示所用时间基本上意味着缩略图创建过程已完成。

我不确定问题是什么。寻求帮助。

最佳答案

经过多次故障排除后,发现问题出在 node-mysql 上。包裹。使用mysql Node.js 的包解决了这个问题。查看我的其他question and answer了解更多详情。

关于javascript - Node.js 中是否需要清除 Buffer?如果是,如何?如果否,这里的问题是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36258653/

相关文章:

php - 使用 __call() 在重载函数中使用引用

node.js - 开 Jest 测试不使用 dotenv 读取环境变量

javascript - 如何在 Nodejs 中的 .then() 函数中使用回调?

javascript - 在 NodeJS 中使用数组时“未定义不是函数”

javascript - Uncaught ReferenceError : fn is not defined

javascript - Cycle.js 中的 Rx 计时器状态未在 View 中更新

javascript - 我怎样才能执行javascript中的所有行

javascript - 使用模板文字添加多个类名

php - SQL 搜索语法错误

mysql - Sequelize 模型中的自动填充字段?