Xls.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Shared;
  3. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  4. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  5. class Xls
  6. {
  7. /**
  8. * Get the width of a column in pixels. We use the relationship y = ceil(7x) where
  9. * x is the width in intrinsic Excel units (measuring width in number of normal characters)
  10. * This holds for Arial 10.
  11. *
  12. * @param Worksheet $sheet The sheet
  13. * @param string $col The column
  14. *
  15. * @return int The width in pixels
  16. */
  17. public static function sizeCol($sheet, $col = 'A')
  18. {
  19. // default font of the workbook
  20. $font = $sheet->getParent()->getDefaultStyle()->getFont();
  21. $columnDimensions = $sheet->getColumnDimensions();
  22. // first find the true column width in pixels (uncollapsed and unhidden)
  23. if (isset($columnDimensions[$col]) and $columnDimensions[$col]->getWidth() != -1) {
  24. // then we have column dimension with explicit width
  25. $columnDimension = $columnDimensions[$col];
  26. $width = $columnDimension->getWidth();
  27. $pixelWidth = Drawing::cellDimensionToPixels($width, $font);
  28. } elseif ($sheet->getDefaultColumnDimension()->getWidth() != -1) {
  29. // then we have default column dimension with explicit width
  30. $defaultColumnDimension = $sheet->getDefaultColumnDimension();
  31. $width = $defaultColumnDimension->getWidth();
  32. $pixelWidth = Drawing::cellDimensionToPixels($width, $font);
  33. } else {
  34. // we don't even have any default column dimension. Width depends on default font
  35. $pixelWidth = Font::getDefaultColumnWidthByFont($font, true);
  36. }
  37. // now find the effective column width in pixels
  38. if (isset($columnDimensions[$col]) and !$columnDimensions[$col]->getVisible()) {
  39. $effectivePixelWidth = 0;
  40. } else {
  41. $effectivePixelWidth = $pixelWidth;
  42. }
  43. return $effectivePixelWidth;
  44. }
  45. /**
  46. * Convert the height of a cell from user's units to pixels. By interpolation
  47. * the relationship is: y = 4/3x. If the height hasn't been set by the user we
  48. * use the default value. If the row is hidden we use a value of zero.
  49. *
  50. * @param Worksheet $sheet The sheet
  51. * @param int $row The row index (1-based)
  52. *
  53. * @return int The width in pixels
  54. */
  55. public static function sizeRow($sheet, $row = 1)
  56. {
  57. // default font of the workbook
  58. $font = $sheet->getParent()->getDefaultStyle()->getFont();
  59. $rowDimensions = $sheet->getRowDimensions();
  60. // first find the true row height in pixels (uncollapsed and unhidden)
  61. if (isset($rowDimensions[$row]) and $rowDimensions[$row]->getRowHeight() != -1) {
  62. // then we have a row dimension
  63. $rowDimension = $rowDimensions[$row];
  64. $rowHeight = $rowDimension->getRowHeight();
  65. $pixelRowHeight = (int) ceil(4 * $rowHeight / 3); // here we assume Arial 10
  66. } elseif ($sheet->getDefaultRowDimension()->getRowHeight() != -1) {
  67. // then we have a default row dimension with explicit height
  68. $defaultRowDimension = $sheet->getDefaultRowDimension();
  69. $rowHeight = $defaultRowDimension->getRowHeight();
  70. $pixelRowHeight = Drawing::pointsToPixels($rowHeight);
  71. } else {
  72. // we don't even have any default row dimension. Height depends on default font
  73. $pointRowHeight = Font::getDefaultRowHeightByFont($font);
  74. $pixelRowHeight = Font::fontSizeToPixels($pointRowHeight);
  75. }
  76. // now find the effective row height in pixels
  77. if (isset($rowDimensions[$row]) and !$rowDimensions[$row]->getVisible()) {
  78. $effectivePixelRowHeight = 0;
  79. } else {
  80. $effectivePixelRowHeight = $pixelRowHeight;
  81. }
  82. return $effectivePixelRowHeight;
  83. }
  84. /**
  85. * Get the horizontal distance in pixels between two anchors
  86. * The distanceX is found as sum of all the spanning columns widths minus correction for the two offsets.
  87. *
  88. * @param Worksheet $sheet
  89. * @param string $startColumn
  90. * @param int $startOffsetX Offset within start cell measured in 1/1024 of the cell width
  91. * @param string $endColumn
  92. * @param int $endOffsetX Offset within end cell measured in 1/1024 of the cell width
  93. *
  94. * @return int Horizontal measured in pixels
  95. */
  96. public static function getDistanceX(Worksheet $sheet, $startColumn = 'A', $startOffsetX = 0, $endColumn = 'A', $endOffsetX = 0)
  97. {
  98. $distanceX = 0;
  99. // add the widths of the spanning columns
  100. $startColumnIndex = Coordinate::columnIndexFromString($startColumn);
  101. $endColumnIndex = Coordinate::columnIndexFromString($endColumn);
  102. for ($i = $startColumnIndex; $i <= $endColumnIndex; ++$i) {
  103. $distanceX += self::sizeCol($sheet, Coordinate::stringFromColumnIndex($i));
  104. }
  105. // correct for offsetX in startcell
  106. $distanceX -= (int) floor(self::sizeCol($sheet, $startColumn) * $startOffsetX / 1024);
  107. // correct for offsetX in endcell
  108. $distanceX -= (int) floor(self::sizeCol($sheet, $endColumn) * (1 - $endOffsetX / 1024));
  109. return $distanceX;
  110. }
  111. /**
  112. * Get the vertical distance in pixels between two anchors
  113. * The distanceY is found as sum of all the spanning rows minus two offsets.
  114. *
  115. * @param Worksheet $sheet
  116. * @param int $startRow (1-based)
  117. * @param int $startOffsetY Offset within start cell measured in 1/256 of the cell height
  118. * @param int $endRow (1-based)
  119. * @param int $endOffsetY Offset within end cell measured in 1/256 of the cell height
  120. *
  121. * @return int Vertical distance measured in pixels
  122. */
  123. public static function getDistanceY(Worksheet $sheet, $startRow = 1, $startOffsetY = 0, $endRow = 1, $endOffsetY = 0)
  124. {
  125. $distanceY = 0;
  126. // add the widths of the spanning rows
  127. for ($row = $startRow; $row <= $endRow; ++$row) {
  128. $distanceY += self::sizeRow($sheet, $row);
  129. }
  130. // correct for offsetX in startcell
  131. $distanceY -= (int) floor(self::sizeRow($sheet, $startRow) * $startOffsetY / 256);
  132. // correct for offsetX in endcell
  133. $distanceY -= (int) floor(self::sizeRow($sheet, $endRow) * (1 - $endOffsetY / 256));
  134. return $distanceY;
  135. }
  136. /**
  137. * Convert 1-cell anchor coordinates to 2-cell anchor coordinates
  138. * This function is ported from PEAR Spreadsheet_Writer_Excel with small modifications.
  139. *
  140. * Calculate the vertices that define the position of the image as required by
  141. * the OBJ record.
  142. *
  143. * +------------+------------+
  144. * | A | B |
  145. * +-----+------------+------------+
  146. * | |(x1,y1) | |
  147. * | 1 |(A1)._______|______ |
  148. * | | | | |
  149. * | | | | |
  150. * +-----+----| BITMAP |-----+
  151. * | | | | |
  152. * | 2 | |______________. |
  153. * | | | (B2)|
  154. * | | | (x2,y2)|
  155. * +---- +------------+------------+
  156. *
  157. * Example of a bitmap that covers some of the area from cell A1 to cell B2.
  158. *
  159. * Based on the width and height of the bitmap we need to calculate 8 vars:
  160. * $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
  161. * The width and height of the cells are also variable and have to be taken into
  162. * account.
  163. * The values of $col_start and $row_start are passed in from the calling
  164. * function. The values of $col_end and $row_end are calculated by subtracting
  165. * the width and height of the bitmap from the width and height of the
  166. * underlying cells.
  167. * The vertices are expressed as a percentage of the underlying cell width as
  168. * follows (rhs values are in pixels):
  169. *
  170. * x1 = X / W *1024
  171. * y1 = Y / H *256
  172. * x2 = (X-1) / W *1024
  173. * y2 = (Y-1) / H *256
  174. *
  175. * Where: X is distance from the left side of the underlying cell
  176. * Y is distance from the top of the underlying cell
  177. * W is the width of the cell
  178. * H is the height of the cell
  179. *
  180. * @param Worksheet $sheet
  181. * @param string $coordinates E.g. 'A1'
  182. * @param int $offsetX Horizontal offset in pixels
  183. * @param int $offsetY Vertical offset in pixels
  184. * @param int $width Width in pixels
  185. * @param int $height Height in pixels
  186. *
  187. * @return array
  188. */
  189. public static function oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height)
  190. {
  191. list($column, $row) = Coordinate::coordinateFromString($coordinates);
  192. $col_start = Coordinate::columnIndexFromString($column);
  193. $row_start = $row - 1;
  194. $x1 = $offsetX;
  195. $y1 = $offsetY;
  196. // Initialise end cell to the same as the start cell
  197. $col_end = $col_start; // Col containing lower right corner of object
  198. $row_end = $row_start; // Row containing bottom right corner of object
  199. // Zero the specified offset if greater than the cell dimensions
  200. if ($x1 >= self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_start))) {
  201. $x1 = 0;
  202. }
  203. if ($y1 >= self::sizeRow($sheet, $row_start + 1)) {
  204. $y1 = 0;
  205. }
  206. $width = $width + $x1 - 1;
  207. $height = $height + $y1 - 1;
  208. // Subtract the underlying cell widths to find the end cell of the image
  209. while ($width >= self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_end))) {
  210. $width -= self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_end));
  211. ++$col_end;
  212. }
  213. // Subtract the underlying cell heights to find the end cell of the image
  214. while ($height >= self::sizeRow($sheet, $row_end + 1)) {
  215. $height -= self::sizeRow($sheet, $row_end + 1);
  216. ++$row_end;
  217. }
  218. // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
  219. // with zero height or width.
  220. if (self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_start)) == 0) {
  221. return;
  222. }
  223. if (self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_end)) == 0) {
  224. return;
  225. }
  226. if (self::sizeRow($sheet, $row_start + 1) == 0) {
  227. return;
  228. }
  229. if (self::sizeRow($sheet, $row_end + 1) == 0) {
  230. return;
  231. }
  232. // Convert the pixel values to the percentage value expected by Excel
  233. $x1 = $x1 / self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_start)) * 1024;
  234. $y1 = $y1 / self::sizeRow($sheet, $row_start + 1) * 256;
  235. $x2 = ($width + 1) / self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_end)) * 1024; // Distance to right side of object
  236. $y2 = ($height + 1) / self::sizeRow($sheet, $row_end + 1) * 256; // Distance to bottom of object
  237. $startCoordinates = Coordinate::stringFromColumnIndex($col_start) . ($row_start + 1);
  238. $endCoordinates = Coordinate::stringFromColumnIndex($col_end) . ($row_end + 1);
  239. $twoAnchor = [
  240. 'startCoordinates' => $startCoordinates,
  241. 'startOffsetX' => $x1,
  242. 'startOffsetY' => $y1,
  243. 'endCoordinates' => $endCoordinates,
  244. 'endOffsetX' => $x2,
  245. 'endOffsetY' => $y2,
  246. ];
  247. return $twoAnchor;
  248. }
  249. }