No Description

Drawing.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * @link https://github.com/PHPOffice/PHPWord
  6. * @copyright 2014 PHPWord
  7. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  8. */
  9. namespace PhpOffice\PhpWord\Shared;
  10. /**
  11. * Common drawing functions
  12. */
  13. class Drawing
  14. {
  15. /**
  16. * Convert pixels to EMU
  17. *
  18. * @param integer $pValue Value in pixels
  19. * @return double Value in EMU
  20. */
  21. public static function pixelsToEMU($pValue = 0)
  22. {
  23. return round($pValue * 9525);
  24. }
  25. /**
  26. * Convert EMU to pixels
  27. *
  28. * @param integer $pValue Value in EMU
  29. * @return integer Value in pixels
  30. */
  31. public static function emuToPixels($pValue = 0)
  32. {
  33. if ($pValue != 0) {
  34. return round($pValue / 9525);
  35. } else {
  36. return 0;
  37. }
  38. }
  39. /**
  40. * Convert pixels to points
  41. *
  42. * @param integer $pValue Value in pixels
  43. * @return double Value in points
  44. */
  45. public static function pixelsToPoints($pValue = 0)
  46. {
  47. return $pValue * 0.67777777;
  48. }
  49. /**
  50. * Convert points width to pixels
  51. *
  52. * @param integer $pValue Value in points
  53. * @return integer Value in pixels
  54. */
  55. public static function pointsToPixels($pValue = 0)
  56. {
  57. if ($pValue != 0) {
  58. return $pValue * 1.333333333;
  59. } else {
  60. return 0;
  61. }
  62. }
  63. /**
  64. * Convert degrees to angle
  65. *
  66. * @param integer $pValue Degrees
  67. * @return integer Angle
  68. */
  69. public static function degreesToAngle($pValue = 0)
  70. {
  71. return (integer)round($pValue * 60000);
  72. }
  73. /**
  74. * Convert angle to degrees
  75. *
  76. * @param integer $pValue Angle
  77. * @return integer Degrees
  78. */
  79. public static function angleToDegrees($pValue = 0)
  80. {
  81. if ($pValue != 0) {
  82. return round($pValue / 60000);
  83. } else {
  84. return 0;
  85. }
  86. }
  87. /**
  88. * Convert pixels to centimeters
  89. *
  90. * @param integer $pValue Value in pixels
  91. * @return double Value in centimeters
  92. */
  93. public static function pixelsToCentimeters($pValue = 0)
  94. {
  95. return $pValue * 0.028;
  96. }
  97. /**
  98. * Convert centimeters width to pixels
  99. *
  100. * @param integer $pValue Value in centimeters
  101. * @return integer Value in pixels
  102. */
  103. public static function centimetersToPixels($pValue = 0)
  104. {
  105. if ($pValue != 0) {
  106. return $pValue / 0.028;
  107. } else {
  108. return 0;
  109. }
  110. }
  111. /**
  112. * Convert centimeters width to twips
  113. *
  114. * @param integer $pValue
  115. */
  116. public static function centimetersToTwips($pValue = 0)
  117. {
  118. if ($pValue != 0) {
  119. return $pValue * 566.928;
  120. } else {
  121. return 0;
  122. }
  123. }
  124. /**
  125. * Convert twips width to centimeters
  126. *
  127. * @param integer $pValue
  128. */
  129. public static function twipsToCentimeters($pValue = 0)
  130. {
  131. if ($pValue != 0) {
  132. return $pValue / 566.928;
  133. } else {
  134. return 0;
  135. }
  136. }
  137. /**
  138. * Convert inches width to twips
  139. *
  140. * @param integer $pValue
  141. */
  142. public static function inchesToTwips($pValue = 0)
  143. {
  144. if ($pValue != 0) {
  145. return $pValue * 1440;
  146. } else {
  147. return 0;
  148. }
  149. }
  150. /**
  151. * Convert twips width to inches
  152. *
  153. * @param integer $pValue
  154. */
  155. public static function twipsToInches($pValue = 0)
  156. {
  157. if ($pValue != 0) {
  158. return $pValue / 1440;
  159. } else {
  160. return 0;
  161. }
  162. }
  163. /**
  164. * Convert twips width to pixels
  165. *
  166. * @param integer $pValue
  167. */
  168. public static function twipsToPixels($pValue = 0)
  169. {
  170. if ($pValue != 0) {
  171. return round($pValue / 15.873984);
  172. } else {
  173. return 0;
  174. }
  175. }
  176. /**
  177. * Convert HTML hexadecimal to RGB
  178. *
  179. * @param string $pValue HTML Color in hexadecimal
  180. * @return array Value in RGB
  181. */
  182. public static function htmlToRGB($pValue)
  183. {
  184. if ($pValue[0] == '#') {
  185. $pValue = substr($pValue, 1);
  186. }
  187. if (strlen($pValue) == 6) {
  188. list($colorR, $colorG, $colorB) = array($pValue[0] . $pValue[1], $pValue[2] . $pValue[3], $pValue[4] . $pValue[5]);
  189. } elseif (strlen($pValue) == 3) {
  190. list($colorR, $colorG, $colorB) = array($pValue[0] . $pValue[0], $pValue[1] . $pValue[1], $pValue[2] . $pValue[2]);
  191. } else {
  192. return false;
  193. }
  194. $colorR = hexdec($colorR);
  195. $colorG = hexdec($colorG);
  196. $colorB = hexdec($colorB);
  197. return array($colorR, $colorG, $colorB);
  198. }
  199. }