There is no function to generate bmp images in the GD library, so I wrote one myself. This function also has a compression algorithm that I haven't written yet, but it is enough. Students who need it can take a look.
int imagebmp (resource image [, string filename [, int $bit [, int compression]]] )
$im: image resource
$filename: If you want to save as a file, please specify the file name. If it is empty, it will be output directly in the browser.
$bit: image quality (1, 4, 8, 16, 24, 32 bits)
$compression: compression method, 0 means no compression, 1 uses RLE8 compression algorithm for compression.
Note: This function still requires the support of the GD library.
Demo:
$im = imagecreatefrompng("test.png");
imagebmp($im);
imagedestroy($im);
Source:
/**
* Create bmp format pictures
*
* @author: legend( [email protected] )
* @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 as a file, please specify the file name. If it is empty, it will be output directly in the browser.
* @param integer $bit image quality (1, 4, 8, 16, 24, 32 bits)
* @param integer $compression compression method, 0 means no compression, 1 uses RLE8 compression algorithm for compression
*
* @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 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']) . "