/Atomic Projects/vendor/phpoffice/common/src/Common/Drawing.php

https://gitlab.com/imamul68e/137619_PHP31 · PHP · 237 lines · 109 code · 20 blank · 108 comment · 26 complexity · 9c20c16265f87ed4668fe56c1a4dace4 MD5 · raw file

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