extension=gd
如果没有找到该行,请手动添加并重启Web服务器。
<!DOCTYPE html>
<html>
<head>
<title>画板</title>
<style>
#canvas {
border: 1px solid #000;
}
</style>
</head>
<body>
<form method="post" action="create_canvas.php">
<label for="width">宽度:</label>
<input type="number" id="width" name="width" min="100" max="1000" required><br>
<label for="height">高度:</label>
<input type="number" id="height" name="height" min="100" max="1000" required><br>
<input type="submit" value="创建画板">
</form>
</body>
</html>
这个表单会发送POST请求到create_canvas.php,并提交用户输入的宽度和高度。
<?php
// 获取宽度和高度参数
$width = $_POST['width'];
$height = $_POST['height'];
// 创建一个空画布
$canvas = imagecreatetruecolor($width, $height);
?>
<?php
// 渲染画布
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
$color = imagecolorallocate($canvas, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($canvas, $x, $y, $color);
}
}
// 输出图像到浏览器
header('Content-Type: image/png');
imagepng($canvas);
imagedestroy($canvas);
?>
这段代码将会使用随机生成的RGB颜色填充画布,并通过imagepng函数输出PNG格式的图像。