問題描述
我正在構建一個網站,該網站從表單中獲取數據(人名)并將其發布到下一頁表格中的自定義注釋"中.我希望用戶能夠將他們的筆記"保存為帶有自定義名稱的圖像.有沒有辦法將特定的 HTML 表格另存為 PNG?也許對網頁上的特定坐標進行屏幕截圖?
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?
如果沒有,有沒有辦法從 HTML 畫布中的表單呈現已發布的 PHP 內容?
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.
推薦答案
雖然您可以使用各種工具呈現 HTML,但您不能確定這些工具是否會以與瀏覽器呈現的方式相同的方式呈現 HTML.
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.
看看 PHP 的 imagettftext() 函數.它包含一個 Example #1,它從可以存儲在任何變量中的文本創建一個小的、簡單的圖像......包括表單變量.
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.
通過使用此示例,并添加其他一些PHP 的 GD 函數,你可以制作一個像樣的表格副本,并確保它看起來完全符合你的要求,而不是 html2ps 或其他一些工具呈現它的方式.
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:
請注意,您需要根據以上鏈接中的說明提供 arial.ttf
.
Note that you'll need to provide arial.ttf
per the instructions at the link above.
如果一切都不起作用,請先在屏幕上和您的網絡服務器的錯誤日志中查找錯誤,然后再在這里跟進.您的 Web 服務器上可能沒有安裝 PHP 的 GD 模塊.如果是這種情況,您應該咨詢您的服務器管理員,以確保您可以使用所需的內容.
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 表格另存為圖像的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!