Image.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\View\Asset;
  7. use Magento\Catalog\Model\Product\Media\ConfigInterface;
  8. use Magento\Framework\Encryption\Encryptor;
  9. use Magento\Framework\Encryption\EncryptorInterface;
  10. use Magento\Framework\View\Asset\ContextInterface;
  11. use Magento\Framework\View\Asset\LocalInterface;
  12. /**
  13. * A locally available image file asset that can be referred with a file path
  14. *
  15. * This class is a value object with lazy loading of some of its data (content, physical file path)
  16. */
  17. class Image implements LocalInterface
  18. {
  19. /**
  20. * Image type of image (thumbnail,small_image,image,swatch_image,swatch_thumb)
  21. *
  22. * @var string
  23. */
  24. private $sourceContentType;
  25. /**
  26. * @var string
  27. */
  28. private $filePath;
  29. /**
  30. * @var string
  31. */
  32. private $contentType = 'image';
  33. /**
  34. * @var ContextInterface
  35. */
  36. private $context;
  37. /**
  38. * Misc image params depend on size, transparency, quality, watermark etc.
  39. *
  40. * @var array
  41. */
  42. private $miscParams;
  43. /**
  44. * @var ConfigInterface
  45. */
  46. private $mediaConfig;
  47. /**
  48. * @var EncryptorInterface
  49. */
  50. private $encryptor;
  51. /**
  52. * Image constructor.
  53. *
  54. * @param ConfigInterface $mediaConfig
  55. * @param ContextInterface $context
  56. * @param EncryptorInterface $encryptor
  57. * @param string $filePath
  58. * @param array $miscParams
  59. */
  60. public function __construct(
  61. ConfigInterface $mediaConfig,
  62. ContextInterface $context,
  63. EncryptorInterface $encryptor,
  64. $filePath,
  65. array $miscParams
  66. ) {
  67. if (isset($miscParams['image_type'])) {
  68. $this->sourceContentType = $miscParams['image_type'];
  69. unset($miscParams['image_type']);
  70. } else {
  71. $this->sourceContentType = $this->contentType;
  72. }
  73. $this->mediaConfig = $mediaConfig;
  74. $this->context = $context;
  75. $this->filePath = $filePath;
  76. $this->miscParams = $miscParams;
  77. $this->encryptor = $encryptor;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getUrl()
  83. {
  84. return $this->context->getBaseUrl() . DIRECTORY_SEPARATOR . $this->getImageInfo();
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getContentType()
  90. {
  91. return $this->contentType;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function getPath()
  97. {
  98. return $this->context->getPath() . DIRECTORY_SEPARATOR . $this->getImageInfo();
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function getSourceFile()
  104. {
  105. return $this->mediaConfig->getBaseMediaPath()
  106. . DIRECTORY_SEPARATOR . ltrim($this->getFilePath(), DIRECTORY_SEPARATOR);
  107. }
  108. /**
  109. * Get source content type
  110. *
  111. * @return string
  112. */
  113. public function getSourceContentType()
  114. {
  115. return $this->sourceContentType;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function getContent()
  121. {
  122. return null;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function getFilePath()
  128. {
  129. return $this->filePath;
  130. }
  131. /**
  132. * {@inheritdoc}
  133. * @return ContextInterface
  134. */
  135. public function getContext()
  136. {
  137. return $this->context;
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function getModule()
  143. {
  144. return 'cache';
  145. }
  146. /**
  147. * Retrieve part of path based on misc params
  148. *
  149. * @return string
  150. */
  151. private function getMiscPath()
  152. {
  153. return $this->encryptor->hash(
  154. implode('_', $this->convertToReadableFormat($this->miscParams)),
  155. Encryptor::HASH_VERSION_MD5
  156. );
  157. }
  158. /**
  159. * Generate path from image info
  160. *
  161. * @return string
  162. */
  163. private function getImageInfo()
  164. {
  165. $path = $this->getModule()
  166. . DIRECTORY_SEPARATOR . $this->getMiscPath()
  167. . DIRECTORY_SEPARATOR . $this->getFilePath();
  168. return preg_replace('|\Q'. DIRECTORY_SEPARATOR . '\E+|', DIRECTORY_SEPARATOR, $path);
  169. }
  170. /**
  171. * Converting bool into a string representation
  172. * @param $miscParams
  173. * @return array
  174. */
  175. private function convertToReadableFormat($miscParams)
  176. {
  177. $miscParams['image_height'] = 'h:' . ($miscParams['image_height'] ?? 'empty');
  178. $miscParams['image_width'] = 'w:' . ($miscParams['image_width'] ?? 'empty');
  179. $miscParams['quality'] = 'q:' . ($miscParams['quality'] ?? 'empty');
  180. $miscParams['angle'] = 'r:' . ($miscParams['angle'] ?? 'empty');
  181. $miscParams['keep_aspect_ratio'] = (isset($miscParams['keep_aspect_ratio']) ? '' : 'non') . 'proportional';
  182. $miscParams['keep_frame'] = (isset($miscParams['keep_frame']) ? '' : 'no') . 'frame';
  183. $miscParams['keep_transparency'] = (isset($miscParams['keep_transparency']) ? '' : 'no') . 'transparency';
  184. $miscParams['constrain_only'] = (isset($miscParams['constrain_only']) ? 'do' : 'not') . 'constrainonly';
  185. $miscParams['background'] = isset($miscParams['background'])
  186. ? 'rgb' . implode(',', $miscParams['background'])
  187. : 'nobackground';
  188. return $miscParams;
  189. }
  190. }