【发布时间】: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/…