将HTML表格保存为图像 [英] Save HTML table as an image

查看:125
本文介绍了将HTML表格保存为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个网站,它从表单中提取数据(人的姓名),并将其张贴到下一页表格中的自定义注释中。我希望用户能够将他们的笔记保存为一张带有自定义名称的图像。有没有办法将一个SPECIFIC HTML TABLE保存为PNG?也许截取一个网页上的特定坐标的截图?



如果没有,是否有方法在HTML画布中呈现张贴的PHP内容?



任何帮助都很棒。

解决方案

虽然您可以使用各种工具呈现HTML,但您可以如果您的最终目标是 生成一张图片,那么您不能确定这些工具的HTML呈现方式与您的浏览器呈现该图片的方式相同。 ,然后让我们解决这个问题,而不是试图让你建议的解决方案。



看看PHP的 imagettftext()函数。它包含一个 Example#1 ,它可以从文本中创建一个小而简单的图像,可以存储在任何变量中......包括一个表单变量。



通过使用这个例子,并添加一些 PHP的GD函数

a>,你可以制作一张正确的表格副本,并确保它看起来完全符合你的需要,而不是html2ps或其他工具呈现它的方式。

 <?php 
//设置内容类型
头('Content-Type:image / png');

//创建图片
$ im = imagecreatetruecolor(400,30);

//创建一些颜色
$ white = imagecolorallocate($ im,255,255,255);
$ gray = imagecolorallocate($ im,128,128,128);
$ black = imagecolorallocate($ im,0,0,0);
imagefilledrectangle($ im,0,0,399,29,$ white);

//要绘制的文本
$ text = isset($ _ POST ['name'])? $ _POST ['name']:name;
//用你自己的字体路径替换路径
$ font ='arial.ttf';

//为文本添加一些阴影
imagettftext($ im,20,0,11,21,$ gray,$ font,$ text);

//添加文本
imagettftext($ im,20,0,10,20,$ black,$ font,$ text);

//与imagejpeg()相比,使用imagepng()可以得到更清晰的文本
imagepng($ im);
imagedestroy($ im);
?>

以下是上述脚本生成的示例输出:





请注意,您需要按照以上链接中的说明提供 arial.ttf



如果事情不起作用,请在继续操作之前在屏幕上和Web服务器的错误日志FIRST中查找错误。您的Web服务器上可能没有安装PHP的GD模块。如果是这种情况,您应该与您的服务器管理员联系,确定您需要的是否可用。


I have a website I am building that takes data (the person's name) from a form and posts it into a customized "note" in a table on the next page. I want the user to then be able to save their "note" as an image with their customized name on it. Is there a way to save a SPECIFIC HTML TABLE as a PNG? Maybe take a screenshot of a specific coordinate on a web page?

If not is there a way to render posted PHP content from the form in an HTML canvas?

Any help would be great. I'm in a little over my head right now.

解决方案

While you can render HTML using a variety of tools, you can't be sure that those tools will render the HTML the same way your browser renders it.

If your end goal is to generate an image with text on it, then let's solve that problem instead of trying to make the solution you suggested work.

Have a look at PHP's imagettftext() function. It contains an Example #1 which creates a small, simple image from text that can be stored in any variable ... including a form variable.

By using this example, and adding a few other of PHP's GD functions, you can make a decent replica of a table, and make sure it looks exactly the way you want it, rather than the way html2ps or some other tool renders it.

<?php
// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = isset($_POST['name']) ? $_POST['name'] : "name";
// Replace path by your own font path
$font = 'arial.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

Here's sample output generated by the above script:

Note that you'll need to provide arial.ttf per the instructions at the link above.

If things don't work, look for errors both on-screen and in your web server's error log FIRST, before following up here. It's possible that PHP's GD module is not installed on your web server. If this is the case, you should check with your server administrator to make sure what you need is available to you.

这篇关于将HTML表格保存为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆