"首先,需要使用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 "