请问 Nodejs 关于图片 resize 问题

请问 Nodejs 关于图片 resize 问题

我是 刚开始 学习 nodejs 之前是开发 php的。

请教高手 使用 nodejs 的 easyimage module resize图片

express 4.8 / multer0.1.6 / easyimage 1.0.3/windows xp 在 localhost 运行

app.js app.use(multer({ dest: path.join(rootDir,‘public/uploads/’ ) }); //设置 图片 存储 路径

routes.js rootDir —> root path exports.register = function(req, res) {

var otarget_path = path.join(rootDir, ‘/public/uploads/’ + req.files.filename.name ); var target_path = path.join(rootDir, ‘/public/uploads/’ + req.files.filename.originalname );

easyimage.resize({src: otarget_path , dst: target_path , width:110, height:100}, function(err, img){ //想 执行 这里时 resize 可是 原图 上传到 path.join(rootDir,‘public/uploads/’ ) 这个路径 后 完事了。。。 if(err) console.log("resize err : ", err); console.log(img); }); } easyimage.resize 这里 为什么 执行 后 图片 没resize? 不懂 搞了 半天 实在 无办法 就请 高手 指点 。谢谢


2 回复

当然可以。根据你的描述,你正在尝试使用 easyimage 模块来实现图片的缩放功能。以下是一个详细的解答,包括如何正确地使用 easyimage 来进行图片的缩放操作。

首先,确保你已经安装了必要的模块:

npm install express multer easyimage

接下来,我们来看一下如何正确配置和使用这些模块来实现图片的上传和缩放。

app.js

app.js 中设置 Multer 以处理文件上传,并配置 Express 应用程序。

const express = require('express');
const multer = require('multer');
const path = require('path');
const easyimage = require('easyimage');

const app = express();
const rootDir = __dirname;

// 设置 Multer 存储路径
app.use(multer({
    dest: path.join(rootDir, 'public/uploads/')
}).single('filename'));

app.post('/upload', (req, res) => {
    const originalPath = path.join(rootDir, 'public/uploads/', req.file.originalname);

    // 调用 easyimage.resize 方法
    easyimage.resize({
        src: originalPath,
        dst: path.join(rootDir, 'public/uploads/', 'resized_' + req.file.originalname),
        width: 110,
        height: 100
    }, (err, image) => {
        if (err) {
            console.log("resize err:", err);
            return res.status(500).send("Error resizing image");
        }
        console.log(image);
        res.send("Image resized successfully");
    });
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

routes.js

routes.js 中定义一个路由处理器来处理文件上传和缩放请求。

const express = require('express');
const router = express.Router();

router.post('/register', (req, res) => {
    const rootDir = __dirname;
    const originalPath = path.join(rootDir, 'public/uploads/', req.body.filename);

    // 调用 easyimage.resize 方法
    easyimage.resize({
        src: originalPath,
        dst: path.join(rootDir, 'public/uploads/', 'resized_' + req.body.filename),
        width: 110,
        height: 100
    }, (err, image) => {
        if (err) {
            console.log("resize err:", err);
            return res.status(500).send("Error resizing image");
        }
        console.log(image);
        res.send("Image resized successfully");
    });
});

module.exports = router;

解释

  1. Multer 设置:在 app.js 中,Multer 被设置为将上传的文件存储在 public/uploads/ 目录下。
  2. 文件上传处理:在 app.post('/upload') 中,我们获取上传的文件路径并调用 easyimage.resize 方法来缩放图片。
  3. 错误处理:如果在缩放过程中发生错误,我们将返回一个 500 错误响应。
  4. 路由定义:在 routes.js 中定义了一个 /register 路由,用于处理文件上传和缩放请求。

通过上述步骤,你应该能够成功地上传和缩放图片。如果仍然有问题,请检查是否有任何错误信息或日志输出。


在你的描述中,easyimage.resize 方法可能没有正确执行的原因有几个。首先,确保 easyimage 库已经安装,并且正确引用了库。其次,确保源文件(otarget_path)存在并且可访问。最后,检查是否传递给 easyimage.resize 的参数是正确的。

下面是一个简化后的代码示例,展示如何使用 easyimage 来调整图片大小:

const easyimage = require('easyimage');
const multer = require('multer');
const path = require('path');

const upload = multer({
    dest: path.join(__dirname, 'public/uploads/')
});

app.post('/upload', upload.single('filename'), (req, res) => {
    const sourcePath = path.join(__dirname, 'public/uploads/', req.file.originalname);
    const targetPath = path.join(__dirname, 'public/uploads/', 'resized_' + req.file.originalname);

    easyimage.resize({
        src: sourcePath,
        dst: targetPath,
        width: 110,
        height: 100
    }, (err, image) => {
        if (err) {
            console.error("Error resizing image:", err);
            return res.status(500).send('Image resize failed.');
        }
        console.log("Image resized successfully:", image);
        res.send('Image resized successfully.');
    });
});

解释

  1. 引入依赖

    • easyimage 用于处理图片。
    • multer 用于处理表单上传。
    • path 用于处理文件路径。
  2. 配置 Multer

    • 设置上传目录为 public/uploads/
  3. 定义路由

    • 接收一个名为 filename 的文件上传。
    • 构建源文件路径和目标文件路径。
    • 使用 easyimage.resize 方法调整图片大小。
  4. 错误处理

    • 如果 resize 操作失败,返回 500 错误并打印错误信息。
    • 如果成功,返回成功的响应。

确保 easyimage 已经通过 npm install easyimage 安装。如果仍然无法正常工作,建议检查文件路径和权限设置。

回到顶部