For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
8.8 KiB

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_Shared_Drawing
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Shared
  32. * @copyright Copyright (c) 2006 - 2015 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. {
  44. return round($pValue * 9525);
  45. }
  46. /**
  47. * Convert EMU to pixels
  48. *
  49. * @param int $pValue Value in EMU
  50. * @return int Value in pixels
  51. */
  52. public static function EMUToPixels($pValue = 0)
  53. {
  54. if ($pValue != 0) {
  55. return round($pValue / 9525);
  56. } else {
  57. return 0;
  58. }
  59. }
  60. /**
  61. * Convert pixels to column width. Exact algorithm not known.
  62. * By inspection of a real Excel file using Calibri 11, one finds 1000px ~ 142.85546875
  63. * This gives a conversion factor of 7. Also, we assume that pixels and font size are proportional.
  64. *
  65. * @param int $pValue Value in pixels
  66. * @param PHPExcel_Style_Font $pDefaultFont Default font of the workbook
  67. * @return int Value in cell dimension
  68. */
  69. public static function pixelsToCellDimension($pValue = 0, PHPExcel_Style_Font $pDefaultFont)
  70. {
  71. // Font name and size
  72. $name = $pDefaultFont->getName();
  73. $size = $pDefaultFont->getSize();
  74. if (isset(PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size])) {
  75. // Exact width can be determined
  76. $colWidth = $pValue * PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['width'] / PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['px'];
  77. } else {
  78. // We don't have data for this particular font and size, use approximation by
  79. // extrapolating from Calibri 11
  80. $colWidth = $pValue * 11 * PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['width'] / PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['px'] / $size;
  81. }
  82. return $colWidth;
  83. }
  84. /**
  85. * Convert column width from (intrinsic) Excel units to pixels
  86. *
  87. * @param float $pValue Value in cell dimension
  88. * @param PHPExcel_Style_Font $pDefaultFont Default font of the workbook
  89. * @return int Value in pixels
  90. */
  91. public static function cellDimensionToPixels($pValue = 0, PHPExcel_Style_Font $pDefaultFont)
  92. {
  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 * PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['px'] / PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['width'];
  99. } else {
  100. // We don't have data for this particular font and size, use approximation by
  101. // extrapolating from Calibri 11
  102. $colWidth = $pValue * $size * PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['px'] / PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['width'] / 11;
  103. }
  104. // Round pixels to closest integer
  105. $colWidth = (int) round($colWidth);
  106. return $colWidth;
  107. }
  108. /**
  109. * Convert pixels to points
  110. *
  111. * @param int $pValue Value in pixels
  112. * @return int Value in points
  113. */
  114. public static function pixelsToPoints($pValue = 0)
  115. {
  116. return $pValue * 0.67777777;
  117. }
  118. /**
  119. * Convert points to pixels
  120. *
  121. * @param int $pValue Value in points
  122. * @return int Value in pixels
  123. */
  124. public static function pointsToPixels($pValue = 0)
  125. {
  126. if ($pValue != 0) {
  127. return (int) ceil($pValue * 1.333333333);
  128. } else {
  129. return 0;
  130. }
  131. }
  132. /**
  133. * Convert degrees to angle
  134. *
  135. * @param int $pValue Degrees
  136. * @return int Angle
  137. */
  138. public static function degreesToAngle($pValue = 0)
  139. {
  140. return (int)round($pValue * 60000);
  141. }
  142. /**
  143. * Convert angle to degrees
  144. *
  145. * @param int $pValue Angle
  146. * @return int Degrees
  147. */
  148. public static function angleToDegrees($pValue = 0)
  149. {
  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. }
  171. $temp = unpack("H*", $read);
  172. $hex = $temp[1];
  173. $header = substr($hex, 0, 108);
  174. // Process the header
  175. // Structure: http://www.fastgraph.com/help/bmp_header_format.html
  176. if (substr($header, 0, 4)=="424d") {
  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. // Calculate line-ending and padding
  204. if ($x >= $width) {
  205. // If padding needed, ignore image-padding
  206. // Shift i to the ending of the current 32-bit-block
  207. if ($usePadding) {
  208. $i += $width%4;
  209. }
  210. // Reset horizontal position
  211. $x = 0;
  212. // Raise the height-position (bottom-up)
  213. $y++;
  214. // Reached the image-height? Break the for-loop
  215. if ($y > $height) {
  216. break;
  217. }
  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. }