首先,需要使用 PHP 的 GD 库创建一个图像资源。XBM 图像只能是黑白的,所以可以用 imagecreatetruecolor() 创建图像,再把它转换成黑白。
"; // 创建一个 100x100 像素的图像 $width = 100; $height = 100; $image = imagecreatetruecolor($width, $height); // 填充白色背景 $white = imagecolorallocate($image, 255, 255, 255); imagefilledrectangle($image, 0, 0, $width, $height, $white); // 绘制一些黑色图案 $black = imagecolorallocate($image, 0, 0, 0); imageline($image, 10, 10, 90, 90, $black); imagerectangle($image, 20, 20, 80, 80, $black); echo "使用 imagexbm() 函数可以将 GD 图像资源保存为 XBM 格式,函数语法如下:
"; echo "bool imagexbm ( resource \$image , string \$filename [, int \$foreground = 0 ] )"; echo "
其中 \$foreground 是黑色像素的颜色索引,默认为 0。
"; // 保存为 XBM 文件 $filename = 'example.xbm'; imagexbm($image, $filename); // 释放图像资源 imagedestroy($image); echo "XBM 图像已生成:$filename
"; echo "XBM 文件通常用作网页图标或 CSS 中的背景图:
"; echo "<img src='example.xbm' alt='XBM 图像'>"; echo "
使用 PHP 的 imagexbm() 函数可以快速将黑白 GD 图像保存为 XBM 格式,适合嵌入式系统、老式网页图标等场景。步骤包括:
"; echo "