簡體   English   中英

FabricJS從服務器端的路徑加載圖像?

[英]FabricJS load image from path at server side?

如何從服務器端的文件加載圖像並對其應用過濾器? 我需要類似波紋管的東西。 接收過濾器列表並從路徑加載圖像,應用過濾器並將圖像保存到另一路徑的功能。

const fabric = require('fabric').fabric;
const fs = require('fs');
const imageSize = require('image-size');

module.exports = {
  applyFilters(filters, imagePath, outputPath) {
    const out = fs.createWriteStream(outputPath);
    const { width, height } = imageSize(imagePath);
    const canvas = fabric.createCanvasForNode(null, { width, height });
    fabric.Image.fromPath(imagePath, function(img) {
      for (const filter of filters) {
        img.filters.push(filter)
      }
      img.applyFilters();
      canvas.add(img);
    });
    const stream = canvas.createPNGStream();
    stream.on('data', function(chunk) {
      out.write(chunk);
    });
  },
}

顯然fabric.Image.fromPath不存在,所以我正在尋找等效的東西。 圖像將成批上傳,並且位於相對於服務器的路徑中。 我會使用fromUrl作為最后一個資源,但我想避免提供原始圖像。

您可以使用fabric.Image.fromURL但根據fabricjs版本的不同,它可能會略有不同。

在Fabric 2.x中,您可以使用文件路徑...

fabric.Image.fromURL('/path/to/image.png', img => { ... });

在Fabric 3.x中,您需要使用文件URL

fabric.Image.fromURL('file:///path/to/image.png', img => { ... });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM