Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Media Class Reference
Inheritance diagram for Media:
AbstractHelper

Public Member Functions

 __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)
 
 getSwatchAttributeImage ($swatchType, $file)
 
 generateSwatchVariations ($imageUrl)
 
 getFolderNameSize ($swatchType, $imageConfig=null)
 
 getImageConfig ()
 
 getSwatchMediaUrl ()
 
 getAttributeSwatchPath ($file)
 
 getSwatchMediaPath ()
 
 getSwatchCachePath ($swatchType)
 
- Public Member Functions inherited from AbstractHelper
 __construct (Context $context)
 
 isModuleOutputEnabled ($moduleName=null)
 

Data Fields

const SWATCH_MEDIA_PATH = 'attribute/swatch'
 

Protected Member Functions

 getUniqueFileName ($file)
 
 setupImageProperties (\Magento\Framework\Image $image, $isSwatch=false)
 
 generateNamePath ($imageConfig, $imageUrl, $swatchType)
 
 prepareFileName ($imageUrl)
 
 prepareFile ($file)
 
- Protected Member Functions inherited from AbstractHelper
 _getRequest ()
 
 _getModuleName ()
 
 _getUrl ($route, $params=[])
 

Protected Attributes

 $mediaConfig
 
 $mediaDirectory
 
 $fileStorageDb = null
 
 $storeManager
 
 $imageFactory
 
 $themeCollection
 
 $viewConfig
 
 $swatchImageTypes = ['swatch_image', 'swatch_thumb']
 
- Protected Attributes inherited from AbstractHelper
 $_moduleName
 
 $_request
 
 $_moduleManager
 
 $_logger
 
 $_urlBuilder
 
 $_httpHeader
 
 $_eventManager
 
 $_remoteAddress
 
 $urlEncoder
 
 $urlDecoder
 
 $scopeConfig
 
 $_cacheConfig
 

Detailed Description

Helper to move images from tmp to catalog directory @api @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 18 of file Media.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\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 at line 84 of file Media.php.

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  }
$filesystem

Member Function Documentation

◆ generateNamePath()

generateNamePath (   $imageConfig,
  $imageUrl,
  $swatchType 
)
protected

Generate swatch path and name for saving

Parameters
array$imageConfig
string$imageUrl
string$swatchType
Returns
array

Definition at line 227 of file Media.php.

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  }
getSwatchCachePath($swatchType)
Definition: Media.php:322
getFolderNameSize($swatchType, $imageConfig=null)
Definition: Media.php:244
$fileName
Definition: translate.phtml:15
if($_imageTitle=$block->escapeHtml($block->getCurrentImage() ->getLabel())) $imageUrl
Definition: gallery.phtml:28

◆ generateSwatchVariations()

generateSwatchVariations (   $imageUrl)

Generate swatch thumb and small swatch image

Parameters
string$imageUrl
Returns
$this

Definition at line 184 of file Media.php.

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  }
setupImageProperties(\Magento\Framework\Image $image, $isSwatch=false)
Definition: Media.php:206
generateNamePath($imageConfig, $imageUrl, $swatchType)
Definition: Media.php:227
if($_imageTitle=$block->escapeHtml($block->getCurrentImage() ->getLabel())) $imageUrl
Definition: gallery.phtml:28

◆ getAttributeSwatchPath()

getAttributeSwatchPath (   $file)

Return example: attribute/swatch/m/a/magento.jpg

Parameters
string$file
Returns
string

Definition at line 301 of file Media.php.

302  {
303  return $this->getSwatchMediaPath() . '/' . $this->prepareFile($file);
304  }

◆ getFolderNameSize()

getFolderNameSize (   $swatchType,
  $imageConfig = null 
)

Generate folder name WIDTHxHEIGHT based on config in view.xml

Parameters
string$swatchType
null$imageConfig
Returns
string

Definition at line 244 of file Media.php.

245  {
246  if ($imageConfig === null) {
247  $imageConfig = $this->getImageConfig();
248  }
249  return $imageConfig[$swatchType]['width'] . 'x' . $imageConfig[$swatchType]['height'];
250  }

◆ getImageConfig()

getImageConfig ( )

Merged config from view.xml

Returns
array

Definition at line 257 of file Media.php.

258  {
259  if (!$this->imageConfig) {
260  $this->imageConfig = $this->viewConfig->getViewConfig()->getMediaEntities(
261  'Magento_Catalog',
263  );
264  }
265 
266  return $this->imageConfig;
267  }

◆ getSwatchAttributeImage()

getSwatchAttributeImage (   $swatchType,
  $file 
)
Parameters
string$swatchType
string$file
Returns
string

Definition at line 107 of file Media.php.

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  }
getFolderNameSize($swatchType, $imageConfig=null)
Definition: Media.php:244
generateSwatchVariations($imageUrl)
Definition: Media.php:184

◆ getSwatchCachePath()

getSwatchCachePath (   $swatchType)

Media path with swatch_image or swatch_thumb folder

Parameters
string$swatchType
Returns
string

Definition at line 322 of file Media.php.

323  {
324  return self::SWATCH_MEDIA_PATH . '/' . $swatchType . '/';
325  }

◆ getSwatchMediaPath()

getSwatchMediaPath ( )

Media swatch path

Returns
string

Definition at line 311 of file Media.php.

312  {
314  }

◆ getSwatchMediaUrl()

getSwatchMediaUrl ( )

Url type http://url/pub/media/attribute/swatch/

Returns
string

Definition at line 288 of file Media.php.

289  {
290  return $this->storeManager
291  ->getStore()
292  ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . $this->getSwatchMediaPath();
293  }

◆ getUniqueFileName()

getUniqueFileName (   $file)
protected

Check whether file to move exists. Getting unique name

Parameters
<type>$file
Returns
string

Definition at line 162 of file Media.php.

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  }
static getNewFileName($destinationFile)
Definition: Uploader.php:605

◆ prepareFile()

prepareFile (   $file)
protected

Prepare file for saving

Parameters
string$file
Returns
string

Definition at line 333 of file Media.php.

334  {
335  return ltrim(str_replace('\\', '/', $file), '/');
336  }

◆ prepareFileName()

prepareFileName (   $imageUrl)
protected

Image url /m/a/magento.png return ['name' => 'magento.png', 'path => '/m/a']

Parameters
string$imageUrl
Returns
array

Definition at line 275 of file Media.php.

276  {
277  $fileArray = explode('/', $imageUrl);
278  $fileName = array_pop($fileArray);
279  $filePath = implode('/', $fileArray);
280  return ['name' => $fileName, 'path' => $filePath];
281  }
$fileName
Definition: translate.phtml:15
if($_imageTitle=$block->escapeHtml($block->getCurrentImage() ->getLabel())) $imageUrl
Definition: gallery.phtml:28

◆ setupImageProperties()

setupImageProperties ( \Magento\Framework\Image  $image,
  $isSwatch = false 
)
protected

Setup base image properties for resize

Parameters
\Magento\Framework\Image$image
bool$isSwatch
Returns
$this

Definition at line 206 of file Media.php.

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  }

Field Documentation

◆ $fileStorageDb

$fileStorageDb = null
protected

Definition at line 41 of file Media.php.

◆ $imageFactory

$imageFactory
protected

Definition at line 53 of file Media.php.

◆ $mediaConfig

$mediaConfig
protected

Definition at line 29 of file Media.php.

◆ $mediaDirectory

$mediaDirectory
protected

Definition at line 34 of file Media.php.

◆ $storeManager

$storeManager
protected

Definition at line 48 of file Media.php.

◆ $swatchImageTypes

$swatchImageTypes = ['swatch_image', 'swatch_thumb']
protected

Definition at line 68 of file Media.php.

◆ $themeCollection

$themeCollection
protected

Definition at line 58 of file Media.php.

◆ $viewConfig

$viewConfig
protected

Definition at line 63 of file Media.php.

◆ SWATCH_MEDIA_PATH

const SWATCH_MEDIA_PATH = 'attribute/swatch'

Swatch area inside media folder

Definition at line 24 of file Media.php.


The documentation for this class was generated from the following file: