Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cache.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Catalog\Helper\Image as ImageHelper;
13 
14 class Cache
15 {
19  protected $viewConfig;
20 
24  protected $themeCollection;
25 
29  protected $imageHelper;
30 
34  protected $data = [];
35 
41  public function __construct(
43  ThemeCollection $themeCollection,
44  ImageHelper $imageHelper
45  ) {
46  $this->viewConfig = $viewConfig;
47  $this->themeCollection = $themeCollection;
48  $this->imageHelper = $imageHelper;
49  }
50 
58  protected function getData()
59  {
60  if (!$this->data) {
62  foreach ($this->themeCollection->loadRegisteredThemes() as $theme) {
63  $config = $this->viewConfig->getViewConfig([
64  'area' => Area::AREA_FRONTEND,
65  'themeModel' => $theme,
66  ]);
67  $images = $config->getMediaEntities('Magento_Catalog', ImageHelper::MEDIA_TYPE_CONFIG_NODE);
68  foreach ($images as $imageId => $imageData) {
69  $this->data[$theme->getCode() . $imageId] = array_merge(['id' => $imageId], $imageData);
70  }
71  }
72  }
73  return $this->data;
74  }
75 
82  public function generate(Product $product)
83  {
84  $galleryImages = $product->getMediaGalleryImages();
85  if ($galleryImages) {
86  foreach ($galleryImages as $image) {
87  foreach ($this->getData() as $imageData) {
88  $this->processImageData($product, $imageData, $image->getFile());
89  }
90  }
91  }
92  return $this;
93  }
94 
105  protected function processImageData(Product $product, array $imageData, $file)
106  {
107  $this->imageHelper->init($product, $imageData['id'], $imageData);
108  $this->imageHelper->setImageFile($file);
109 
110  if (isset($imageData['aspect_ratio'])) {
111  $this->imageHelper->keepAspectRatio($imageData['aspect_ratio']);
112  }
113  if (isset($imageData['frame'])) {
114  $this->imageHelper->keepFrame($imageData['frame']);
115  }
116  if (isset($imageData['transparency'])) {
117  $this->imageHelper->keepTransparency($imageData['transparency']);
118  }
119  if (isset($imageData['constrain'])) {
120  $this->imageHelper->constrainOnly($imageData['constrain']);
121  }
122  if (isset($imageData['background'])) {
123  $this->imageHelper->backgroundColor($imageData['background']);
124  }
125 
126  $this->imageHelper->save();
127 
128  return $this;
129  }
130 }
getData($key='', $index=null)
Definition: DataObject.php:119
$config
Definition: fraud_order.php:17
processImageData(Product $product, array $imageData, $file)
Definition: Cache.php:105
__construct(ConfigInterface $viewConfig, ThemeCollection $themeCollection, ImageHelper $imageHelper)
Definition: Cache.php:41
$theme