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

原文 标签 javascript php node.js imagemagick gearman

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

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

我在缓冲区中使用js delete函数寻找清除缓冲区的方法,例如将缓冲区变量指向null。但是,这些似乎都没有帮助。缓冲区没有被清除或者缓冲区根本没有问题。

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

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包中。对node.js使用mysql包解决了该问题。查看我的其他question and answer以获得更多详细信息。

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

相关文章:

javascript - AngularJS将两个列表合二为一

php - 将XMLHttpRequest.responseText存储为变量:(

php - socket 连接仅需要一根线程吗?

php - mysql中的票务预订查询,基于日期,目的地和返回日期

node.js - Slack API通过Webhook发送命令

javascript - 计算将使用 Javascript window.print 打印的页数

javascript - ReactJS 未捕获的类型错误 : Cannot read property 'target' of undefined

Zend 框架 2 中的 phpexcel fatal error : Class 'PHPExcel_IOFactory' not found

Javascript:如何在不创建循环依赖项的情况下重用创建子实例的方法

node.js - IE11 中的 http 伪流