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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBオリジナル
2016-07-25 09:07:182533ブラウズ
  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 $i ++;
  64. }
  65. $bmp_data .= chr($bin);

  66. }
  67. $bmp_data .= $extra;

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

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

  94. }
  95. $bmp_data .= “ 1″;

  96. }
  97. $size_quad = strlen( $rgb_quad);

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

  110. $bmp_data = ”;
  111. for ($j = $height – 1; $j >= 0; $j –)

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

  117. {
  118. $bin = 0
  119. $bin |= ($colors['red'] >> 3) $bin |= ($colors['green'] >> 3) $bin |= $colors['blue'] >> 3;

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

  121. }
  122. else
  123. {
  124. $bmp_data .= Pack(“c*”, $colors['blue' ], $colors['green'], $colors['red']);
  125. }
  126. // todo: 32bit;

  127. }
  128. $ bmp_data .= $extra;

  129. }
  130. $size_quad = 0;

  131. $size_data = strlen($bmp_data);
  132. $colors_num = 0;
  133. }
  134. $file_header = “BM” 。 Pack(“V3”, 54 + $size_quad + $size_data, 0, 54 + $size_quad);

  135. // 位图情報头

  136. $info_header = Pack(“V3v2V*”, 0 ×28, $width, $height, 1, $bit, $compression, $size_data, 0, 0, $colors_num, 0);
  137. // 書き込み文章

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

  142. fwrite($fp, $info_header );
  143. fwrite($fp, $rgb_quad);
  144. fwrite($fp, $bmp_data);
  145. fclose($fp);
  146. return 1;

  147. }
  148. // 浏览器出力

  149. header(“Content-Type: image/bmp”);
  150. echo $file_header . $info_header;
  151. echo $rgb_quad;
  152. echo $bmp_data;
  153. return 1;

  154. }
  155. ?>
复制發


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