PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/extensions/wideImage/vendor/de77/BMP.php

https://bitbucket.org/ddonthula/zurmoldap
PHP | 264 lines | 197 code | 24 blank | 43 comment | 21 complexity | ee350913773656dcd803710db5271a68 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * @author Gasper Kozak
  4. * @copyright 2007-2011
  5. This file is part of WideImage.
  6. WideImage is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation; either version 2.1 of the License, or
  9. (at your option) any later version.
  10. WideImage is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with WideImage; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. * @package Internal/Mappers
  18. **/
  19. /**
  20. * External code for BMP
  21. *
  22. * Adapted for use in WideImage. Code used with permission from the original author de77.
  23. * http://de77.com/php/read-and-write-bmp-in-php-imagecreatefrombmp-imagebmp
  24. *
  25. * @author de77
  26. * @license MIT
  27. * @url de77.com
  28. * @version 21.08.2010
  29. *
  30. * @package Internal/Mappers
  31. */
  32. class WideImage_vendor_de77_BMP
  33. {
  34. public static function imagebmp(&$img, $filename = false)
  35. {
  36. $wid = imagesx($img);
  37. $hei = imagesy($img);
  38. $wid_pad = str_pad('', $wid % 4, "\0");
  39. $size = 54 + ($wid + $wid_pad) * $hei * 3; //fixed
  40. //prepare & save header
  41. $header['identifier'] = 'BM';
  42. $header['file_size'] = self::dword($size);
  43. $header['reserved'] = self::dword(0);
  44. $header['bitmap_data'] = self::dword(54);
  45. $header['header_size'] = self::dword(40);
  46. $header['width'] = self::dword($wid);
  47. $header['height'] = self::dword($hei);
  48. $header['planes'] = self::word(1);
  49. $header['bits_per_pixel'] = self::word(24);
  50. $header['compression'] = self::dword(0);
  51. $header['data_size'] = self::dword(0);
  52. $header['h_resolution'] = self::dword(0);
  53. $header['v_resolution'] = self::dword(0);
  54. $header['colors'] = self::dword(0);
  55. $header['important_colors'] = self::dword(0);
  56. if ($filename)
  57. {
  58. $f = fopen($filename, "wb");
  59. foreach ($header AS $h)
  60. {
  61. fwrite($f, $h);
  62. }
  63. //save pixels
  64. for ($y=$hei-1; $y>=0; $y--)
  65. {
  66. for ($x=0; $x<$wid; $x++)
  67. {
  68. $rgb = imagecolorat($img, $x, $y);
  69. fwrite($f, self::byte3($rgb));
  70. }
  71. fwrite($f, $wid_pad);
  72. }
  73. fclose($f);
  74. }
  75. else
  76. {
  77. foreach ($header AS $h)
  78. {
  79. echo $h;
  80. }
  81. //save pixels
  82. for ($y=$hei-1; $y>=0; $y--)
  83. {
  84. for ($x=0; $x<$wid; $x++)
  85. {
  86. $rgb = imagecolorat($img, $x, $y);
  87. echo self::byte3($rgb);
  88. }
  89. echo $wid_pad;
  90. }
  91. }
  92. return true;
  93. }
  94. public static function imagecreatefromstring($data)
  95. {
  96. //read header
  97. $pos = 0;
  98. $header = substr($data, 0, 54);
  99. $pos = 54;
  100. if (strlen($header) < 54)
  101. return false;
  102. $header = unpack( 'c2identifier/Vfile_size/Vreserved/Vbitmap_data/Vheader_size/' .
  103. 'Vwidth/Vheight/vplanes/vbits_per_pixel/Vcompression/Vdata_size/'.
  104. 'Vh_resolution/Vv_resolution/Vcolors/Vimportant_colors', $header);
  105. if ($header['identifier1'] != 66 or $header['identifier2'] != 77)
  106. {
  107. return false;
  108. //die('Not a valid bmp file');
  109. }
  110. if (!in_array($header['bits_per_pixel'], array(24, 32, 8, 4, 1)))
  111. {
  112. return false;
  113. //die('Only 1, 4, 8, 24 and 32 bit BMP images are supported');
  114. }
  115. $bps = $header['bits_per_pixel']; //bits per pixel
  116. $wid2 = ceil(($bps/8 * $header['width']) / 4) * 4;
  117. $colors = pow(2, $bps);
  118. $wid = $header['width'];
  119. $hei = $header['height'];
  120. $img = imagecreatetruecolor($header['width'], $header['height']);
  121. //read palette
  122. if ($bps < 9)
  123. {
  124. for ($i=0; $i<$colors; $i++)
  125. {
  126. $palette[] = self::undword(substr($data, $pos, 4));
  127. $pos += 4;
  128. }
  129. }
  130. else
  131. {
  132. if ($bps == 32)
  133. {
  134. imagealphablending($img, false);
  135. imagesavealpha($img, true);
  136. }
  137. $palette = array();
  138. }
  139. //read pixels
  140. for ($y=$hei-1; $y>=0; $y--)
  141. {
  142. $row = substr($data, $pos, $wid2);
  143. $pos += $wid2;
  144. $pixels = self::str_split2($row, $bps, $palette);
  145. for ($x=0; $x<$wid; $x++)
  146. {
  147. self::makepixel($img, $x, $y, $pixels[$x], $bps);
  148. }
  149. }
  150. return $img;
  151. }
  152. public static function imagecreatefrombmp($filename)
  153. {
  154. return self::imagecreatefromstring(file_get_contents($filename));
  155. }
  156. private static function str_split2($row, $bps, $palette)
  157. {
  158. switch ($bps)
  159. {
  160. case 32:
  161. case 24: return str_split($row, $bps/8);
  162. case 8: $out = array();
  163. $count = strlen($row);
  164. for ($i=0; $i<$count; $i++)
  165. {
  166. $out[] = $palette[ ord($row[$i]) ];
  167. }
  168. return $out;
  169. case 4: $out = array();
  170. $count = strlen($row);
  171. for ($i=0; $i<$count; $i++)
  172. {
  173. $roww = ord($row[$i]);
  174. $out[] = $palette[ ($roww & 240) >> 4 ];
  175. $out[] = $palette[ ($roww & 15) ];
  176. }
  177. return $out;
  178. case 1: $out = array();
  179. $count = strlen($row);
  180. for ($i=0; $i<$count; $i++)
  181. {
  182. $roww = ord($row[$i]);
  183. $out[] = $palette[ ($roww & 128) >> 7 ];
  184. $out[] = $palette[ ($roww & 64) >> 6 ];
  185. $out[] = $palette[ ($roww & 32) >> 5 ];
  186. $out[] = $palette[ ($roww & 16) >> 4 ];
  187. $out[] = $palette[ ($roww & 8) >> 3 ];
  188. $out[] = $palette[ ($roww & 4) >> 2 ];
  189. $out[] = $palette[ ($roww & 2) >> 1 ];
  190. $out[] = $palette[ ($roww & 1) ];
  191. }
  192. return $out;
  193. }
  194. }
  195. private static function makepixel($img, $x, $y, $str, $bps)
  196. {
  197. switch ($bps)
  198. {
  199. case 32 : $a = ord($str[0]);
  200. $b = ord($str[1]);
  201. $c = ord($str[2]);
  202. $d = 256 - ord($str[3]); //TODO: gives imperfect results
  203. $pixel = $d*256*256*256 + $c*256*256 + $b*256 + $a;
  204. imagesetpixel($img, $x, $y, $pixel);
  205. break;
  206. case 24 : $a = ord($str[0]);
  207. $b = ord($str[1]);
  208. $c = ord($str[2]);
  209. $pixel = $c*256*256 + $b*256 + $a;
  210. imagesetpixel($img, $x, $y, $pixel);
  211. break;
  212. case 8 :
  213. case 4 :
  214. case 1 : imagesetpixel($img, $x, $y, $str);
  215. break;
  216. }
  217. }
  218. private static function byte3($n)
  219. {
  220. return chr($n & 255) . chr(($n >> 8) & 255) . chr(($n >> 16) & 255);
  221. }
  222. private static function undword($n)
  223. {
  224. $r = unpack("V", $n);
  225. return $r[1];
  226. }
  227. private static function dword($n)
  228. {
  229. return pack("V", $n);
  230. }
  231. private static function word($n)
  232. {
  233. return pack("v", $n);
  234. }
  235. }