![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-catalog/Model/Product/Image/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Catalog\Model\Product\Image; use Magento\Catalog\Helper\Image as ImageHelper; use Magento\Catalog\Model\Product; use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection; use Magento\Framework\App\Area; use Magento\Framework\View\ConfigInterface; class Cache { /** * @var ConfigInterface */ protected $viewConfig; /** * @var ThemeCollection */ protected $themeCollection; /** * @var ImageHelper */ protected $imageHelper; /** * @var array */ protected $data = []; /** * @param ConfigInterface $viewConfig * @param ThemeCollection $themeCollection * @param ImageHelper $imageHelper */ public function __construct( ConfigInterface $viewConfig, ThemeCollection $themeCollection, ImageHelper $imageHelper ) { $this->viewConfig = $viewConfig; $this->themeCollection = $themeCollection; $this->imageHelper = $imageHelper; } /** * Retrieve view configuration data * * Collect data for 'Magento_Catalog' module from /etc/view.xml files. * * @return array */ protected function getData() { if (!$this->data) { /** @var \Magento\Theme\Model\Theme $theme */ foreach ($this->themeCollection->loadRegisteredThemes() as $theme) { $config = $this->viewConfig->getViewConfig([ 'area' => Area::AREA_FRONTEND, 'themeModel' => $theme, ]); $images = $config->getMediaEntities('Magento_Catalog', ImageHelper::MEDIA_TYPE_CONFIG_NODE); foreach ($images as $imageId => $imageData) { $this->data[$theme->getCode() . $imageId] = array_merge(['id' => $imageId], $imageData); } } } return $this->data; } /** * Resize product images and save results to image cache * * @param Product $product * @return $this */ public function generate(Product $product) { $galleryImages = $product->getMediaGalleryImages(); if ($galleryImages) { foreach ($galleryImages as $image) { foreach ($this->getData() as $imageData) { $this->processImageData($product, $imageData, $image->getFile()); } } } return $this; } /** * Process image data * * @param Product $product * @param array $imageData * @param string $file * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function processImageData(Product $product, array $imageData, $file) { $this->imageHelper->init($product, $imageData['id'], $imageData); $this->imageHelper->setImageFile($file); if (isset($imageData['aspect_ratio'])) { $this->imageHelper->keepAspectRatio($imageData['aspect_ratio']); } if (isset($imageData['frame'])) { $this->imageHelper->keepFrame($imageData['frame']); } if (isset($imageData['transparency'])) { $this->imageHelper->keepTransparency($imageData['transparency']); } if (isset($imageData['constrain'])) { $this->imageHelper->constrainOnly($imageData['constrain']); } if (isset($imageData['background'])) { $this->imageHelper->backgroundColor($imageData['background']); } $this->imageHelper->save(); return $this; } }