OSWikiHK: 請協力 GPLv3 的中文翻譯工作。
Generate transparent gif in PHP
OSWikiHK,自由中文開源知識庫
先用 imageCreate() 來建立一個GD圖像物件:
$im = imageCreate($width, $height);
分配一個顏色作為透明色:
$backgroundColor = imageColorAllocate($im, 255, 255, 255);
用那顏色塗滿整個圖像:
imageFilledRectangle($im, 0, 0, $width - 1 , $height - 1, $backgroundColor);
在圖像上畫些東西:
$sansFont = '/usr/share/fonts/truetype/ttf-bitstream-vera/VeraBd.ttf'; $textColor = imageColorAllocate($im, 0, 102, 255); imageTTFText($im, $fontsize, 0, 4, 14, $textColor, $sansFont, 'Hello World!');
把那顏色設定為透明色:
imageColorTransparent($im, $backgroundColor);
使用交錯模式:
imageInterlace($im);
傳出成 GIF:
header("Content-type: image/gif");
imageGif($im);
別忘了釋放圖像佔用的記憶體:
imageDestroy($im);

