Drawing.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Shared
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. /**
  28. * PHPExcel_Shared_Drawing
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Shared
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Shared_Drawing
  35. {
  36. /**
  37. * Convert pixels to EMU
  38. *
  39. * @param int $pValue Value in pixels
  40. * @return int Value in EMU
  41. */
  42. public static function pixelsToEMU($pValue = 0) {
  43. return round($pValue * 9525);
  44. }
  45. /**
  46. * Convert EMU to pixels
  47. *
  48. * @param int $pValue Value in EMU
  49. * @return int Value in pixels
  50. */
  51. public static function EMUToPixels($pValue = 0) {
  52. if ($pValue != 0) {
  53. return round($pValue / 9525);
  54. } else {
  55. return 0;
  56. }
  57. }
  58. /**
  59. * Convert pixels to column width. Exact algorithm not known.
  60. * By inspection of a real Excel file using Calibri 11, one finds 1000px ~ 142.85546875
  61. * This gives a conversion factor of 7. Also, we assume that pixels and font size are proportional.
  62. *
  63. * @param int $pValue Value in pixels
  64. * @param PHPExcel_Style_Font $pDefaultFont Default font of the workbook
  65. * @return int Value in cell dimension
  66. */
  67. public static function pixelsToCellDimension($pValue = 0, PHPExcel_Style_Font $pDefaultFont) {
  68. // Font name and size
  69. $name = $pDefaultFont->getName();
  70. $size = $pDefaultFont->getSize();
  71. if (isset(PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size])) {
  72. // Exact width can be determined
  73. $colWidth = $pValue
  74. * PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['width']
  75. / PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['px'];
  76. } else {
  77. // We don't have data for this particular font and size, use approximation by
  78. // extrapolating from Calibri 11
  79. $colWidth = $pValue * 11
  80. * PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['width']
  81. / PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['px'] / $size;
  82. }
  83. return $colWidth;
  84. }
  85. /**
  86. * Convert column width from (intrinsic) Excel units to pixels
  87. *
  88. * @param float $pValue Value in cell dimension
  89. * @param PHPExcel_Style_Font $pDefaultFont Default font of the workbook
  90. * @return int Value in pixels
  91. */
  92. public static function cellDimensionToPixels($pValue = 0, PHPExcel_Style_Font $pDefaultFont) {
  93. // Font name and size
  94. $name = $pDefaultFont->getName();
  95. $size = $pDefaultFont->getSize();
  96. if (isset(PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size])) {
  97. // Exact width can be determined
  98. $colWidth = $pValue
  99. * PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['px']
  100. / PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['width'];
  101. } else {
  102. // We don't have data for this particular font and size, use approximation by
  103. // extrapolating from Calibri 11
  104. $colWidth = $pValue * $size
  105. * PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['px']
  106. / PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['width'] / 11;
  107. }
  108. // Round pixels to closest integer
  109. $colWidth = (int) round($colWidth);
  110. return $colWidth;
  111. }
  112. /**
  113. * Convert pixels to points
  114. *
  115. * @param int $pValue Value in pixels
  116. * @return int Value in points
  117. */
  118. public static function pixelsToPoints($pValue = 0) {
  119. return $pValue * 0.67777777;
  120. }
  121. /**
  122. * Convert points to pixels
  123. *
  124. * @param int $pValue Value in points
  125. * @return int Value in pixels
  126. */
  127. public static function pointsToPixels($pValue = 0) {
  128. if ($pValue != 0) {
  129. return (int) ceil($pValue * 1.333333333);
  130. } else {
  131. return 0;
  132. }
  133. }
  134. /**
  135. * Convert degrees to angle
  136. *
  137. * @param int $pValue Degrees
  138. * @return int Angle
  139. */
  140. public static function degreesToAngle($pValue = 0) {
  141. return (int)round($pValue * 60000);
  142. }
  143. /**
  144. * Convert angle to degrees
  145. *
  146. * @param int $pValue Angle
  147. * @return int Degrees
  148. */
  149. public static function angleToDegrees($pValue = 0) {
  150. if ($pValue != 0) {
  151. return round($pValue / 60000);
  152. } else {
  153. return 0;
  154. }
  155. }
  156. /**
  157. * Create a new image from file. By alexander at alexauto dot nl
  158. *
  159. * @link http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214
  160. * @param string $filename Path to Windows DIB (BMP) image
  161. * @return resource
  162. */
  163. public static function imagecreatefrombmp($p_sFile)
  164. {
  165. // Load the image into a string
  166. $file = fopen($p_sFile,"rb");
  167. $read = fread($file,10);
  168. while(!feof($file)&&($read<>""))
  169. $read .= fread($file,1024);
  170. $temp = unpack("H*",$read);
  171. $hex = $temp[1];
  172. $header = substr($hex,0,108);
  173. // Process the header
  174. // Structure: http://www.fastgraph.com/help/bmp_header_format.html
  175. if (substr($header,0,4)=="424d")
  176. {
  177. // Cut it in parts of 2 bytes
  178. $header_parts = str_split($header,2);
  179. // Get the width 4 bytes
  180. $width = hexdec($header_parts[19].$header_parts[18]);
  181. // Get the height 4 bytes
  182. $height = hexdec($header_parts[23].$header_parts[22]);
  183. // Unset the header params
  184. unset($header_parts);
  185. }
  186. // Define starting X and Y
  187. $x = 0;
  188. $y = 1;
  189. // Create newimage
  190. $image = imagecreatetruecolor($width,$height);
  191. // Grab the body from the image
  192. $body = substr($hex,108);
  193. // Calculate if padding at the end-line is needed
  194. // Divided by two to keep overview.
  195. // 1 byte = 2 HEX-chars
  196. $body_size = (strlen($body)/2);
  197. $header_size = ($width*$height);
  198. // Use end-line padding? Only when needed
  199. $usePadding = ($body_size>($header_size*3)+4);
  200. // Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
  201. // Calculate the next DWORD-position in the body
  202. for ($i=0;$i<$body_size;$i+=3)
  203. {
  204. // Calculate line-ending and padding
  205. if ($x>=$width)
  206. {
  207. // If padding needed, ignore image-padding
  208. // Shift i to the ending of the current 32-bit-block
  209. if ($usePadding)
  210. $i += $width%4;
  211. // Reset horizontal position
  212. $x = 0;
  213. // Raise the height-position (bottom-up)
  214. $y++;
  215. // Reached the image-height? Break the for-loop
  216. if ($y>$height)
  217. break;
  218. }
  219. // Calculation of the RGB-pixel (defined as BGR in image-data)
  220. // Define $i_pos as absolute position in the body
  221. $i_pos = $i*2;
  222. $r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
  223. $g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
  224. $b = hexdec($body[$i_pos].$body[$i_pos+1]);
  225. // Calculate and draw the pixel
  226. $color = imagecolorallocate($image,$r,$g,$b);
  227. imagesetpixel($image,$x,$height-$y,$color);
  228. // Raise the horizontal position
  229. $x++;
  230. }
  231. // Unset the body / free the memory
  232. unset($body);
  233. // Return image-object
  234. return $image;
  235. }
  236. }