要使用 PHP 生成图片,您可以使用 GD 库或 Imagick 库。这些库可以帮助您创建图像、操作图像并将其保存为文件或输出到浏览器。
下面是一个使用 GD 库生成图像的示例:
<?php // 创建一个 200x200 像素的图像 $image = imagecreatetruecolor(200, 200); // 分配颜色 $color = imagecolorallocate($image, 255, 255, 255); // 画一个矩形 imagefilledrectangle($image, 50, 50, 150, 150, $color); // 将图像输出到浏览器 header('Content-Type: image/png'); imagepng($image); // 保存图像到文件 imagepng($image, 'output.png'); // 释放内存 imagedestroy($image); ?>