PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Atomic Projects/vendor/phpoffice/phpword/src/PhpWord/Shared/Converter.php

https://gitlab.com/imamul68e/137619_PHP31
PHP | 278 lines | 108 code | 26 blank | 144 comment | 6 complexity | 97b1a0180264b7de728347fb9a70f604 MD5 | raw file
  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. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2016 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Shared;
  18. /**
  19. * Common converter functions
  20. */
  21. class Converter
  22. {
  23. const INCH_TO_CM = 2.54;
  24. const INCH_TO_TWIP = 1440;
  25. const INCH_TO_PIXEL = 96;
  26. const INCH_TO_POINT = 72;
  27. const PIXEL_TO_EMU = 9525;
  28. const DEGREE_TO_ANGLE = 60000;
  29. /**
  30. * Convert centimeter to twip
  31. *
  32. * @param int $centimeter
  33. * @return float
  34. */
  35. public static function cmToTwip($centimeter = 1)
  36. {
  37. return $centimeter / self::INCH_TO_CM * self::INCH_TO_TWIP;
  38. }
  39. /**
  40. * Convert centimeter to inch
  41. *
  42. * @param int $centimeter
  43. * @return float
  44. */
  45. public static function cmToInch($centimeter = 1)
  46. {
  47. return $centimeter / self::INCH_TO_CM;
  48. }
  49. /**
  50. * Convert centimeter to pixel
  51. *
  52. * @param int $centimeter
  53. * @return float
  54. */
  55. public static function cmToPixel($centimeter = 1)
  56. {
  57. return $centimeter / self::INCH_TO_CM * self::INCH_TO_PIXEL;
  58. }
  59. /**
  60. * Convert centimeter to point
  61. *
  62. * @param int $centimeter
  63. * @return float
  64. */
  65. public static function cmToPoint($centimeter = 1)
  66. {
  67. return $centimeter / self::INCH_TO_CM * self::INCH_TO_POINT;
  68. }
  69. /**
  70. * Convert centimeter to EMU
  71. *
  72. * @param int $centimeter
  73. * @return int
  74. */
  75. public static function cmToEmu($centimeter = 1)
  76. {
  77. return round($centimeter / self::INCH_TO_CM * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU);
  78. }
  79. /**
  80. * Convert inch to twip
  81. *
  82. * @param int $inch
  83. * @return int
  84. */
  85. public static function inchToTwip($inch = 1)
  86. {
  87. return $inch * self::INCH_TO_TWIP;
  88. }
  89. /**
  90. * Convert inch to centimeter
  91. *
  92. * @param int $inch
  93. * @return float
  94. */
  95. public static function inchToCm($inch = 1)
  96. {
  97. return $inch * self::INCH_TO_CM;
  98. }
  99. /**
  100. * Convert inch to pixel
  101. *
  102. * @param int $inch
  103. * @return int
  104. */
  105. public static function inchToPixel($inch = 1)
  106. {
  107. return $inch * self::INCH_TO_PIXEL;
  108. }
  109. /**
  110. * Convert inch to point
  111. *
  112. * @param int $inch
  113. * @return int
  114. */
  115. public static function inchToPoint($inch = 1)
  116. {
  117. return $inch * self::INCH_TO_POINT;
  118. }
  119. /**
  120. * Convert inch to EMU
  121. *
  122. * @param int $inch
  123. * @return int
  124. */
  125. public static function inchToEmu($inch = 1)
  126. {
  127. return round($inch * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU);
  128. }
  129. /**
  130. * Convert pixel to twip
  131. *
  132. * @param int $pixel
  133. * @return int
  134. */
  135. public static function pixelToTwip($pixel = 1)
  136. {
  137. return $pixel / self::INCH_TO_PIXEL * self::INCH_TO_TWIP;
  138. }
  139. /**
  140. * Convert pixel to centimeter
  141. *
  142. * @param int $pixel
  143. * @return float
  144. */
  145. public static function pixelToCm($pixel = 1)
  146. {
  147. return $pixel / self::INCH_TO_PIXEL * self::INCH_TO_CM;
  148. }
  149. /**
  150. * Convert pixel to point
  151. *
  152. * @param int $pixel
  153. * @return float
  154. */
  155. public static function pixelToPoint($pixel = 1)
  156. {
  157. return $pixel / self::INCH_TO_PIXEL * self::INCH_TO_POINT;
  158. }
  159. /**
  160. * Convert pixel to EMU
  161. *
  162. * @param int $pixel
  163. * @return int
  164. */
  165. public static function pixelToEmu($pixel = 1)
  166. {
  167. return round($pixel * self::PIXEL_TO_EMU);
  168. }
  169. /**
  170. * Convert point to twip unit
  171. *
  172. * @param int $point
  173. * @return int
  174. */
  175. public static function pointToTwip($point = 1)
  176. {
  177. return $point / self::INCH_TO_POINT * self::INCH_TO_TWIP;
  178. }
  179. /**
  180. * Convert point to pixel
  181. *
  182. * @param int $point
  183. * @return float
  184. */
  185. public static function pointToPixel($point = 1)
  186. {
  187. return $point / self::INCH_TO_POINT * self::INCH_TO_PIXEL;
  188. }
  189. /**
  190. * Convert point to EMU
  191. *
  192. * @param int $point
  193. * @return int
  194. */
  195. public static function pointToEmu($point = 1)
  196. {
  197. return round($point / self::INCH_TO_POINT * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU);
  198. }
  199. /**
  200. * Convert EMU to pixel
  201. *
  202. * @param int $emu
  203. * @return int
  204. */
  205. public static function emuToPixel($emu = 1)
  206. {
  207. return round($emu / self::PIXEL_TO_EMU);
  208. }
  209. /**
  210. * Convert degree to angle
  211. *
  212. * @param int $degree
  213. * @return int
  214. */
  215. public static function degreeToAngle($degree = 1)
  216. {
  217. return (int)round($degree * self::DEGREE_TO_ANGLE);
  218. }
  219. /**
  220. * Convert angle to degrees
  221. *
  222. * @param int $angle
  223. * @return int
  224. */
  225. public static function angleToDegree($angle = 1)
  226. {
  227. return round($angle / self::DEGREE_TO_ANGLE);
  228. }
  229. /**
  230. * Convert HTML hexadecimal to RGB
  231. *
  232. * @param string $value HTML Color in hexadecimal
  233. * @return array Value in RGB
  234. */
  235. public static function htmlToRgb($value)
  236. {
  237. if ($value[0] == '#') {
  238. $value = substr($value, 1);
  239. }
  240. if (strlen($value) == 6) {
  241. list($red, $green, $blue) = array($value[0] . $value[1], $value[2] . $value[3], $value[4] . $value[5]);
  242. } elseif (strlen($value) == 3) {
  243. list($red, $green, $blue) = array($value[0] . $value[0], $value[1] . $value[1], $value[2] . $value[2]);
  244. } else {
  245. return false;
  246. }
  247. $red = hexdec($red);
  248. $green = hexdec($green);
  249. $blue = hexdec($blue);
  250. return array($red, $green, $blue);
  251. }
  252. }