ホームページ  >  記事  >  バックエンド開発  >  PHP は GD を模倣して BMP 画像を生成します

PHP は GD を模倣して BMP 画像を生成します

WBOY
WBOYオリジナル
2016-07-25 09:02:101138ブラウズ
PHPからGD生成BMP画像
  1. function imagebmp(&$im, $filename = '', $bit = 8, $compression = 0)
  2. {
  3. if (!in_array($bit, array(1, 4, 8, 16, 24, 32)))
  4. {
  5. $bit = 8;
  6. }
  7. else if ($bit == 32) // todo:32 bit
  8. {
  9. $bit = 24;
  10. }
  11. $bits = pow(2, $bit );
  12. // 调整调色板
  13. imagetruecolortopalette($im, true, $bits);
  14. $width = imagex($im);
  15. $height = imagey($im);
  16. $colors_num = imagecolorstotal($im) );
  17. if ($bit {
  18. // 颜色インデックス
  19. $rgb_quad = '';
  20. for ($i = 0; $i {
  21. $colors = imagecolorsforindex($im, $i);
  22. $rgb_quad .= chr($colors['blue']) 。 chr($colors['green']) 。 chr($colors['red']) 。 " ";
  23. }
  24. // 位图データ据
  25. $bmp_data = '';
  26. // 非压缩
  27. if ($compression == 0 || $bit < 8)
  28. {
  29. if (!in_array($ bit, array(1, 4, 8)))
  30. {
  31. $bit = 8;
  32. }
  33. $compression = 0;
  34. // 実行文字数は 4 の倍数である必要があります。
  35. $extra = '';
  36. $padding = 4 - ceil($width / (8 / $bit)) % 4;
  37. if ($padding % 4 != 0)
  38. {
  39. $extra = str_repeat(" ", $padding);
  40. }
  41. for ($j = $height - 1; $j >= 0; $j --)
  42. {
  43. $i = 0;
  44. while ($i < $width)
  45. {
  46. $bin = 0;
  47. $limit = $width - $i <; 8 / $ビット ? (8 / $bit - $width + $i) * $bit : 0;
  48. for ($k = 8 - $bit; $k >= $limit; $k -= $bit)
  49. {
  50. $index = imagecolorat($im, $i, $j);
  51. $bin |= $index << $k;
  52. $i ++;
  53. }
  54. $bmp_data .= chr($bin);
  55. }
  56. $bmp_data .= $extra;
  57. }
  58. }
  59. // RLE8 压缩
  60. else if ($compression = = 1 && $bit == 8)
  61. {
  62. for ($j = $height - 1; $j >= 0; $j --)
  63. {
  64. $last_index = " ";
  65. $same_num = 0;
  66. for ($i = 0; $i {
  67. $index = imagecolorat($im, $i, $j);
  68. if ($index !== $last_index || $same_num > 255)
  69. {
  70. if ($same_num != 0)
  71. {
  72. $bmp_data .= chr($same_num) . chr($last_index);
  73. }
  74. $last_index = $index;
  75. $same_num = 1;
  76. }
  77. else
  78. {
  79. $same_num ++;
  80. }
  81. }
  82. $bmp_data .= " ";
  83. }
  84. $bmp_data .= " 1";
  85. }
  86. $size_quad = strlen($rgb_quad);
  87. $size_data = strlen($bmp_data);
  88. }
  89. else
  90. {
  91. // 実行文字数は 4 の倍数である必要があります、补齐。
  92. $extra = '';
  93. $padding = 4 - ($width * ($bit / 8)) % 4;
  94. if ($padding % 4 != 0)
  95. {
  96. $extra = str_repeat( " ", $padding);
  97. }
  98. // 位图データ据
  99. $bmp_data = '';
  100. for ($j = $height - 1; $j >= 0; $j --)
  101. {
  102. for ( $i = 0; $i < $i ++)
  103. {
  104. $index = imagecolorat($im, $i, $j);
  105. $colors = imagecolorsforindex($im, $index); ($bit == 16)
  106. {
  107. $bin = 0 << $bit;
  108. $bin |= ($colors['red'] >> 3) << 10;
  109. $bin |= ($colors['green'] >> 3) << 5;
  110. $bin |= $colors['blue'] >> 3;
  111. $bmp_data .= Pack("v", $bin);
  112. }
  113. else
  114. {
  115. $bmp_data .= Pack("c*", $colors['blue'], $colors['green' ], $colors['red']);
  116. }
  117. // todo: 32bit;
  118. }
  119. $bmp_data .= $extra;
  120. }
  121. $size_quad = 0;
  122. $size_data = strlen($bmp_data);
  123. $colors_num = 0;
  124. }
  125. // 位图文件头
  126. $file_header = "BM" . Pack("V3", 54 + $size_quad + $size_data, 0, 54 + $size_quad);
  127. // 位图情報头
  128. $info_header = Pack("V3v2V*", 0x28, $width, $height, 1 , $bit, $compression, $size_data, 0, 0, $colors_num, 0);
  129. // 書き込み文章
  130. if ($filename != '')
  131. {
  132. $fp = fopen($filename, "wb ");
  133. fwrite($fp, $file_header);
  134. fwrite($fp, $info_header);
  135. fwrite($fp, $rgb_quad);
  136. fwrite($fp, $bmp_data);
  137. fclose($fp);
  138. return true;
  139. }
  140. // 浏览器出力
  141. header("Content-Type: image/bmp");
  142. echo $file_header . $info_header;
  143. echo $rgb_quad;
  144. echo $bmp_data;
  145. return true;
  146. }
复制代

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。