CreateImage.inc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. /**
  3. * Create images
  4. *
  5. * @category Phpdocx
  6. * @package elements
  7. * @copyright Copyright (c) 2009-2011 Narcea Producciones Multimedia S.L.
  8. * (http://www.2mdc.com)
  9. * @license LGPL
  10. * @version 1.0
  11. * @link http://www.phpdocx.com
  12. * @since File available since Release 1.0
  13. */
  14. include_once dirname(__FILE__) . '/CreateElement.inc';
  15. /**
  16. * Create images
  17. *
  18. * @category Phpdocx
  19. * @package elements
  20. * @copyright Copyright (c) 2009-2011 Narcea Producciones Multimedia S.L.
  21. * (http://www.2mdc.com)
  22. * @license http://www.phpdocx.com/wp-content/themes/lightword/pro_license.php
  23. * @version 1.0
  24. * @link http://www.phpdocx.com
  25. * @since Class available since Release 1.0
  26. */
  27. class CreateImage extends CreateElement
  28. {
  29. const NAMESPACEWORD = 'wp';
  30. const NAMESPACEWORD1 = 'a';
  31. const NAMESPACEWORD2 = 'pic';
  32. const CONSTWORD = 360000;
  33. const TAMBORDER = 12700;
  34. /**
  35. * @access private
  36. * @var CreateImage
  37. * @static
  38. */
  39. private static $_instance = NULL;
  40. /**
  41. *
  42. * @access private
  43. * @var string
  44. */
  45. private $_name;
  46. /**
  47. *
  48. * @access private
  49. * @var int
  50. */
  51. private $_rId;
  52. /**
  53. *
  54. * @access private
  55. * @var string
  56. */
  57. private $_ajusteTexto;
  58. /**
  59. *
  60. * @access private
  61. * @var int
  62. */
  63. private $_sizeX;
  64. /**
  65. *
  66. * @access private
  67. * @var int
  68. */
  69. private $_sizeY;
  70. /**
  71. *
  72. * @access private
  73. * @var int
  74. */
  75. private $_spacingTop;
  76. /**
  77. *
  78. * @access private
  79. * @var int
  80. */
  81. private $_spacingBottom;
  82. /**
  83. *
  84. * @access private
  85. * @var int
  86. */
  87. private $_spacingLeft;
  88. /**
  89. *
  90. * @access private
  91. * @var int
  92. */
  93. private $_spacingRight;
  94. /**
  95. *
  96. * @access private
  97. * @var int
  98. */
  99. private $_jc;
  100. /**
  101. *
  102. * @access private
  103. * @var string
  104. */
  105. private $_border;
  106. /**
  107. *
  108. * @access private
  109. * @var string
  110. */
  111. private $_borderDiscontinuo;
  112. /**
  113. *
  114. * @access private
  115. * @var int
  116. */
  117. private $_scaling;
  118. /**
  119. * Construct
  120. *
  121. * @access public
  122. */
  123. public function __construct()
  124. {
  125. $this->_name = '';
  126. $this->_rId = '';
  127. $this->_ajusteTexto = '';
  128. $this->_sizeX = '';
  129. $this->_sizeY = '';
  130. $this->_spacingTop = '';
  131. $this->_spacingBottom = '';
  132. $this->_spacingLeft = '';
  133. $this->_spacingRight = '';
  134. $this->_jc = '';
  135. $this->_border = '';
  136. $this->_borderDiscontinuo = '';
  137. $this->_scaling = '';
  138. }
  139. /**
  140. * Destruct
  141. *
  142. * @access public
  143. */
  144. public function __destruct()
  145. {
  146. }
  147. /**
  148. *
  149. * @return string
  150. * @access public
  151. */
  152. public function __toString()
  153. {
  154. return $this->_xml;
  155. }
  156. /**
  157. *
  158. * @return CreateImage
  159. * @access public
  160. * @static
  161. */
  162. public static function getInstance()
  163. {
  164. if (self::$_instance == NULL) {
  165. self::$_instance = new CreateImage();
  166. }
  167. return self::$_instance;
  168. }
  169. /**
  170. * Setter. Name
  171. *
  172. * @access public
  173. * @param string $name
  174. */
  175. public function setName($name)
  176. {
  177. $this->_name = $name;
  178. }
  179. /**
  180. * Getter. Name
  181. *
  182. * @access public
  183. * @return <type>
  184. */
  185. public function getName()
  186. {
  187. return $this->_name;
  188. }
  189. /**
  190. * Setter. Rid
  191. *
  192. * @access public
  193. * @param string $rId
  194. */
  195. public function setRId($rId)
  196. {
  197. $this->_rId = $rId;
  198. }
  199. /**
  200. * Getter. Rid
  201. *
  202. * @access public
  203. * @return <type>
  204. */
  205. public function getRId()
  206. {
  207. return $this->_rId;
  208. }
  209. /**
  210. * Create image
  211. *
  212. * @access public
  213. * @param array $args[0]
  214. */
  215. public function createImage()
  216. {
  217. $this->_xml = '';
  218. $this->_name = '';
  219. $this->_rId = '';
  220. $args = func_get_args();
  221. if (!empty($args[0]['rId']) && (!empty($args[0]['name']))) {
  222. $arr = array();
  223. $atributos = getimagesize($args[0]['name']);
  224. $ajusteTexto = 0;
  225. if (!empty($args[0]['sizeX'])) {
  226. $tamPxX = $args[0]['sizeX'];
  227. } elseif (!empty($args[0]['scaling'])) {
  228. $tamPxX = $atributos[0] * $args[0]['scaling'] / 100;
  229. } else {
  230. $tamPxX = $atributos[0];
  231. }
  232. if (!empty($args[0]['scaling'])) {
  233. $tamPxY = $atributos[1] * $args[0]['scaling'] / 100;
  234. } elseif (!empty($args[0]['sizeY'])) {
  235. $tamPxY = $args[0]['sizeY'];
  236. } else {
  237. $tamPxY = $atributos[1];
  238. }
  239. $this->setName($args[0]['name']);
  240. $this->setRId($args[0]['rId']);
  241. $top = '0';
  242. $bottom = '0';
  243. $left = '0';
  244. $right = '0';
  245. switch ($atributos['mime']) {
  246. case 'image/png':
  247. list($dpiX, $dpiY) =
  248. $this->get_dpi_png($args[0]['name']);
  249. $tamWordX =
  250. round($tamPxX * 100 / $dpiX * CreateImage::CONSTWORD);
  251. $tamWordY =
  252. round($tamPxY * 100 / $dpiY * CreateImage::CONSTWORD);
  253. break;
  254. case 'image/jpg':
  255. case 'image/jpeg':
  256. list($dpiX, $dpiY) = $this->get_dpi_jpg($args[0]['name']);
  257. $tamWordX = round(
  258. $tamPxX * 2.54 /
  259. $dpiX * CreateImage::CONSTWORD
  260. );
  261. $tamWordY = round(
  262. $tamPxY * 2.54 /
  263. $dpiY * CreateImage::CONSTWORD
  264. );
  265. break;
  266. case 'image/gif':
  267. $tamWordX = round(
  268. $tamPxX * 2.54 /
  269. 96 * CreateImage::CONSTWORD
  270. );
  271. $tamWordY = round(
  272. $tamPxY * 2.54 /
  273. 96 * CreateImage::CONSTWORD
  274. );
  275. break;
  276. }
  277. $align = '';
  278. $this->generateP();
  279. $this->generateR();
  280. $this->generateRPR();
  281. $this->generateNOPROOF();
  282. $this->generateDRAWING();
  283. $this->generateINLINE();
  284. $this->generateEXTENT($tamWordX, $tamWordY);
  285. $this->generateEFFECTEXTENT($left, $top, $right, $bottom);
  286. $this->generateDOCPR();
  287. $this->generateCNVGRAPHICFRAMEPR();
  288. $this->generateGRAPHICPRAMELOCKS(1);
  289. $this->generateGRAPHIC();
  290. $this->generateGRAPHICDATA();
  291. $this->generatePIC();
  292. $this->generateNVPICPR();
  293. $this->generateCNVPR();
  294. $this->generateCNVPICPR();
  295. $this->generateBLIPFILL();
  296. $this->generateBLIP();
  297. $this->generateSTRETCH();
  298. $this->generateFILLRECT();
  299. $this->generateSPPR();
  300. $this->generateXFRM();
  301. $this->generateOFF();
  302. $this->generateEXT($tamWordX, $tamWordY);
  303. $this->generatePRSTGEOM();
  304. $this->generateAVLST();
  305. $this->cleanTemplate();
  306. } else {
  307. echo 'There was an error adding the image';
  308. }
  309. }
  310. /**
  311. * Get image jpg dpi
  312. *
  313. * @access private
  314. * @param string $filename
  315. * @return array
  316. */
  317. private function get_dpi_jpg($filename)
  318. {
  319. $a = fopen($filename, 'r');
  320. $string = fread($a, 20);
  321. fclose($a);
  322. $data = bin2hex(substr($string, 14, 4));
  323. $x = substr($data, 0, 4);
  324. $y = substr($data, 4, 4);
  325. return array(hexdec($x), hexdec($y));
  326. }
  327. /**
  328. * Get image png dpi
  329. *
  330. * @access private
  331. * @param string $filename
  332. * @return array
  333. */
  334. private function get_dpi_png($filename)
  335. {
  336. $a = fopen($filename, 'r');
  337. $string = fread($a, 1000);
  338. $aux = strpos($string, 'pHYs');
  339. $data = bin2hex(substr($string, $aux + strlen('pHYs'), 16));
  340. fclose($a);
  341. $x = substr($data, 0, 8);
  342. $y = substr($data, 8, 8);
  343. return array(hexdec($x), hexdec($y));
  344. }
  345. }