Image.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Metadata\Form;
  7. use Magento\Customer\Api\AddressMetadataInterface;
  8. use Magento\Customer\Api\CustomerMetadataInterface;
  9. use Magento\Customer\Model\FileProcessor;
  10. use Magento\Framework\Api\ArrayObjectSearch;
  11. use Magento\Framework\Api\Data\ImageContentInterface;
  12. use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
  13. use Magento\Framework\App\ObjectManager;
  14. use Magento\Framework\File\UploaderFactory;
  15. use Magento\Framework\Filesystem;
  16. /**
  17. * Metadata for form image field
  18. *
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class Image extends File
  22. {
  23. /**
  24. * @var ImageContentInterfaceFactory
  25. */
  26. private $imageContentFactory;
  27. /**
  28. * Constructor
  29. *
  30. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  31. * @param \Psr\Log\LoggerInterface $logger
  32. * @param \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute
  33. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  34. * @param null|string $value
  35. * @param string $entityTypeCode
  36. * @param bool $isAjax
  37. * @param \Magento\Framework\Url\EncoderInterface $urlEncoder
  38. * @param \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $fileValidator
  39. * @param Filesystem $fileSystem
  40. * @param UploaderFactory $uploaderFactory
  41. * @param \Magento\Customer\Model\FileProcessorFactory|null $fileProcessorFactory
  42. * @param \Magento\Framework\Api\Data\ImageContentInterfaceFactory|null $imageContentInterfaceFactory
  43. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  44. */
  45. public function __construct(
  46. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  47. \Psr\Log\LoggerInterface $logger,
  48. \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute,
  49. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  50. $value,
  51. $entityTypeCode,
  52. $isAjax,
  53. \Magento\Framework\Url\EncoderInterface $urlEncoder,
  54. \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $fileValidator,
  55. Filesystem $fileSystem,
  56. UploaderFactory $uploaderFactory,
  57. \Magento\Customer\Model\FileProcessorFactory $fileProcessorFactory = null,
  58. \Magento\Framework\Api\Data\ImageContentInterfaceFactory $imageContentInterfaceFactory = null
  59. ) {
  60. parent::__construct(
  61. $localeDate,
  62. $logger,
  63. $attribute,
  64. $localeResolver,
  65. $value,
  66. $entityTypeCode,
  67. $isAjax,
  68. $urlEncoder,
  69. $fileValidator,
  70. $fileSystem,
  71. $uploaderFactory,
  72. $fileProcessorFactory
  73. );
  74. $this->imageContentFactory = $imageContentInterfaceFactory ?: ObjectManager::getInstance()
  75. ->get(\Magento\Framework\Api\Data\ImageContentInterfaceFactory::class);
  76. }
  77. /**
  78. * Validate file by attribute validate rules
  79. *
  80. * Return array of errors
  81. *
  82. * @param array $value
  83. * @return string[]
  84. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  85. * @SuppressWarnings(PHPMD.NPathComplexity)
  86. */
  87. protected function _validateByRules($value)
  88. {
  89. $label = $value['name'];
  90. $rules = $this->getAttribute()->getValidationRules();
  91. $imageProp = @getimagesize($value['tmp_name']);
  92. if (!$this->_isUploadedFile($value['tmp_name']) || !$imageProp) {
  93. return [__('"%1" is not a valid file.', $label)];
  94. }
  95. $allowImageTypes = [1 => 'gif', 2 => 'jpg', 3 => 'png'];
  96. if (!isset($allowImageTypes[$imageProp[2]])) {
  97. return [__('"%1" is not a valid image format.', $label)];
  98. }
  99. // modify image name
  100. $extension = pathinfo($value['name'], PATHINFO_EXTENSION);
  101. if ($extension != $allowImageTypes[$imageProp[2]]) {
  102. $value['name'] = pathinfo($value['name'], PATHINFO_FILENAME) . '.' . $allowImageTypes[$imageProp[2]];
  103. }
  104. $maxFileSize = ArrayObjectSearch::getArrayElementByName(
  105. $rules,
  106. 'max_file_size'
  107. );
  108. $errors = [];
  109. if ($maxFileSize !== null) {
  110. $size = $value['size'];
  111. if ($maxFileSize < $size) {
  112. $errors[] = __('"%1" exceeds the allowed file size.', $label);
  113. }
  114. }
  115. $maxImageWidth = ArrayObjectSearch::getArrayElementByName(
  116. $rules,
  117. 'max_image_width'
  118. );
  119. if ($maxImageWidth !== null) {
  120. if ($maxImageWidth < $imageProp[0]) {
  121. $r = $maxImageWidth;
  122. $errors[] = __('"%1" width exceeds allowed value of %2 px.', $label, $r);
  123. }
  124. }
  125. $maxImageHeight = ArrayObjectSearch::getArrayElementByName(
  126. $rules,
  127. 'max_image_height'
  128. );
  129. if ($maxImageHeight !== null) {
  130. if ($maxImageHeight < $imageProp[1]) {
  131. $r = $maxImageHeight;
  132. $errors[] = __('"%1" height exceeds allowed value of %2 px.', $label, $r);
  133. }
  134. }
  135. return $errors;
  136. }
  137. /**
  138. * Process file uploader UI component data
  139. *
  140. * @param array $value
  141. * @return bool|int|ImageContentInterface|string
  142. */
  143. protected function processUiComponentValue(array $value)
  144. {
  145. if ($this->_entityTypeCode == AddressMetadataInterface::ENTITY_TYPE_ADDRESS) {
  146. $result = $this->processCustomerAddressValue($value);
  147. return $result;
  148. }
  149. if ($this->_entityTypeCode == CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER) {
  150. $result = $this->processCustomerValue($value);
  151. return $result;
  152. }
  153. return $this->_value;
  154. }
  155. /**
  156. * Process file uploader UI component data for customer_address entity
  157. *
  158. * @param array $value
  159. * @return string
  160. */
  161. protected function processCustomerAddressValue(array $value)
  162. {
  163. $result = $this->getFileProcessor()->moveTemporaryFile($value['file']);
  164. return $result;
  165. }
  166. /**
  167. * Process file uploader UI component data for customer entity
  168. *
  169. * @param array $value
  170. * @return bool|int|ImageContentInterface|string
  171. */
  172. protected function processCustomerValue(array $value)
  173. {
  174. $temporaryFile = FileProcessor::TMP_DIR . '/' . ltrim($value['file'], '/');
  175. if ($this->getFileProcessor()->isExist($temporaryFile)) {
  176. $base64EncodedData = $this->getFileProcessor()->getBase64EncodedData($temporaryFile);
  177. /** @var ImageContentInterface $imageContentDataObject */
  178. $imageContentDataObject = $this->imageContentFactory->create()
  179. ->setName($value['name'])
  180. ->setBase64EncodedData($base64EncodedData)
  181. ->setType($value['type']);
  182. // Remove temporary file
  183. $this->getFileProcessor()->removeUploadedFile($temporaryFile);
  184. return $imageContentDataObject;
  185. }
  186. return $this->_value;
  187. }
  188. }