gpt4 book ai didi

node.js - 如果字符串包含图像,Fabric.js loadFromJSON 有时会在 Node.js 中失败

转载 作者:太空宇宙 更新时间:2023-11-04 02:23:05 26 4
gpt4 key购买 nike

我在服务器端使用 Fabric.js + Node.js 生成 PNG 图像时遇到问题。我想知道论坛中没有人发现类似的问题。我完全绝望了。这使得在我们的项目中使用 Fabric.js 面临风险。
Fabric.js Node.js 服务中的 PNG 图像生成偶尔会失败。我无法确定为什么有时会生成有时不会。
我需要在服务器端生成PNG。我根据示例 here 开发了一个小型 Node.js Web 服务和 here .
我还基于 Kangax 示例 here 开发了一个自定义 Fabric.js 图像类“RemoteImage” .
为了最小化 JSON 字符串大小,我在数据库中存储无数据 JSON,并且应该使用 Fabric.js Image 元素的“src”属性中提供的链接来加载图像。因此,我需要将以下 JSON 加载到包含 3 个图像的 Canvas 中:

{"objects":[{"type":"remote-image","originX":"left","originY":"top","left":44,"top":29,"width":976,"height":544,"fill":"rgb(0,0,0)","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":0.5,"scaleY":0.5,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","localId":"222c0a8b-46ac-4c01-9c5c-79753937bc24","layerName":"productCanvas","itemName":"mainCanvas","src":"http://localhost:41075/en/RemoteStorage/GetRemoteItemImage/222c0a8b-46ac-4c01-9c5c-79753937bc24","filters":[],"crossOrigin":"use-credentials","alignX":"none","alignY":"none","meetOrSlice":"meet","remoteSrc":"http://localhost:41075/en/RemoteStorage/GetRemoteItemImage/222c0a8b-46ac-4c01-9c5c-79753937bc24","lockUniScaling":true},
{"type":"remote-image","originX":"left","originY":"top","left":382.5,"top":152.25,"width":292,"height":291,"fill":"rgb(0,0,0)","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":0.43,"scaleY":0.43,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","localId":"8d97050e-eae8-4e95-b50b-f934f0df2d4c","itemName":"BestDeal.png","src":"http://localhost:41075/en/RemoteStorage/GetRemoteItemImage/8d97050e-eae8-4e95-b50b-f934f0df2d4c","filters":[],"crossOrigin":"use-credentials","alignX":"none","alignY":"none","meetOrSlice":"meet","remoteSrc":"http://localhost:41075/en/RemoteStorage/GetRemoteItemImage/8d97050e-eae8-4e95-b50b-f934f0df2d4c","lockUniScaling":true},
{"type":"remote-image","originX":"left","originY":"top","left":38,"top":38.5,"width":678,"height":370,"fill":"rgb(0,0,0)","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":0.21,"scaleY":0.21,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","localId":"42dc0e49-e45f-4aa7-80cf-72d362deebb7","itemName":"simple_car.png","src":"http://localhost:41075/en/RemoteStorage/GetRemoteItemImage/42dc0e49-e45f-4aa7-80cf-72d362deebb7","filters":[],"crossOrigin":"use-credentials","alignX":"none","alignY":"none","meetOrSlice":"meet","remoteSrc":"http://localhost:41075/en/RemoteStorage/GetRemoteItemImage/42dc0e49-e45f-4aa7-80cf-72d362deebb7","lockUniScaling":true}],"background":""}

在 Node.js 服务器端,我使用以下代码。我正在以 base64 编码传输 JSON 字符串,以避免一些特殊字符问题:

var fabric = require('fabric').fabric;

function generatePNG(response, postData) {
var canvas = fabric.createCanvasForNode(1500, 800);
var decodedData = new Buffer(postData, 'base64').toString('utf8');

response.writeHead(200, "OK", { 'Content-Type': 'image/png' });

console.log("decodedData data: " + JSON.stringify(decodedData));
console.log("prepare to load");

canvas.loadFromJSON(decodedData, function () {
console.log("loaded");
canvas.renderAll();
console.log("rendered");

var stream = canvas.createPNGStream();
stream.on('data', function (chunk) {
response.write(chunk);
});
stream.on('end', function () {
response.end();
});
});
}

在控制台中,我看到出现消息“准备加载”,但没有出现消息“已加载”。我不是 Node.js 专家,这是我确定 loadFromJSON 调用期间发生错误的唯一方法。但我不明白,问题出在哪里。
我在服务器端使用fabric v.1.5.0和node-canvas v.1.1.6。
Node.js + Fabric.js 服务在 Windows 8 计算机上运行。我正在使用 POST 请求从 .NET MVC 应用程序发出请求。

备注:可能我需要省略有关 Base64 编码的评论,因为它令人困惑。我尝试使用正常的 json 字符串运行并得到相同的结果。

最佳答案

如果 JSON 中引用的图像位于 NodeJS 服务器上,请尝试将文件路径更改为服务器上的目录路径,而不是 Web URL。

我不确定我是否完全理解您如何使用 Base64 图像,但 Base64 图像需要进行一些字符更正。我当然不记得具体细节,也没有方便执行此操作的代码,但 Google 搜索应该会为您指明正确的方向。

我希望这些想法有所帮助。

关于node.js - 如果字符串包含图像,Fabric.js loadFromJSON 有时会在 Node.js 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32035750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com