MemoryImage.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * Copyright (c) 2011 PHPWord
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPWord
  22. * @package PHPWord
  23. * @copyright Copyright (c) 010 PHPWord
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version Beta 0.6.3, 08.07.2011
  26. */
  27. /**
  28. * PHPWord_Section_MemoryImage
  29. *
  30. * @category PHPWord
  31. * @package PHPWord_Section
  32. * @copyright Copyright (c) 2011 PHPWord
  33. */
  34. class PHPWord_Section_MemoryImage {
  35. /**
  36. * Image Src
  37. *
  38. * @var string
  39. */
  40. private $_src;
  41. /**
  42. * Image Style
  43. *
  44. * @var PHPWord_Style_Image
  45. */
  46. private $_style;
  47. /**
  48. * Image Relation ID
  49. *
  50. * @var string
  51. */
  52. private $_rId;
  53. /**
  54. * Image Type
  55. *
  56. * @var string
  57. */
  58. private $_imageType;
  59. /**
  60. * Image Create function
  61. *
  62. * @var string
  63. */
  64. private $_imageCreateFunc;
  65. /**
  66. * Image function
  67. *
  68. * @var string
  69. */
  70. private $_imageFunc;
  71. /**
  72. * Image function
  73. *
  74. * @var string
  75. */
  76. private $_imageExtension;
  77. /**
  78. * Create a new Image
  79. *
  80. * @param string $src
  81. * @param mixed style
  82. */
  83. public function __construct($src, $style = null) {
  84. $imgData = getimagesize($src);
  85. $this->_imageType = $imgData['mime'];
  86. $_supportedImageTypes = array('image/jpeg', 'image/gif', 'image/png');
  87. if(in_array($this->_imageType, $_supportedImageTypes)) {
  88. $this->_src = $src;
  89. $this->_style = new PHPWord_Style_Image();
  90. if(!is_null($style) && is_array($style)) {
  91. foreach($style as $key => $value) {
  92. if(substr($key, 0, 1) != '_') {
  93. $key = '_'.$key;
  94. }
  95. $this->_style->setStyleValue($key, $value);
  96. }
  97. }
  98. if($this->_style->getWidth() == null && $this->_style->getHeight() == null) {
  99. $this->_style->setWidth($imgData[0]);
  100. $this->_style->setHeight($imgData[1]);
  101. }
  102. $this->_setFunctions();
  103. return $this;
  104. } else {
  105. return false;
  106. }
  107. }
  108. /**
  109. * Set Functions
  110. */
  111. private function _setFunctions() {
  112. switch($this->_imageType) {
  113. case 'image/png':
  114. $this->_imageCreateFunc = 'imagecreatefrompng';
  115. $this->_imageFunc = 'imagepng';
  116. $this->_imageExtension = 'png';
  117. break;
  118. case 'image/gif':
  119. $this->_imageCreateFunc = 'imagecreatefromgif';
  120. $this->_imageFunc = 'imagegif';
  121. $this->_imageExtension = 'gif';
  122. break;
  123. case 'image/jpeg': case 'image/jpg':
  124. $this->_imageCreateFunc = 'imagecreatefromjpeg';
  125. $this->_imageFunc = 'imagejpeg';
  126. $this->_imageExtension = 'jpg';
  127. break;
  128. }
  129. }
  130. /**
  131. * Get Image style
  132. *
  133. * @return PHPWord_Style_Image
  134. */
  135. public function getStyle() {
  136. return $this->_style;
  137. }
  138. /**
  139. * Get Image Relation ID
  140. *
  141. * @return int
  142. */
  143. public function getRelationId() {
  144. return $this->_rId;
  145. }
  146. /**
  147. * Set Image Relation ID
  148. *
  149. * @param int $rId
  150. */
  151. public function setRelationId($rId) {
  152. $this->_rId = $rId;
  153. }
  154. /**
  155. * Get Image Source
  156. *
  157. * @return string
  158. */
  159. public function getSource() {
  160. return $this->_src;
  161. }
  162. /**
  163. * Get Image Media ID
  164. *
  165. * @return string
  166. */
  167. public function getMediaId() {
  168. return md5($this->_src);
  169. }
  170. /**
  171. * Get Image Type
  172. *
  173. * @return string
  174. */
  175. public function getImageType() {
  176. return $this->_imageType;
  177. }
  178. /**
  179. * Get Image Create Function
  180. *
  181. * @return string
  182. */
  183. public function getImageCreateFunction() {
  184. return $this->_imageCreateFunc;
  185. }
  186. /**
  187. * Get Image Function
  188. *
  189. * @return string
  190. */
  191. public function getImageFunction() {
  192. return $this->_imageFunc;
  193. }
  194. /**
  195. * Get Image Extension
  196. *
  197. * @return string
  198. */
  199. public function getImageExtension() {
  200. return $this->_imageExtension;
  201. }
  202. }
  203. ?>