求助。imagemagick压缩一直报错
发布于 3 个月前 作者 linkenliu 396 次浏览 来自 问答

当我上传图片压缩的时候就报这个错,上传的图片路径又是正确的,在浏览器上面可以打开。, events.js:141 throw er; // Unhandled ‘error’ event ^

Error: spawn convert ENOENT at exports._errnoException (util.js:874:11) at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32) at onErrorNT (internal/child_process.js:344:16) at doNTCallback2 (node.js:439:9) at process._tickCallback (node.js:353:17)

代码如下:

exports.logoUpload = function(req, res) {
    logger.info("================>>>>",req.files);

    var chid = req.params.chid;

    if (req.files.image && req.files.image.size > 0) { // include image file

        var uploadImage = req.files.image;
        if (uploadImage.type.indexOf('image') >= 0) {

            var smallFileName = chid + "-s" + uploadImage.name.substring(uploadImage.name.lastIndexOf('.'));
            var bigFileName = chid + "-b" + uploadImage.name.substring(uploadImage.name.lastIndexOf('.'));
            var newPath = imager.logoDir + "/" + bigFileName;
            fse.removeSync(smallFileName);
            fse.removeSync(bigFileName);
            var resizeBigOption = imager.logoSizeOption.big;
            var resizeSmallOption = imager.logoSizeOption.small;
            resizeBigOption.srcPath = uploadImage.path;
            resizeBigOption.dstPath = imager.logoDir+bigFileName;
            resizeSmallOption.srcPath = uploadImage.path;
            resizeSmallOption.dstPath = imager.logoDir+smallFileName;
            console.log("resizeBigOption============="+JSON.stringify(resizeBigOption));
            console.log("resizeSmallOption============="+JSON.stringify(resizeSmallOption));
            im.resize(resizeBigOption, function(err) {
                if (err) {
                    console.error(err);
                    res.json({ "result": "服务器出错!" });
                } else {
                    im.resize(resizeSmallOption, function(err1) {
                        if(err1) {
                            console.error(err1);
                        } else {
                            fse.removeSync(uploadImage.path);
                            var updateData ={"logo":{}};
                            updateData.logo.small = config.staticServer + imager.logoPath + smallFileName;
                            updateData.logo.big = config.staticServer + imager.logoPath + bigFileName;

                            Channel.findOneAndUpdate({chid: chid }, updateData, {upsert:false }, function(err2, ch) {
                                if(err2) {
                                    console.error(err);
                                    res.json({ "result": "服务器出错!" });
                                }
                                else {
                                    logger.info("OK")
                                    res.json({ "result": "OK","imgurl": imager.logoPath + smallFileName });
                                }
                            });
                        }
                    });
                }

            });

        } else {
            logger.error("Error: Input file is not a image file!");
            res.json({ "result": "提交的不是图片文件!" });
        }
    } else { // change images url
        logger.error("Error: No image file or file size is zero!");
        res.json({"result": "上传文件为空!"});
    }

};

3 回复

看样子是路径找不到?看看imagemagick是不是在环境变量里面。试试在命令行压缩能否成功

@DevinXian 用这个模块imagemagick?还需要把它安装在电脑系统中?我现在就只npm install imagemagick

@linkenliu Requires imagemagick CLI tools to be installed.

回到顶部