GraphicsMagick处理导致文件为空

9

我正在进行这项工作

gm(jpgName).setFormat('jpg')
    .resize(160,158)
    .compress('JPEG')
.write(fs.createWriteStream(jpgName),function(err){
    if(err){
        console.log(err,jpgName);
    res.send(400);
    }else{
        console.log('file formated to jpg' + jpgName);

And I get

{ [Error: Command failed: gm convert: Empty input file (/home/ubuntu/node/uploads/icon.jpg). ] code: 1, signal: null }

如果我在这里写的第一行代码之前停止代码,然后查看文件,它会有一个文件长度。如果我让这个过程发生(错误),然后再查看文件,那么它的大小为0字节。
我还尝试了这个:
fs.stat(jpgName, function (err, stats) {
    console.log('file size:',stats.size);
    gm(jpgName).setFormat('jpg')
        .resize(160,158)
        .compress('JPEG')
        .write(fs.createWriteStream(jpgName),function(err){
            if(err){
                console.log('console.log error writing jpg file',err,jpgName);
                    fs.unlink(jpgName);
                        callback(400);
                        return;
                    }else{//...

我收到了文件大小的返回值,因此在开始这个过程之前,我知道它不是空的。

编辑:现在我在 form.on('end') 上开始这个过程,以确保整个上传过程已完成。

整个函数如下:

function formatProfileImage(req,form,file,callback){
    console.log('formatting profile image');
    var ext = file.name.split('.')[1].toLowerCase();
    var name = encodeURIComponent(file.name.split('.')[0].toLowerCase());
    var smallName = form.uploadDir+"/"+"small_"+name+".jpg";
    var jpgName = form.uploadDir + "/" + name+'.jpg';

    console.log('extension:',ext);
    console.log('name:',name);
    console.log('smallName:',smallName);
    console.log('jpgName:',jpgName);

    if(!(ext == "png" || ext == "jpeg"|| ext == "jpg"|| ext == "gif")){
        fs.unlink(file.path);
        console.log("extension rejected");
        callback(415);
        return; 
    }

    console.log('renaming file from ' + file.path + ' to ' + jpgName);

    fs.rename(file.path,jpgName,function(err){
        if(err){
            console.log('error renaming file',err);
            callback(400);
            return;
        }

        console.log('file renamed');
        fs.stat(jpgName, function (err, stats) {
            console.log('file size:',stats.size);
            if(!(typeof req.query.tenant === 'undefined')){
                //rename to jpg
                gm(jpgName).setFormat('jpg')
                .resize(160,158)
                .compress('JPEG')
                .write(fs.createWriteStream(jpgName),function(err){
                    if(err){
                        console.log('console.log error writing jpg file',err,jpgName);
                        fs.unlink(jpgName);
                        callback(400);
                        return;
                    }else{
                        console.log('file formated to jpg' + jpgName);
                        //make "thumbnail"
                        gm(jpgName).setFormat('jpg')
                        .resize(50)
                        .write(fs.createWriteStream(smallName),function(err){
                            if(err){
                                fs.unlink(smallName);
                                console.log('error writing thumbnail',err,smallName);
                                callback(400);
                                return;
                            }else{
                                console.log('thumbnail created' + smallName);
                                //upload everything
                                uploadS3(jpgName,req.query.tenant,function(back){
                                    if(back.success){
                                        console.log('success ' +back.url);
                                        callback(back.url);
                                        fs.unlink(jpgName);
                                    }else{
                                        console.log('error uploading' + jpgName, back);
                                        callback(400);
                                        fs.unlink(jpgName);
                                        return;
                                    }
                                });
                                uploadS3(smallName,req.query.tenant,function(back){
                                    if(back.success){
                                        console.log('success ' +back.url);
                                        fs.unlink(smallName);
                                    }else{
                                        console.log('error uploading ' + smallName, back);
                                        callback(400);
                                        fs.unlink(smallName);
                                        return;
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });
    });
}

你有检查你的输入文件是否为空吗?它在之前可能已经变为空了,因为fs.createWriteStream(jpgName)没有写入任何内容,可能是因为其他错误导致的。 - Volune
@Volune 我可以在第一行运行之前放置一个返回并查看文件大小,以便确认它不是空的。我有这个文件在发生任何其他过程之前和之后的日志,因此我认为没有其他事情会影响该文件。 - user773737
我稍后会尝试(现在没有gm),但我很好奇为什么错误消息会说输入文件是空的,如果它不是空的。 - Volune
@Volune 它在完成其操作后变为空。 - user773737
@Houseman:最新的Node中gm("image.png").filesize()失败了。你能确认一下吗?看起来像是gm()的问题,而不是编码问题。 - Alvin K.
显示剩余2条评论
1个回答

6

.write()接受字符串作为路径。因此,您需要使用字符串(位置,必须保存生成的图像的位置)替换流。

GM支持三种保存文件的方式。

1)保存到文件。很简单,只需使用.write()并将字符串作为参数发送,指定文件保存的位置。例如:

gm('image.png')
    .resize()
    .write('resized.png', function (error) {
        if (error) {
            console.error(error);
        }
    });

2) 使用流,示例:

gm('image.png')
    .resize()
    .stream(function (error, stdout, stderr) {
        var writeStream = fs.createWriteStream('resized.jpg');
        stdout.pipe(writeStream);
    });

3) 最后一个 - 缓冲区:

gm('image.png')
    .resize()
    .toBuffer(function (error, buffer) {
        if (error) {
            console.error(error);
        }
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接