Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Media.php
Go to the documentation of this file.
1 <?php
7 
11 
19 {
24  const SWATCH_MEDIA_PATH = 'attribute/swatch';
25 
29  protected $mediaConfig;
30 
34  protected $mediaDirectory;
35 
41  protected $fileStorageDb = null;
42 
48  protected $storeManager;
49 
53  protected $imageFactory;
54 
58  protected $themeCollection;
59 
63  protected $viewConfig;
64 
68  protected $swatchImageTypes = ['swatch_image', 'swatch_thumb'];
69 
73  private $imageConfig;
74 
84  public function __construct(
85  \Magento\Catalog\Model\Product\Media\Config $mediaConfig,
86  \Magento\Framework\Filesystem $filesystem,
87  \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb,
89  \Magento\Framework\Image\Factory $imageFactory,
90  \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection,
91  \Magento\Framework\View\ConfigInterface $configInterface
92  ) {
93  $this->mediaConfig = $mediaConfig;
94  $this->fileStorageDb = $fileStorageDb;
95  $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
96  $this->storeManager = $storeManager;
97  $this->imageFactory = $imageFactory;
98  $this->themeCollection = $themeCollection;
99  $this->viewConfig = $configInterface;
100  }
101 
107  public function getSwatchAttributeImage($swatchType, $file)
108  {
109  $generationPath = $swatchType . '/' . $this->getFolderNameSize($swatchType) . $file;
110  $absoluteImagePath = $this->mediaDirectory
111  ->getAbsolutePath($this->getSwatchMediaPath() . '/' . $generationPath);
112  if (!file_exists($absoluteImagePath)) {
113  try {
114  $this->generateSwatchVariations($file);
115  } catch (\Exception $e) {
116  return '';
117  }
118  }
119  return $this->getSwatchMediaUrl() . '/' . $generationPath;
120  }
121 
128  public function moveImageFromTmp($file)
129  {
130  if (strrpos($file, '.tmp') == strlen($file) - 4) {
131  $file = substr($file, 0, strlen($file) - 4);
132  }
133  $destinationFile = $this->getUniqueFileName($file);
134 
136  $storageHelper = $this->fileStorageDb;
137 
138  if ($storageHelper->checkDbUsage()) {
139  $storageHelper->renameFile(
140  $this->mediaConfig->getTmpMediaShortUrl($file),
141  $this->mediaConfig->getMediaShortUrl($destinationFile)
142  );
143 
144  $this->mediaDirectory->delete($this->mediaConfig->getTmpMediaPath($file));
145  $this->mediaDirectory->delete($this->getAttributeSwatchPath($destinationFile));
146  } else {
147  $this->mediaDirectory->renameFile(
148  $this->mediaConfig->getTmpMediaPath($file),
149  $this->getAttributeSwatchPath($destinationFile)
150  );
151  }
152 
153  return str_replace('\\', '/', $destinationFile);
154  }
155 
162  protected function getUniqueFileName($file)
163  {
164  if ($this->fileStorageDb->checkDbUsage()) {
165  $destFile = $this->fileStorageDb->getUniqueFilename(
166  $this->mediaConfig->getBaseMediaUrlAddition(),
167  $file
168  );
169  } else {
170  $destFile = dirname($file) . '/' . \Magento\MediaStorage\Model\File\Uploader::getNewFileName(
171  $this->mediaDirectory->getAbsolutePath($this->getAttributeSwatchPath($file))
172  );
173  }
174 
175  return $destFile;
176  }
177 
185  {
186  $absoluteImagePath = $this->mediaDirectory->getAbsolutePath($this->getAttributeSwatchPath($imageUrl));
187  foreach ($this->swatchImageTypes as $swatchType) {
188  $imageConfig = $this->getImageConfig();
189  $swatchNamePath = $this->generateNamePath($imageConfig, $imageUrl, $swatchType);
190  $image = $this->imageFactory->create($absoluteImagePath);
192  $image->resize($imageConfig[$swatchType]['width'], $imageConfig[$swatchType]['height']);
193  $this->setupImageProperties($image, true);
194  $image->save($swatchNamePath['path_for_save'], $swatchNamePath['name']);
195  }
196  return $this;
197  }
198 
206  protected function setupImageProperties(\Magento\Framework\Image $image, $isSwatch = false)
207  {
208  $image->quality(100);
209  $image->constrainOnly(true);
210  $image->keepAspectRatio(true);
211  if ($isSwatch) {
212  $image->keepFrame(true);
213  $image->keepTransparency(true);
214  $image->backgroundColor([255, 255, 255]);
215  }
216  return $this;
217  }
218 
227  protected function generateNamePath($imageConfig, $imageUrl, $swatchType)
228  {
230  $absolutePath = $this->mediaDirectory->getAbsolutePath($this->getSwatchCachePath($swatchType));
231  return [
232  'path_for_save' => $absolutePath . $this->getFolderNameSize($swatchType, $imageConfig) . $fileName['path'],
233  'name' => $fileName['name']
234  ];
235  }
236 
244  public function getFolderNameSize($swatchType, $imageConfig = null)
245  {
246  if ($imageConfig === null) {
247  $imageConfig = $this->getImageConfig();
248  }
249  return $imageConfig[$swatchType]['width'] . 'x' . $imageConfig[$swatchType]['height'];
250  }
251 
257  public function getImageConfig()
258  {
259  if (!$this->imageConfig) {
260  $this->imageConfig = $this->viewConfig->getViewConfig()->getMediaEntities(
261  'Magento_Catalog',
263  );
264  }
265 
266  return $this->imageConfig;
267  }
268 
275  protected function prepareFileName($imageUrl)
276  {
277  $fileArray = explode('/', $imageUrl);
278  $fileName = array_pop($fileArray);
279  $filePath = implode('/', $fileArray);
280  return ['name' => $fileName, 'path' => $filePath];
281  }
282 
288  public function getSwatchMediaUrl()
289  {
290  return $this->storeManager
291  ->getStore()
292  ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . $this->getSwatchMediaPath();
293  }
294 
301  public function getAttributeSwatchPath($file)
302  {
303  return $this->getSwatchMediaPath() . '/' . $this->prepareFile($file);
304  }
305 
311  public function getSwatchMediaPath()
312  {
314  }
315 
322  public function getSwatchCachePath($swatchType)
323  {
324  return self::SWATCH_MEDIA_PATH . '/' . $swatchType . '/';
325  }
326 
333  protected function prepareFile($file)
334  {
335  return ltrim(str_replace('\\', '/', $file), '/');
336  }
337 }
getSwatchCachePath($swatchType)
Definition: Media.php:322
__construct(\Magento\Catalog\Model\Product\Media\Config $mediaConfig, \Magento\Framework\Filesystem $filesystem, \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Image\Factory $imageFactory, \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection, \Magento\Framework\View\ConfigInterface $configInterface)
Definition: Media.php:84
setupImageProperties(\Magento\Framework\Image $image, $isSwatch=false)
Definition: Media.php:206
getFolderNameSize($swatchType, $imageConfig=null)
Definition: Media.php:244
generateNamePath($imageConfig, $imageUrl, $swatchType)
Definition: Media.php:227
$fileName
Definition: translate.phtml:15
if($_imageTitle=$block->escapeHtml($block->getCurrentImage() ->getLabel())) $imageUrl
Definition: gallery.phtml:28
generateSwatchVariations($imageUrl)
Definition: Media.php:184
getSwatchAttributeImage($swatchType, $file)
Definition: Media.php:107
$filesystem
static getNewFileName($destinationFile)
Definition: Uploader.php:605