【问题标题】:ReferenceError: window is not defined when using mergeImages in express.jsReferenceError:在 express.js 中使用 mergeImages 时未定义窗口
【发布时间】:2021-05-09 12:01:22
【问题描述】:

我在我的 express.js 服务器上使用 mergeImages,但我收到“ReferenceError:未定义窗口”的错误。我不知道它在说什么。即使我没有在我的代码中使用窗口词。 请检查我的 server.js 代码。在代码的末尾我有 mergeimage 功能。

const express = require('express');
const app = express();
const fs = require('fs'); 
const mergeImages = require('merge-images');
const bodyParser = require('body-parser');
const multer = require('multer');
const { response } = require('express');
const cors = require('cors');

app.use(cors());

app.use(express.static(__dirname + '/public'));

app.use(bodyParser.json());      
app.use(bodyParser.urlencoded({    
  extended: true
}));

app.get('/', (req,res) => {
    res.send('this is express!');
})

app.listen(8000, (err) =>{
    if(err) console.log(err);
    console.log('Server Started please listen to port: 8000');
})

app.get('/get-image', function(req,res,next){ 
    const pixel_base_value = 1.31;
    const top_margin = 5;
    const left_margin = 3;
    var photo_frame = [];
    for(var j = 1; j <= req.query.row; j++){
      var total_width_check = 0;
      var total_height_check = 0;
      for(var k= 1; k <= req.query.col; k++){
          var height_cal =  req.query.photo_height*pixel_base_value;

          var width_cal = req.query.photo_width*pixel_base_value;

          if(j !== 1){
              var top1 = top_margin + height_cal + 3;
          }else{
              top1 = top_margin;
          }
          if(k !== 1){
              var left1 =  (width_cal * (k-1) + (left_margin * k));
          }else{
              left1 = left_margin;
          }
          total_width_check = total_width_check + req.query.photo_width*pixel_base_value + left_margin;

          total_height_check = total_height_check + req.query.photo_height*pixel_base_value + top_margin;

          if(total_width_check >= req.query.board_width*pixel_base_value-3 && total_height_check >= req.query.board_height*pixel_base_value-3){   

          }else{
              photo_frame.push({src:'http://localhost:8000/image/'+ req.query.img_name +'', x: left1, y: top1, width: width_cal, height: height_cal});
          }
      }
      total_width_check = 0;
  }
  //check photo_frame is a valid Array
  mergeImages(photo_frame).then((b64) =>{ //add options here for canvas
    console.log(b64)
  },(error) =>{
    console.log(error);
  } );
  return res.end('done');
})

请帮忙!

谢谢!

【问题讨论】:

    标签: javascript node.js image express image-processing


    【解决方案1】:

    我认为对于节点使用,您应该添加节点画布并通过选项对象传递它。目前,您没有使用画布。

    const mergeImages = require('merge-images');
    const { Canvas, Image } = require('canvas'); // import this
    
    mergeImages(['./body.png', './eyes.png', './mouth.png'], {. //pass Valid images in array
      Canvas: Canvas, // here you need to add canvas
      Image: Image
    })
      .then(b64 => ...);
    

    Node.js中的示例项目,可以查看here

    【讨论】:

    • 现在抛出 Error: Couldn't load image
    • 您传递的图片网址是否正确?
    猜你喜欢
    • 2016-02-13
    • 2021-02-02
    • 2021-03-30
    • 2020-10-08
    • 2018-05-27
    • 2020-09-18
    • 1970-01-01
    • 2020-05-06
    • 2020-11-12
    相关资源
    最近更新 更多