简体   繁体   English

将渲染的HTML保存为图像

[英]Save rendered HTML as image

I am building a web app that needs to export a div to an image. 我正在构建一个需要将div导出到图像的Web应用程序。 That div will contain images, other divs, text with css styling, etc. At the end, the user should have an image that would look the same as if he had taken a screenshot of that div. 该div将包含图像,其他div,具有css样式的文本等。最后,用户应具有一张看起来与该div的屏幕快照相同的图像。 I 've looked into server-side php libs but I don't see anything that would handle the complexity of the rendered HTML. 我已经研究了服务器端php库,但没有发现任何能够处理呈现的HTML的复杂性的东西。 HTML5 canvas has that capability but I can't use a canvas for my case. HTML5画布具有该功能,但是我不能在我的情况下使用画布。 Any ideas? 有任何想法吗?

Thanks! 谢谢!

Check out html2canvas . 查看html2canvas A javascript framework that renders the page content on a canvas element. 一个在框架元素上呈现页面内容的JavaScript框架。

I've written a project that converts HTML with CSS styles to canvas. 我编写了一个项目,该项目将具有CSS样式的HTML转换为画布。

It is available here: https://github.com/coolbloke1324/html-to-canvas 它在这里可用: https//github.com/coolbloke1324/html-to-canvas

And an example test page is available here: http://www.isogenicengine.com/html-canvas/test/index.html 此处提供了示例测试页面: http : //www.isogenicengine.com/html-canvas/test/index.html

On the test page, the initial load will just show a square div with some other divs inside it with various stylings like backgrounds and borders including border radius etc. You'll see a button on the top-right of the page marked "Render". 在测试页面上,初始加载将仅显示一个方形div,其中包含其他div,并带有各种样式,例如背景和边框,包括边框半径等。您会在页面右上方看到一个标记为“渲染”的按钮。 Click that and a canvas will be created and then the DOM will be parsed and rendered to the canvas. 单击该按钮,将创建一个画布,然后将DOM解析并呈现到画布。 I intentionally changed the background colour of the canvas to black so you can see the difference. 我特意将画布的背景颜色更改为黑色,以便您可以看到其中的区别。

You could wrap the div in a canvas tag, access the pixel data directly, send that to a PHP script and use the data to construct an image. 您可以将div包装在canvas标记中,直接访问像素数据,将其发送到PHP脚本并使用该数据构造图像。

Here's how you get the pixel data https://developer.mozilla.org/en/html/canvas/pixel_manipulation_with_canvas 这是获取像素数据的方法https://developer.mozilla.org/en/html/canvas/pixel_manipulation_with_canvas

And here's what you would use to create an image pixel by pixel http://php.net/manual/en/function.imagesetpixel.php 这就是您用来按像素创建图像的方法http://php.net/manual/en/function.imagesetpixel.php

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

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