ホームページ  >  記事  >  バックエンド開発  >  PHP は GD ライブラリを使用して bmp 形式の画像を生成します (imagebmp)

PHP は GD ライブラリを使用して bmp 形式の画像を生成します (imagebmp)

WBOY
WBOYオリジナル
2016-07-25 09:07:182372ブラウズ
  1. /**

  2. * bmp形式の画像を作成します
  3. *
  4. * @author: legend
  5. * @description: GDライブラリでビットマップファイルを作成します
  6. * @version: 0.1
  7. *
  8. * @param resource $im image resource
  9. * @param string $filenameファイルとして保存したい場合はファイル名を指定してください。空の場合はブラウザに直接出力されます
  10. * @param integer $bit画質(1, 4, 8, 16, 24, 32ビット) )
  11. * @param integer $compression 圧縮モード、0 は圧縮なしを意味し、1 は圧縮に RLE8 圧縮アルゴリズムを使用します
  12. *
  13. * @return integer
  14. */
  15. function imagebmp(&$im, $filename = ”, $bit = 8, $compression = 0)
  16. {
  17. if (!in_array($bit, array(1, 4, 8, 16, 24, 32)))
  18. {
  19. $bit = 8;
  20. }
  21. else if ($bit == 32) // todo:32 ビット
  22. {
  23. $bit = 24;
  24. }

  25. $bits = pow(2, $bit);

  26. // 调整调色板

  27. imagetruecolortopalette($ im, true, $bits);
  28. $width = imagex($im);
  29. $height = imagey($im);
  30. $colors_num = imagecolorstotal($im);

  31. if ( $bit {

  32. // 颜色インデックス
  33. $rgb_quad = ”;
  34. for ($i = 0; $i {
  35. $colors = imagecolorsforindex($im) , $i);
  36. $rgb_quad .= chr($colors['blue']) 。 chr($colors['green']) 。 chr($colors['red']) 。 “ ″;
  37. }

  38. // 位图データ架

  39. $bmp_data = ”;

  40. // 非压缩

  41. if ($compression == 0 || $bit {
  42. if (!in_array($bit, array(1, 4, 8)))
  43. {
  44. $bit = 8;
  45. }

  46. $compression = 0;

  47. // 実行文字数は 4 の倍数である必要があります。

  48. $extra = ”;
  49. $padding = 4 – ceil($width / (8 / $bit )) % 4;
  50. if ($padding % 4 != 0)
  51. {
  52. $extra = str_repeat(" '', $padding);
  53. }

  54. for ($j = $height – 1; $j >= 0; $j –)

  55. {
  56. $i = 0;
  57. while ($i {
  58. $bin = 0;
  59. $limit = $width – $i
  60. for ($k = 8 – $bit; $k >= $limit; $k -= $bit)

  61. {
  62. $index = imagecolorat($im, $i, $j);
  63. $bin |= $index << $k;
  64. $i ++;
  65. }

  66. $bmp_data .= chr($bin);

  67. }

  68. $bmp_data .= $extra;

  69. }
  70. }
  71. // RLE8 压缩
  72. else if ($compression == 1 && $bit == 8)
  73. {
  74. for ($j = $height – 1; $j >= 0; $j –)
  75. {
  76. $last_index = “ ″;
  77. $same_num = 0;
  78. for ($i = 0; $i {
  79. $index = imagecolorat($im, $i, $ j);
  80. if ($index !== $last_index || $same_num > 255)
  81. {
  82. if ($same_num != 0)
  83. {
  84. $bmp_data .= chr($same_num) . chr($last_index);
  85. }

  86. $last_index = $index;

  87. $same_num = 1;
  88. }
  89. else
  90. {
  91. $same_num ++;
  92. }
  93. }
  94. $bmp_data .= “ ″;

  95. }

  96. $bmp_data .= “ 1″;

  97. }

  98. $size_quad = strlen( $rgb_quad);

  99. $size_data = strlen($bmp_data);
  100. }
  101. else
  102. {
  103. // 実行文字数は 4 の倍数でなければなりません。
  104. $extra = ”;
  105. $padding = 4 – ($width * ($bit / 8)) % 4;
  106. if ($padding % 4 != 0)
  107. {
  108. $extra = str_repeat(“ ”, $padding );
  109. }

  110. // 位图データ

  111. $bmp_data = ”;

  112. for ($j = $height – 1; $j >= 0; $j –)

  113. {
  114. for ($i = 0; $i {
  115. $index = imagecolorat($im, $i, $j);
  116. $colors = imagecolorsforindex($im, $index);

  117. if ($bit == 16)

  118. {
  119. $bin = 0 << $bit;

  120. $bin |= ($colors['red'] >> 3) << 10;

  121. $bin |= ($colors['green'] >> 3) << 5;
  122. $bin |= $colors['blue'] >> 3;

  123. $bmp_data .= Pack(“v”, $bin);

  124. }
  125. else
  126. {
  127. $bmp_data .= Pack(“c*”, $colors['blue' ], $colors['green'], $colors['red']);
  128. }

  129. // todo: 32bit;

  130. }

  131. $ bmp_data .= $extra;

  132. }

  133. $size_quad = 0;

  134. $size_data = strlen($bmp_data);
  135. $colors_num = 0;
  136. }

  137. $file_header = “BM” 。 Pack(“V3”, 54 + $size_quad + $size_data, 0, 54 + $size_quad);

  138. // 位图情報头

  139. $info_header = Pack(“V3v2V*”, 0 ×28, $width, $height, 1, $bit, $compression, $size_data, 0, 0, $colors_num, 0);

  140. // 書き込み文章

  141. if ($filename != ”)
  142. {
  143. $fp = fopen(“test.bmp”, “wb”);

  144. fwrite($fp, $file_header);

  145. fwrite($fp, $info_header );
  146. fwrite($fp, $rgb_quad);
  147. fwrite($fp, $bmp_data);
  148. fclose($fp);

  149. return 1;

  150. }

  151. // 浏览器出力

  152. header(“Content-Type: image/bmp”);
  153. echo $file_header . $info_header;
  154. echo $rgb_quad;
  155. echo $bmp_data;

  156. return 1;

  157. }
  158. ?>

复制發


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