Image.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework;
  7. /**
  8. * Image handler library
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Image
  13. {
  14. /**
  15. * @var Image\Adapter\AdapterInterface
  16. */
  17. protected $_adapter;
  18. /**
  19. * @var null|string
  20. */
  21. protected $_fileName;
  22. /**
  23. * Constructor
  24. *
  25. * @param Image\Adapter\AdapterInterface $adapter
  26. * @param string|null $fileName
  27. */
  28. public function __construct(\Magento\Framework\Image\Adapter\AdapterInterface $adapter, $fileName = null)
  29. {
  30. $this->_adapter = $adapter;
  31. $this->_fileName = $fileName;
  32. if (isset($fileName)) {
  33. $this->open();
  34. }
  35. }
  36. /**
  37. * Opens an image and creates image handle
  38. *
  39. * @access public
  40. * @return void
  41. * @throws \Exception
  42. */
  43. public function open()
  44. {
  45. $this->_adapter->checkDependencies();
  46. if (!file_exists($this->_fileName)) {
  47. throw new \Exception("File '{$this->_fileName}' does not exist.");
  48. }
  49. $this->_adapter->open($this->_fileName);
  50. }
  51. /**
  52. * Display handled image in your browser
  53. *
  54. * @access public
  55. * @return void
  56. */
  57. public function display()
  58. {
  59. $this->_adapter->display();
  60. }
  61. /**
  62. * Save handled image into file
  63. *
  64. * @param null|string $destination Default value is NULL
  65. * @param null|string $newFileName Default value is NULL
  66. * @access public
  67. * @return void
  68. */
  69. public function save($destination = null, $newFileName = null)
  70. {
  71. $this->_adapter->save($destination, $newFileName);
  72. }
  73. /**
  74. * Rotate an image.
  75. *
  76. * @param int $angle
  77. * @access public
  78. * @return void
  79. */
  80. public function rotate($angle)
  81. {
  82. $this->_adapter->rotate($angle);
  83. }
  84. /**
  85. * Crop an image.
  86. *
  87. * @param int $top Default value is 0
  88. * @param int $left Default value is 0
  89. * @param int $right Default value is 0
  90. * @param int $bottom Default value is 0
  91. * @access public
  92. * @return void
  93. */
  94. public function crop($top = 0, $left = 0, $right = 0, $bottom = 0)
  95. {
  96. $this->_adapter->crop($top, $left, $right, $bottom);
  97. }
  98. /**
  99. * Resize an image
  100. *
  101. * @param int $width
  102. * @param int $height
  103. * @access public
  104. * @return void
  105. */
  106. public function resize($width, $height = null)
  107. {
  108. $this->_adapter->resize($width, $height);
  109. }
  110. /**
  111. * Get/set keepAspectRatio
  112. *
  113. * @param bool $value
  114. * @return bool
  115. */
  116. public function keepAspectRatio($value)
  117. {
  118. return $this->_adapter->keepAspectRatio($value);
  119. }
  120. /**
  121. * Get/set keepFrame
  122. *
  123. * @param bool $value
  124. * @return bool
  125. */
  126. public function keepFrame($value)
  127. {
  128. return $this->_adapter->keepFrame($value);
  129. }
  130. /**
  131. * Get/set keepTransparency
  132. *
  133. * @param bool $value
  134. * @return bool
  135. */
  136. public function keepTransparency($value)
  137. {
  138. return $this->_adapter->keepTransparency($value);
  139. }
  140. /**
  141. * Get/set constrainOnly
  142. *
  143. * @param bool $value
  144. * @return bool
  145. */
  146. public function constrainOnly($value)
  147. {
  148. return $this->_adapter->constrainOnly($value);
  149. }
  150. /**
  151. * Get/set backgroundColor
  152. *
  153. * @param null|array $value
  154. * @return array|null
  155. */
  156. public function backgroundColor($value)
  157. {
  158. return $this->_adapter->backgroundColor($value);
  159. }
  160. /**
  161. * Get/set quality, values in percentage from 0 to 100
  162. *
  163. * @param int $value
  164. * @return int
  165. */
  166. public function quality($value)
  167. {
  168. return $this->_adapter->quality($value);
  169. }
  170. /**
  171. * Adds watermark to our image.
  172. *
  173. * @param string $watermarkImage Absolute path to watermark image.
  174. * @param int $positionX Watermark X position.
  175. * @param int $positionY Watermark Y position.
  176. * @param int $watermarkImageOpacity Watermark image opacity.
  177. * @param bool $repeat Enable or disable watermark brick.
  178. * @access public
  179. * @throws \Exception
  180. * @return void
  181. */
  182. public function watermark(
  183. $watermarkImage,
  184. $positionX = 0,
  185. $positionY = 0,
  186. $watermarkImageOpacity = 30,
  187. $repeat = false
  188. ) {
  189. if (!file_exists($watermarkImage)) {
  190. throw new \Exception("Required file '{$watermarkImage}' does not exists.");
  191. }
  192. $this->_adapter->watermark($watermarkImage, $positionX, $positionY, $watermarkImageOpacity, $repeat);
  193. }
  194. /**
  195. * Get mime type of handled image
  196. *
  197. * @access public
  198. * @return string
  199. */
  200. public function getMimeType()
  201. {
  202. return $this->_adapter->getMimeType();
  203. }
  204. /**
  205. * Get image type of handled image
  206. *
  207. * @access public
  208. * @return int
  209. */
  210. public function getImageType()
  211. {
  212. return $this->_adapter->getImageType();
  213. }
  214. /**
  215. * Process
  216. *
  217. * @access public
  218. * @return void
  219. */
  220. public function process()
  221. {
  222. }
  223. /**
  224. * Instruction
  225. *
  226. * @access public
  227. * @return void
  228. */
  229. public function instruction()
  230. {
  231. }
  232. /**
  233. * Set image background color
  234. *
  235. * @param int $color
  236. * @access public
  237. * @return void
  238. */
  239. public function setImageBackgroundColor($color)
  240. {
  241. /** @noinspection PhpUndefinedFieldInspection */
  242. $this->_adapter->imageBackgroundColor = (int)$color;
  243. }
  244. /**
  245. * Set watermark position
  246. *
  247. * @param string $position
  248. * @return $this
  249. */
  250. public function setWatermarkPosition($position)
  251. {
  252. $this->_adapter->setWatermarkPosition($position);
  253. return $this;
  254. }
  255. /**
  256. * Set watermark image opacity
  257. *
  258. * @param int $imageOpacity
  259. * @return $this
  260. */
  261. public function setWatermarkImageOpacity($imageOpacity)
  262. {
  263. $this->_adapter->setWatermarkImageOpacity($imageOpacity);
  264. return $this;
  265. }
  266. /**
  267. * Set watermark width
  268. *
  269. * @param int $width
  270. * @return $this
  271. */
  272. public function setWatermarkWidth($width)
  273. {
  274. $this->_adapter->setWatermarkWidth($width);
  275. return $this;
  276. }
  277. /**
  278. * Set watermark height
  279. *
  280. * @param int $height
  281. * @return $this
  282. */
  283. public function setWatermarkHeight($height)
  284. {
  285. $this->_adapter->setWatermarkHeight($height);
  286. return $this;
  287. }
  288. /**
  289. * Retrieve original image width
  290. *
  291. * @return int|null
  292. */
  293. public function getOriginalWidth()
  294. {
  295. return $this->_adapter->getOriginalWidth();
  296. }
  297. /**
  298. * Retrieve original image height
  299. *
  300. * @return int|null
  301. */
  302. public function getOriginalHeight()
  303. {
  304. return $this->_adapter->getOriginalHeight();
  305. }
  306. /**
  307. * Create Image from string
  308. *
  309. * @param string $text
  310. * @param string $font Path to font file
  311. * @return $this
  312. */
  313. public function createPngFromString($text, $font = '')
  314. {
  315. $this->_adapter->createPngFromString($text, $font);
  316. return $this;
  317. }
  318. }