9 use Magento\Catalog\Model\View\Asset\ImageFactory;
10 use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
167 private $viewAssetImageFactory;
172 private $viewAssetPlaceholderFactory;
182 private $paramsBuilder;
187 private $cachePrefix =
'IMG_INFO';
222 \
Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase,
225 \
Magento\Framework\View\Asset\Repository $assetRepo,
226 \
Magento\Framework\View\FileSystem $viewFileSystem,
227 ImageFactory $viewAssetImageFactory,
228 PlaceholderFactory $viewAssetPlaceholderFactory,
237 $this->_catalogProductMediaConfig = $catalogProductMediaConfig;
238 $this->_coreFileStorageDatabase = $coreFileStorageDatabase;
241 $this->_imageFactory = $imageFactory;
242 $this->_assetRepo = $assetRepo;
243 $this->_viewFileSystem = $viewFileSystem;
244 $this->_scopeConfig = $scopeConfig;
245 $this->viewAssetImageFactory = $viewAssetImageFactory;
246 $this->viewAssetPlaceholderFactory = $viewAssetPlaceholderFactory;
257 $this->_width = $width;
275 $this->_height = $height;
295 $this->_quality = $quality;
315 $this->_keepAspectRatio = $keep && $keep !==
'false';
325 $this->_keepFrame = $keep && $keep !==
'false';
335 $this->_keepTransparency = $keep && $keep !==
'false';
345 $this->_constrainOnly = $flag && $flag !==
'false';
355 $this->_backgroundColor = $rgbArray;
366 list($width, $height) = explode(
'x', strtolower($size), 2);
367 foreach ([
'width',
'height'] as $wh) {
377 $this->
setWidth($width)->setHeight($height);
391 $this->_isBaseFilePlaceholder =
false;
393 $this->imageAsset = $this->viewAssetImageFactory->create(
395 'miscParams' => $this->getMiscParams(),
399 if ($file ==
'no_selection' || !$this->
_fileExists($this->imageAsset->getSourceFile())) {
400 $this->_isBaseFilePlaceholder =
true;
401 $this->imageAsset = $this->viewAssetPlaceholderFactory->create(
408 $this->_baseFile = $this->imageAsset->getSourceFile();
455 if (!$this->_processor) {
457 $this->_processor = $this->_imageFactory->create($filename);
459 $this->_processor->keepAspectRatio($this->_keepAspectRatio);
460 $this->_processor->keepFrame($this->_keepFrame);
461 $this->_processor->keepTransparency($this->_keepTransparency);
462 $this->_processor->constrainOnly($this->_constrainOnly);
463 $this->_processor->backgroundColor($this->_backgroundColor);
464 $this->_processor->quality($this->_quality);
487 $angle = (int) $angle;
502 $this->_angle = $angle;
527 if ($this->_isBaseFilePlaceholder) {
560 $imagePreprocessor->watermark($filePath);
571 if ($this->_isBaseFilePlaceholder) {
574 $filename = $this->
getBaseFile() ? $this->imageAsset->getPath() :
null;
576 $this->_coreFileStorageDatabase->saveFile($filename);
585 return $this->imageAsset->getUrl();
594 $this->_destinationSubdir = $dir;
611 $path = $this->imageAsset->getPath();
612 return is_array($this->loadImageInfoFromCache(
$path)) || file_exists(
$path);
623 $this->_watermarkFile = $file;
650 $baseDir = $this->_catalogProductMediaConfig->getBaseMediaPath();
653 $baseDir .
'/watermark/stores/' . $this->_storeManager->getStore()->getId() . $file,
654 $baseDir .
'/watermark/websites/' . $this->_storeManager->getWebsite()->getId() . $file,
655 $baseDir .
'/watermark/default/' . $file,
658 foreach ($candidates as $candidate) {
659 if ($this->_mediaDirectory->isExist($candidate)) {
660 $filePath = $this->_mediaDirectory->getAbsolutePath($candidate);
665 $filePath = $this->_viewFileSystem->getStaticFileName($file);
679 $this->_watermarkPosition = $position;
701 $this->_watermarkImageOpacity = $imageOpacity;
723 if (is_array($size)) {
737 $this->_watermarkWidth = $width;
759 $this->_watermarkHeight = $height;
778 $directory = $this->_catalogProductMediaConfig->getBaseMediaPath() .
'/cache';
779 $this->_mediaDirectory->delete($directory);
781 $this->_coreFileStorageDatabase->deleteFolder($this->_mediaDirectory->getAbsolutePath($directory));
782 $this->clearImageInfoFromCache();
794 if ($this->_mediaDirectory->isFile($filename)) {
797 return $this->_coreFileStorageDatabase->saveFileToFilesystem(
798 $this->_mediaDirectory->getAbsolutePath($filename)
812 $image = $this->imageAsset->getSourceFile();
814 $image = $this->imageAsset->getPath();
817 $imageProperties = $this->getImageSize(
$image);
819 return $imageProperties;
821 if (empty($imageProperties)) {
833 private function getMiscParams()
835 return $this->paramsBuilder->build(
840 'frame' => $this->_keepFrame,
841 'constrain' => $this->_constrainOnly,
842 'aspect_ratio' => $this->_keepAspectRatio,
843 'transparency' => $this->_keepTransparency,
844 'background' => $this->_backgroundColor,
845 'angle' => $this->_angle,
846 'quality' => $this->_quality
857 private function getImageSize($imagePath)
859 $imageInfo = $this->loadImageInfoFromCache($imagePath);
860 if (!isset($imageInfo[
'size'])) {
862 $this->saveImageInfoToCache([
'size' => $imageSize], $imagePath);
865 return $imageInfo[
'size'];
876 private function saveImageInfoToCache(array $imageInfo,
string $imagePath)
878 $imagePath = $this->cachePrefix . $imagePath;
879 $this->_cacheManager->save(
880 $this->serializer->serialize($imageInfo),
892 private function loadImageInfoFromCache(
string $imagePath)
894 $imagePath = $this->cachePrefix . $imagePath;
895 $cacheData = $this->_cacheManager->load($imagePath);
899 return $this->serializer->unserialize($cacheData);
908 private function clearImageInfoFromCache()
910 $this->_cacheManager->clean([$this->cachePrefix]);
setImageProcessor($processor)
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\Product\Media\Config $catalogProductMediaConfig, \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Image\Factory $imageFactory, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\View\FileSystem $viewFileSystem, ImageFactory $viewAssetImageFactory, PlaceholderFactory $viewAssetPlaceholderFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], SerializerInterface $serializer=null, ParamsBuilder $paramsBuilder=null)
setWatermarkHeight($height)
setBackgroundColor(array $rgbArray)
setKeepAspectRatio($keep)
setWatermarkPosition($position)
getWatermarkImageOpacity()
setKeepTransparency($keep)
setDestinationSubdir($dir)
$_catalogProductMediaConfig
setWatermarkWidth($width)
$_coreFileStorageDatabase
setWatermarkImageOpacity($imageOpacity)
setWatermark( $file, $position=null, $size=null, $width=null, $height=null, $opacity=null)