【问题标题】:How can we convert html tables in to png using GD library in PHP?我们如何使用 PHP 中的 GD 库将 html 表格转换为 png?
【发布时间】:2019-04-30 10:39:39
【问题描述】:

我正在尝试创建一个棋盘并保存为 png 文件。我添加了创建棋盘并将图像存储到img 文件夹的功能。图像存储在文件夹中,但它是空白的,并且 html 代码显示在图像上。我使用了 GD 库。

我在这里包含了我的代码、php gd 库信息和图像。任何帮助将不胜感激。

<?php
$tableWidth = 150 . 'px';
$width      = 20 . 'px';
$height     = 20 . 'px';

$image = "<table width=".$tableWidth." style='margin-left: 200px;'>";
for($i=0; $i < 8; $i++){
    $image .= "<tr>";
    for($j=0; $j < 8; $j++){
        if($i % 2 == 0){
            if($j % 2 == 0){
                $image .= '<td style="background-color: pink; width: '.$width.'; height: '.$height.'"></td>';
            } else {
                $image .= '<td style="background-color: black; width: '.$width.'; height: '.$height.'"></td>';
            }
        } else {
            if($j % 2 == 0){
                $image .= '<td style="background-color: black; width: '.$width.'; height: '.$height.'"></td>';
            } else {
                $image .= '<td style="background-color: pink; width: '.$width.'; height: '.$height.'"></td>';
            }
        }
    }
    $image .= "</tr>";
}
$image .= "<table>";

$im               = @imagecreate(300, 600)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color       = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  $image, $text_color);
//imagettftext($img, 9, 0, 1, 1, $white, "VERDANA.TTF", $html_code);
header("Content-Type: image/png");
imagepng($im, 'img/chessboard.png');
?>

目前结果

PHP 信息

GD Support          enabled
GD Version          bundled (2.1.0 compatible)
FreeType Support    enabled
FreeType Linkage    with freetype
FreeType Version    2.9.1
GIF Read Support    enabled
GIF Create Support  enabled
JPEG Support        enabled
libJPEG Version     9 compatible
PNG Support         enabled
libPNG Version      1.6.34
WBMP Support        enabled
XPM Support         enabled
libXpm Version      30512
XBM Support         enabled
WebP Support        enabled

【问题讨论】:

  • 为此您需要一个 HTML 渲染器。我非常怀疑 GD 能做到这一点。
  • 以及页面顶部的这些行 ini_set('error_reporting', E_ALL); ini_set('display_errors', true);还有这个解决方案stackoverflow.com/questions/30935560/…

标签: php gd


【解决方案1】:

由于带有 CSS 的 HTML 表格是由 Web 浏览器“渲染”的,因此您不能简单地将其代码发送到 GD 库并期望图形输出。所做的只是打印示例中所示的字符串。

您可以做的是不使用 HTML 代码进行绘制,即作为具有填充颜色的几何形状的图像,这对于 GD 库来说是非常完美的方法。

让我们想一想:棋盘。棋盘是一个有八行八列的正方形,用浅色和深色两种基本颜色着色。 所以你只需要:

  1. 创建一个图像,例如 640 x 640 像素manual
  2. 位置或 8 * 8 个矩形,每个大小为宽度和高度的 1/8,80 x 80 像素manual
  3. 用颜色填充这些矩形manual
  4. 用一些线条、边框、阴影等来改进它。
  5. 渲染最终图像manual

互联网上的众多教程之一是here 或here。

编辑 这是一个第一列和第一行的示例,没有任何优化,只是概念证明:

<?php
  header('Content-type: image/png');

  $png_image = imagecreate(180, 180);
  $grey = imagecolorallocate($png_image, 229, 229, 229);
  $black = imagecolorallocate($png_image, 0, 0, 0);
  $white = imagecolorallocate($png_image, 255, 255, 255);

  imagefilltoborder($png_image, 0, 0, $grey, $grey);

  // first row
  imagefilledrectangle ($png_image, 10, 10, 30, 30, $black);
  imagefilledrectangle ($png_image, 30, 10, 50, 30, $white);
  imagefilledrectangle ($png_image, 50, 10, 70, 30, $black);
  imagefilledrectangle ($png_image, 70, 10, 90, 30, $white); 
  imagefilledrectangle ($png_image, 90, 10, 110, 30, $black); 
  imagefilledrectangle ($png_image, 110, 10, 130, 30, $white);
  imagefilledrectangle ($png_image, 130, 10, 150, 30, $black);      
  imagefilledrectangle ($png_image, 150, 10, 170, 30, $white);

  // first column
  imagefilledrectangle ($png_image, 10, 10, 30, 30, $black);
  imagefilledrectangle ($png_image, 10, 30, 30, 50, $white);
  imagefilledrectangle ($png_image, 10, 50, 30, 70, $black);
  imagefilledrectangle ($png_image, 10, 70, 30, 90, $white);
  imagefilledrectangle ($png_image, 10, 90, 30, 110, $black);
  imagefilledrectangle ($png_image, 10, 110, 30, 130, $white);  
  imagefilledrectangle ($png_image, 10, 130, 30, 150, $black);
  imagefilledrectangle ($png_image, 10, 150, 30, 170, $white);

  imagepng($png_image);
  imagedestroy($png_image);
  ?>

输出是:

需要更多解释 - 你在这里:

在上面的示例中,我使用 20x20 像素的正方形,因此每一行从 +20 像素开始,然后是前一列,每一列从 +20 像素开始,然后是前一列。

坐标轴命名为x水平从左到右计数,y垂直从上到下计数。

1 是正方形的左上角点,2 是正方形的右下角。

从这个角度来看,图像的最左上角的所有坐标都为零,即。 x1 = 0, y1 = 0, x2 = 0, y2 = 0。

现在是方块

第一行的第一个正方形从顶部和左侧填充了 10 个像素,因此左上角坐标为 x1 = 10(从左侧),y1 = 10(从顶部),并且正方形有 20 个像素的尺寸,所以右下角坐标是 x2 = x1 + 20 和 y2 = y2 + 20,即。 x2 = 30,y2 = 30。

第二行的第一个正方形从左边开始有 10 个像素的填充,并附在第一行正方形的下方,保持填充与以前相同,即。 x1 = 10 以保留填充(从左侧),但将起点从顶部 +20 像素移动,即。 y1 = 30(从顶部开始)。现在将右下角设置为 x1 和 y1 的坐标 +20 像素,即 x2 = 30 和 y2 = 50。 以此类推。

长话短说: 首先点正方形的左上角某处,这将设置 x1 和 y1 坐标。 其次将给定的像素添加到两个坐标以创建一个正方形并将其与给定的行号和列号相乘,即。如果是 20 像素,则 x2 = x1 + 20 * column_number,y2 = y2 + 20 * row_number。

参考PHP函数imagerectangle()

【讨论】:

  • 谢谢。但如何完全填满。我尝试了 for 循环,但没有工作。
  • 更新了原始答案,解释了如何计算坐标。
【解决方案2】:

不使用 PHP,最简单的方法是将表格呈现为 HTML,使其成为画布,然后使用 javascript 创建图像。

var canvas = document.getElementById("some-canvas-id");
var img = canvas.toDataURL("image/png");

【讨论】:

    猜你喜欢
    • 2011-08-10
    • 2015-04-06
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    相关资源
    最近更新 更多