Drawing.php 8.2 KB

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