简体   繁体   English

如何以用户友好的方式在计算机上保存从画布转换的图像?

[英]How to save an image converted from a canvas, on the computer, in a user-friendly manner?

I'm using Nihilogic's library Canvas2Image to convert canvas drawings to PNG, and to give the users of my application the possibility to download that image. 我正在使用Nihilogic的库Canvas2Image将画布图转换为PNG,并为我的应用程序的用户提供下载该图像的可能性。

What I need is to be able to give the downloadable file a name and the png extension (eg "goalboard.png") and not have it download just as an octet stream with no recognizable extension and the name "download", because the average Joe won't know what to do with such a file. 我需要的是能够为可下载文件提供一个名称和png扩展名(例如“goalboard.png”),而不是将其下载为八位字节流,没有可识别的扩展名和“下载”这个名称,因为平均值乔不知道如何处理这样的文件。 And I need to do this on the client-side, because sending that byte stream to the server, depending on the quantity of data in it, can take up to 20 seconds (it's a big canvas!). 我需要在客户端执行此操作,因为根据其中的数据量将该字节流发送到服务器可能需要长达20秒(这是一个很大的画布!)。 Not to mention retrieving the image afterward... 更不用说之后检索图像了......

So, how do I do this? 那么,我该怎么做?

I had a big RaphaelJs canvas too and needed to allow the user to run a script that would save lots of canvas as png images. 我也有一个很大的RaphaelJs画布,需要允许用户运行一个脚本,将大量画布保存为png图像。 I tried to transform my raphael into an svn, then my svg to a png and then using wget but that of course didn't work because my canvas where generated by javascript and wget can't deal with that. 我试图将我的raphael转换为svn,然后将我的svg转换为png,然后使用wget,但这当然不起作用,因为我的画布由javascript和wget生成无法处理。 In the end I realised that I could just have my webapp to build a page with just the svg canvas and use phantomjs ( a headless browser) to save it as a png. 最后我意识到我可以让我的webapp只使用svg画布构建一个页面并使用phantomjs(一个无头浏览器)将其保存为png。 It works briliantly. 它工作得很好。

It is as simple as that. 它是如此简单。 1. Make your webapp to build a page with just the svg canvas. 1.使用web应用程序仅使用svg画布构建页面。 2. Create a svgToPgn.js file with the following code : 2.使用以下代码创建svgToPgn.js文件:

var page = require('webpage').create(); var page = require('webpage')。create(); page.open('URL_TO_YOUR_HTML_PAGE', function() { page.render('PATH_TO_PNG/example.png'); phantom.exit(); }); page.open('URL_TO_YOUR_HTML_PAGE',function(){page.render('PATH_TO_PNG / example.png'); phantom.exit();}); 3. Download Phantom ( http://phantomjs.org ), unzip it and in the bin directory you will find the phantomjs executable. 3.下载Phantom( http://phantomjs.org ),将其解压缩,然后在bin目录中找到phantomjs可执行文件。 Run : ./phantomjs svgToPgn.js Your png file will be saved in : PATH_TO_PNG/example.png 运行:./ elephantomjs svgToPgn.js您的png文件将保存在:PATH_TO_PNG / example.png

And I need to do this on the client-side 我需要在客户端执行此操作

Well that requirement kind of kills your chances. 那个要求有点杀了你的机会。 Sorry. 抱歉。

You can't do what you want to do, the best you've got is displaying the image and telling the user to right-click save-as. 你无法做你想做的事情,你所拥有的最好的就是显示图像并告诉用户右键单击另存为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM