PHP uses GD library to generate images in BMP format (imagebmp)

Source: Internet
Author: User
/ **

* Create bmp format picture
*
* @author: legend
* @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 directly output in the browser
* @param integer $ bit image quality (1, 4, 8, 16, 24, 32 bits)
* @param integer $ compression compression method, 0 is not compressed, 1 is compressed using 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 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']). "\ 0 ″;
}
// bitmap data

$ bmp_data = ”;
// uncompressed

if ($ compression == 0 || $ bit <8)
{
if (! in_array ($ bit, array (1, 4, 8)))
{
$ bit = 8;
}
$ compression = 0;

// The number of bytes in each line must be a multiple of 4, which is complemented.

$ extra = ”;
$ padding = 4 – ceil ($ width / (8 / $ bit))% 4;
if ($ padding% 4! = 0)
{
$ extra = str_repeat (“\ 0 ″, $ 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 = "\ 0 ″;
$ same_num = 0;
for ($ i = 0; $ i <= $ width; $ i ++)
{
$ index = imagecolorat ($ im, $ i, $ j);
if ($ index! == $ last_index || $ same_num> 255)
{
if ($ same_num! = 0)
{
$ bmp_data. = chr ($ same_num). chr ($ last_index);
}
$ last_index = $ index;

$ same_num = 1;
}
else
{
$ same_num ++;
}
}
$ bmp_data. = "\ 0 \ 0 ″;

}
$ bmp_data. = "\ 0 \ 1 ″;

}
$ size_quad = strlen ($ rgb_quad);

$ size_data = strlen ($ bmp_data);
}
else
{
// The number of bytes in each line must be a multiple of 4, which is complemented.
$ extra = ”;
$ padding = 4 – ($ width * ($ bit / 8))% 4;
if ($ padding% 4! = 0)
{
$ extra = str_repeat (“\ 0 ″, $ padding);
}
// bitmap data

$ bmp_data = ”;
for ($ j = $ height – 1; $ j> = 0; $ j-)

{
for ($ i = 0; $ i <$ width; $ i ++)
{
$ index = imagecolorat ($ im, $ i, $ j);
$ colors = imagecolorsforindex ($ im, $ index);
if ($ bit == 16)

{
$ bin = 0 << $ bit;
$ bin | = ($ colors ['red'] >> 3) << 10;

$ bin | = ($ colors ['green'] >> 3) << 5;
$ bin | = $ colors ['blue'] >> 3;
$ bmp_data. = pack (“v”, $ bin);

}
else
{
$ bmp_data. = pack (“c *”, $ colors ['blue'], $ colors ['green'], $ colors ['red']);
}
// todo: 32bit;

}
$ bmp_data. = $ extra;

}
$ size_quad = 0;

$ size_data = strlen ($ bmp_data);
$ colors_num = 0;
}
// bitmap file header

$ file_header = “BM”. pack (“V3 ″, 54 + $ size_quad + $ size_data, 0, 54 + $ size_quad);
// bitmap information header

$ info_header = pack (“V3v2V *”, 0 × 28, $ width, $ height, 1, $ bit, $ compression, $ size_data, 0, 0, $ colors_num, 0);
// write to file

if ($ filename! = ”)
{
$ fp = fopen (“test.bmp”, “wb”);
fwrite ($ fp, $ file_header);

fwrite ($ fp, $ info_header);
fwrite ($ fp, $ rgb_quad);
fwrite ($ fp, $ bmp_data);
fclose ($ fp);
return 1;

}
// browser output

header ("Content-Type: image / bmp");
echo $ file_header. $ info_header;
echo $ rgb_quad;
echo $ bmp_data;
return 1;

}
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.