/vendor/magento/module-catalog/Model/Product/Image/Cache.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 130 lines · 73 code · 12 blank · 45 comment · 7 complexity · 17385c5aade555f19a8af16d10952548 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\Product\Image;
  7. use Magento\Catalog\Helper\Image as ImageHelper;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection;
  10. use Magento\Framework\App\Area;
  11. use Magento\Framework\View\ConfigInterface;
  12. class Cache
  13. {
  14. /**
  15. * @var ConfigInterface
  16. */
  17. protected $viewConfig;
  18. /**
  19. * @var ThemeCollection
  20. */
  21. protected $themeCollection;
  22. /**
  23. * @var ImageHelper
  24. */
  25. protected $imageHelper;
  26. /**
  27. * @var array
  28. */
  29. protected $data = [];
  30. /**
  31. * @param ConfigInterface $viewConfig
  32. * @param ThemeCollection $themeCollection
  33. * @param ImageHelper $imageHelper
  34. */
  35. public function __construct(
  36. ConfigInterface $viewConfig,
  37. ThemeCollection $themeCollection,
  38. ImageHelper $imageHelper
  39. ) {
  40. $this->viewConfig = $viewConfig;
  41. $this->themeCollection = $themeCollection;
  42. $this->imageHelper = $imageHelper;
  43. }
  44. /**
  45. * Retrieve view configuration data
  46. *
  47. * Collect data for 'Magento_Catalog' module from /etc/view.xml files.
  48. *
  49. * @return array
  50. */
  51. protected function getData()
  52. {
  53. if (!$this->data) {
  54. /** @var \Magento\Theme\Model\Theme $theme */
  55. foreach ($this->themeCollection->loadRegisteredThemes() as $theme) {
  56. $config = $this->viewConfig->getViewConfig([
  57. 'area' => Area::AREA_FRONTEND,
  58. 'themeModel' => $theme,
  59. ]);
  60. $images = $config->getMediaEntities('Magento_Catalog', ImageHelper::MEDIA_TYPE_CONFIG_NODE);
  61. foreach ($images as $imageId => $imageData) {
  62. $this->data[$theme->getCode() . $imageId] = array_merge(['id' => $imageId], $imageData);
  63. }
  64. }
  65. }
  66. return $this->data;
  67. }
  68. /**
  69. * Resize product images and save results to image cache
  70. *
  71. * @param Product $product
  72. * @return $this
  73. */
  74. public function generate(Product $product)
  75. {
  76. $galleryImages = $product->getMediaGalleryImages();
  77. if ($galleryImages) {
  78. foreach ($galleryImages as $image) {
  79. foreach ($this->getData() as $imageData) {
  80. $this->processImageData($product, $imageData, $image->getFile());
  81. }
  82. }
  83. }
  84. return $this;
  85. }
  86. /**
  87. * Process image data
  88. *
  89. * @param Product $product
  90. * @param array $imageData
  91. * @param string $file
  92. * @return $this
  93. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  94. * @SuppressWarnings(PHPMD.NPathComplexity)
  95. */
  96. protected function processImageData(Product $product, array $imageData, $file)
  97. {
  98. $this->imageHelper->init($product, $imageData['id'], $imageData);
  99. $this->imageHelper->setImageFile($file);
  100. if (isset($imageData['aspect_ratio'])) {
  101. $this->imageHelper->keepAspectRatio($imageData['aspect_ratio']);
  102. }
  103. if (isset($imageData['frame'])) {
  104. $this->imageHelper->keepFrame($imageData['frame']);
  105. }
  106. if (isset($imageData['transparency'])) {
  107. $this->imageHelper->keepTransparency($imageData['transparency']);
  108. }
  109. if (isset($imageData['constrain'])) {
  110. $this->imageHelper->constrainOnly($imageData['constrain']);
  111. }
  112. if (isset($imageData['background'])) {
  113. $this->imageHelper->backgroundColor($imageData['background']);
  114. }
  115. $this->imageHelper->save();
  116. return $this;
  117. }
  118. }