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格式的圖像。