Drawing.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Shared;
  18. /**
  19. * Drawing
  20. */
  21. class Drawing
  22. {
  23. const DPI_96 = 96;
  24. /**
  25. * Convert pixels to EMU
  26. *
  27. * @param int $pValue Value in pixels
  28. * @return int
  29. */
  30. public static function pixelsToEmu($pValue = 0)
  31. {
  32. return round($pValue * 9525);
  33. }
  34. /**
  35. * Convert EMU to pixels
  36. *
  37. * @param int $pValue Value in EMU
  38. * @return int
  39. */
  40. public static function emuToPixels($pValue = 0)
  41. {
  42. if ($pValue == 0) {
  43. return 0;
  44. }
  45. return round($pValue / 9525);
  46. }
  47. /**
  48. * Convert pixels to points
  49. *
  50. * @param int $pValue Value in pixels
  51. * @return float
  52. */
  53. public static function pixelsToPoints($pValue = 0)
  54. {
  55. return $pValue * 0.67777777;
  56. }
  57. /**
  58. * Convert points width to centimeters
  59. *
  60. * @param int $pValue Value in points
  61. * @return float
  62. */
  63. public static function pointsToCentimeters($pValue = 0)
  64. {
  65. if ($pValue == 0) {
  66. return 0;
  67. }
  68. return (($pValue * 1.333333333) / self::DPI_96) * 2.54;
  69. }
  70. /**
  71. * Convert points width to pixels
  72. *
  73. * @param int $pValue Value in points
  74. * @return float
  75. */
  76. public static function pointsToPixels($pValue = 0)
  77. {
  78. if ($pValue == 0) {
  79. return 0;
  80. }
  81. return $pValue * 1.333333333;
  82. }
  83. /**
  84. * Convert pixels to centimeters
  85. *
  86. * @param int $pValue Value in pixels
  87. * @return float
  88. */
  89. public static function pixelsToCentimeters($pValue = 0)
  90. {
  91. //return $pValue * 0.028;
  92. return ($pValue / self::DPI_96) * 2.54;
  93. }
  94. /**
  95. * Convert centimeters width to pixels
  96. *
  97. * @param int $pValue Value in centimeters
  98. * @return float
  99. */
  100. public static function centimetersToPixels($pValue = 0)
  101. {
  102. if ($pValue == 0) {
  103. return 0;
  104. }
  105. return ($pValue / 2.54) * self::DPI_96;
  106. }
  107. /**
  108. * Convert degrees to angle
  109. *
  110. * @param int $pValue Degrees
  111. * @return int
  112. */
  113. public static function degreesToAngle($pValue = 0)
  114. {
  115. return (int) round($pValue * 60000);
  116. }
  117. /**
  118. * Convert angle to degrees
  119. *
  120. * @param int $pValue Angle
  121. * @return int
  122. */
  123. public static function angleToDegrees($pValue = 0)
  124. {
  125. if ($pValue == 0) {
  126. return 0;
  127. }
  128. return round($pValue / 60000);
  129. }
  130. /**
  131. * Convert centimeters width to twips
  132. *
  133. * @param int $pValue
  134. * @return float
  135. */
  136. public static function centimetersToTwips($pValue = 0)
  137. {
  138. if ($pValue == 0) {
  139. return 0;
  140. }
  141. return $pValue * 566.928;
  142. }
  143. /**
  144. * Convert twips width to centimeters
  145. *
  146. * @param int $pValue
  147. * @return float
  148. */
  149. public static function twipsToCentimeters($pValue = 0)
  150. {
  151. if ($pValue == 0) {
  152. return 0;
  153. }
  154. return $pValue / 566.928;
  155. }
  156. /**
  157. * Convert inches width to twips
  158. *
  159. * @param int $pValue
  160. * @return float
  161. */
  162. public static function inchesToTwips($pValue = 0)
  163. {
  164. if ($pValue == 0) {
  165. return 0;
  166. }
  167. return $pValue * 1440;
  168. }
  169. /**
  170. * Convert twips width to inches
  171. *
  172. * @param int $pValue
  173. * @return float
  174. */
  175. public static function twipsToInches($pValue = 0)
  176. {
  177. if ($pValue == 0) {
  178. return 0;
  179. }
  180. return $pValue / 1440;
  181. }
  182. /**
  183. * Convert twips width to pixels
  184. *
  185. * @param int $pValue
  186. * @return float
  187. */
  188. public static function twipsToPixels($pValue = 0)
  189. {
  190. if ($pValue == 0) {
  191. return 0;
  192. }
  193. return round($pValue / 15.873984);
  194. }
  195. /**
  196. * Convert HTML hexadecimal to RGB
  197. *
  198. * @param string $pValue HTML Color in hexadecimal
  199. * @return array|false Value in RGB
  200. */
  201. public static function htmlToRGB($pValue)
  202. {
  203. if ($pValue[0] == '#') {
  204. $pValue = substr($pValue, 1);
  205. }
  206. if (strlen($pValue) == 6) {
  207. list($colorR, $colorG, $colorB) = array($pValue[0] . $pValue[1], $pValue[2] . $pValue[3], $pValue[4] . $pValue[5]);
  208. } elseif (strlen($pValue) == 3) {
  209. list($colorR, $colorG, $colorB) = array($pValue[0] . $pValue[0], $pValue[1] . $pValue[1], $pValue[2] . $pValue[2]);
  210. } else {
  211. return false;
  212. }
  213. $colorR = hexdec($colorR);
  214. $colorG = hexdec($colorG);
  215. $colorB = hexdec($colorB);
  216. return array($colorR, $colorG, $colorB);
  217. }
  218. }