PHP imitation GD generated BMP image
- Function Imagebmp (& $im, $filename = ", $bit = 8, $compression = 0)
- {
- if (!in_array ($bit, Array (1, 4, 8, 16, 24, 32)))
- {
- $bit = 8;
- }
- else if ($bit = = +)//todo:32 bit
- {
- $bit = 24;
- }
- $bits = POW (2, $bit);
- Adjust 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 ']). "The";
- }
- 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 per line must be a multiple of 4, padded.
- $extra = ";
- $padding = 4-ceil ($width/(8/$bit))% 4;
- if ($padding% 4! = 0)
- {
- $extra = Str_repeat ("n", $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 = "n";
- $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 per line must be a multiple of 4, padded.
- $extra = ";
- $padding = 4-($width * ($bit/8))% 4;
- if ($padding% 4! = 0)
- {
- $extra = Str_repeat ("n", $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", si + $size _quad + $size _data, 0, + $size _quad);
- Bitmap Information Header
- $info _header = Pack ("v3v2v*", 0x28, $width, $height, 1, $bit, $compression, $size _data, 0, 0, $colors _num, 0);
- Write file
- if ($filename! = ")
- {
- $fp = fopen ($filename, "WB");
- Fwrite ($fp, $file _header);
- Fwrite ($fp, $info _header);
- Fwrite ($fp, $rgb _quad);
- Fwrite ($fp, $bmp _data);
- Fclose ($FP);
- return true;
- }
- Browser output
- Header ("Content-type:image/bmp");
- Echo $file _header. $info _header;
- echo $rgb _quad;
- echo $bmp _data;
- return true;
- }
Copy Code |