Javascript 如何使用Graphicsmagic创建缩略图?
我是node.js的新手,我正在尝试使用GraphicsMagic。我已经在我的windows pc中安装了GraphicsMagic,还安装了带有npm安装的gm模块。我能够得到完整的图像,但不能用下面的代码创建拇指。有人能帮我朝正确的方向走吗Javascript 如何使用Graphicsmagic创建缩略图?,javascript,node.js,Javascript,Node.js,我是node.js的新手,我正在尝试使用GraphicsMagic。我已经在我的windows pc中安装了GraphicsMagic,还安装了带有npm安装的gm模块。我能够得到完整的图像,但不能用下面的代码创建拇指。有人能帮我朝正确的方向走吗 var express = require('express'); var app = express(); var fs = require('fs'); var gm = require('gm'); app.configure(functi
var express = require('express');
var app = express();
var fs = require('fs');
var gm = require('gm');
app.configure(function () {
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
});
app.listen(8080);
console.log("App listening on port 8080");
app.post('/upload', function (req, res) {
fs.readFile(req.files.image.path, function (err, data) {
var imageName = req.files.image.name;
if (!imageName) {
console.log("There was an error");
res.redirect("/");
res.end();
} else {
var newPath = __dirname + "/uploads/fullsize/" + imageName;
var thumbPath = __dirname + "/uploads/thumbs/" + imageName;
fs.writeFile(newPath, data, function (err) {
gm(newPath)
.resize(240, 240)
.noProfile()
.write(thumbPath, function (err) {
if (!err) console.log('done');
res.redirect("/uploads/thumbs/"+imageName);
});
});
}
});
});
app.get('/uploads/thumbs/:file', function (req, res) {
file = req.params.file;
var img = fs.readFileSync(__dirname + "/uploads/thumbs/" + file);
res.writeHead(200, {'Content-Type': 'image/jpg' });
res.end(img, 'binary');
});
app.get('*', function (req, res) {
res.sendfile('./views/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});
index.html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
类型代码提示:在第一个<代码>如果分支,替换<代码> RES.Enter())/>代码> <代码>返回RES.Enter()/<代码>,并考虑在其中放入500代码。通过使用返回
,您可以丢失else
,并相应地减少下面逻辑的缩进。缩进是基于回调的敌人。解决方案是命名函数,并尽早且经常返回。任何时候,当你看到其他人时,试着用返回来避免它。你的错误是什么?我想@ptgamr也一样:没有错误。它应该生成一个图像,但它不起作用。