GraphicsMagick进程导致空文件 [英] GraphicsMagick processes resulting in empty file

查看:111
本文介绍了GraphicsMagick进程导致空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这样做

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);

我得到

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

如果我在这里写的第一行之前停止代码,然后查看文件,它有一个文件长度。如果我让这个过程发生(错误输出)那么看在文件中,它的大小为0字节。

If I stop the code right before the first line written here, and then look at the file, it has a file length. If I let this process happen (error out) then look at the file, then it has a size of 0 bytes.

我也试过这个:

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{//...

我的文件大小又回来了,所以我知道在开始这个过程之前它并不是空的。

And I'm getting a file size back, so I know it's not empty before I start this process.

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

I now start this process on form.on('end') so I could be sure the entire upload process was done.

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;
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });
    });
}


推荐答案

.write()接受字符串作为路径。所以你需要用字符串替换你的流(位置,必须保存所得到的图像)。

.write() accept string as path. So you need replace your stream with string (location, where resulting image must me saved).

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

GM support 3 ways of saving files.

1)保存到文件。很简单,只需使用 .write()并发送为参数字符串,其中应保存文件。示例:

1) Save to file. Simple, just use .write() and send as parameter string, where file should saved. Example:

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)最后一个 - 缓冲区:

3) And last one - buffers:

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

这篇关于GraphicsMagick进程导致空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆