[GD] generates an image (imagebmp) in bmp format ). There is no bmp image generation function in the GD library, so I wrote one myself. there is still a compression algorithm not written, but it is enough. For more information, see. Intimage
There is no bmp image generation function in the GD library, so I wrote one myself. there is still a compression algorithm not written, but it is enough. For more information, see.
Int imagebmp (resource image [, string filename [, int $ bit [, int compression])
$ Im: Image resources
$ Filename: If you want to save it as a file, specify the file name. if it is null, it is output directly in the browser.
$ Bit: image quality (1, 4, 8, 16, 24, 32 bits)
$ Compression: compression method. 0 indicates no compression, and 1 uses RLE8 compression algorithm for compression.
Note: This function still needs the support of the GD Library.
Demo:
$ Im = imagecreatefrompng ("test.png ");
Imagebmp ($ im );
Imagedestroy ($ im );
Source:
/**
* Create a bmp image
*
* @ Author: legend (legendsky@hotmail.com)
* @ Link: http://www.ugia.cn /? P = 96
* @ Description: create Bitmap-File with GD library
* @ Version: 0.1
*
* @ Param resource $ im image resource
* @ Param string $ filename if you want to save it as a file, specify the file name. if it is null, the file is output directly in the browser.
* @ Param integer $ bit image quality (1, 4, 8, 16, 24, 32 bits)
* @ Param integer $ compression: compression mode. 0 indicates no compression, and 1 indicates RLE8 compression algorithm.
*
* @ Return integer
*/
Function imagebmp (& $ im, $ filename =, $ bit = 8, $ compression = 0)
{
If (! In_array ($ bit, array (1, 4, 8, 16, 24, 32 )))
{
$ Bit = 8;
}
Else if ($ bit = 32) // todo: 32 bit
{
$ Bit = 24;
}
$ Bits = pow (2, $ bit );
// Adjust the color palette
Imagetruecolortopalette ($ im, true, $ bits );
$ Width = imagesx ($ im );
$ Height = imagesy ($ im );
$ Colors_num = imagecolorstotal ($ im );
If ($ bit <= 8)
{
// Color index
$ Rgb_quad =;
For ($ I = 0; $ I <$ colors_num; $ I ++)
{
$ Colors = imagecolorsforindex ($ im, $ I );
$ Rgb_quad. = chr ($ colors [blue]). chr ($ colors [green]). chr ($ colors [red]). "";
}
// Bitmap data
$ BMP _data =;
// Non-compressed
If ($ compression = 0 | $ bit <8)
{
If (! In_array ($ bit, array (1, 4, 8 )))
{
$ Bit = 8;
}
$ Compression = 0;
// The number of bytes in each row must be a multiple of 4.
$ Extra =;
$ Padding = 4-ceil ($ width/(8/$ bit) % 4;
If ($ padding % 4! = 0)
{
$ Extra = str_repeat ("", $ padding );
}
For ($ j = $ height-1; $ j> = 0; $ j --)
{
$ I = 0;
While ($ I <$ width)
{
$ Bin = 0;
$ Limit = $ width-$ I <8/$ bit? (8/$ bit-$ width + $ I) * $ bit: 0;
For ($ k = 8-$ bit; $ k >=$ limit; $ k-= $ bit)
{
$ Index = imagecolorat ($ im, $ I, $ j );
$ Bin | = $ index <$ k;
$ I ++;
}
$ BMP _data. = chr ($ bin );
}
$ BMP _data. = $ extra;
}
}
// RLE8 compression
Else if ($ compression = 1 & $ bit = 8)
{
For ($ j = $ height-1; $ j> = 0; $ j --)
{
$ Last_index = "";
$ Same_num = 0;
For ($ I = 0; $ I <= $ width; $ I ++)
{
$ Index = imagecolorat ($ im, $ I, $ j );
If ($ index! ==$ Last_index | $ same_num> 255)
& Nb
Bytes. For more information, see. Int image...