12
12
namespace Magento \Catalog \Model \Product ;
13
13
14
14
use Magento \Framework \App \Filesystem \DirectoryList ;
15
+ use Magento \Framework \App \ObjectManager ;
15
16
use Magento \Framework \Image as MagentoImage ;
16
- use Magento \Store \Model \Store ;
17
17
18
18
/**
19
19
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -170,6 +170,21 @@ class Image extends \Magento\Framework\Model\AbstractModel
170
170
*/
171
171
protected $ _storeManager ;
172
172
173
+ /**
174
+ * @var \Magento\Catalog\Model\View\Asset\ImageFactory
175
+ */
176
+ private $ viewAssetImageFactory ;
177
+
178
+ /**
179
+ * @var \Magento\Catalog\Model\View\Asset\PlaceholderFactory
180
+ */
181
+ private $ viewAssetPlaceholderFactory ;
182
+
183
+ /**
184
+ * @var \Magento\Framework\View\Asset\LocalInterface
185
+ */
186
+ private $ imageAsset ;
187
+
173
188
/**
174
189
* @param \Magento\Framework\Model\Context $context
175
190
* @param \Magento\Framework\Registry $registry
@@ -184,6 +199,8 @@ class Image extends \Magento\Framework\Model\AbstractModel
184
199
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
185
200
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
186
201
* @param array $data
202
+ * @param \Magento\Catalog\Model\View\Asset\ImageFactory $assetImageFactory
203
+ * @param \Magento\Catalog\Model\View\Asset\PlaceholderFactory $assetPlaceholderFactory
187
204
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
188
205
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
189
206
*/
@@ -200,18 +217,31 @@ public function __construct(
200
217
\Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig ,
201
218
\Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
202
219
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
203
- array $ data = []
220
+ array $ data = [],
221
+ \Magento \Catalog \Model \View \Asset \ImageFactory $ assetImageFactory = null ,
222
+ \Magento \Catalog \Model \View \Asset \PlaceholderFactory $ assetPlaceholderFactory = null
204
223
) {
205
224
$ this ->_storeManager = $ storeManager ;
206
225
$ this ->_catalogProductMediaConfig = $ catalogProductMediaConfig ;
207
226
$ this ->_coreFileStorageDatabase = $ coreFileStorageDatabase ;
208
227
parent ::__construct ($ context , $ registry , $ resource , $ resourceCollection , $ data );
209
228
$ this ->_mediaDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
210
- $ result = $ this ->_mediaDirectory ->create ($ this ->_catalogProductMediaConfig ->getBaseMediaPath ());
211
229
$ this ->_imageFactory = $ imageFactory ;
212
230
$ this ->_assetRepo = $ assetRepo ;
213
231
$ this ->_viewFileSystem = $ viewFileSystem ;
214
232
$ this ->_scopeConfig = $ scopeConfig ;
233
+ $ this ->viewAssetImageFactory = $ assetImageFactory ;
234
+ if ($ this ->viewAssetImageFactory == null ) {
235
+ $ this ->viewAssetImageFactory = ObjectManager::getInstance ()->get (
236
+ \Magento \Catalog \Model \View \Asset \ImageFactory::class
237
+ );
238
+ }
239
+ $ this ->viewAssetPlaceholderFactory = $ assetPlaceholderFactory ;
240
+ if ($ this ->viewAssetPlaceholderFactory == null ) {
241
+ $ this ->viewAssetPlaceholderFactory = ObjectManager::getInstance ()->get (
242
+ \Magento \Catalog \Model \View \Asset \PlaceholderFactory::class
243
+ );
244
+ }
215
245
}
216
246
217
247
/**
@@ -450,85 +480,29 @@ protected function _rgbToString($rgbArray)
450
480
* @param string $file
451
481
* @return $this
452
482
* @throws \Exception
453
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
454
- * @SuppressWarnings(PHPMD.NPathComplexity)
455
483
*/
456
484
public function setBaseFile ($ file )
457
485
{
458
486
$ this ->_isBaseFilePlaceholder = false ;
459
487
460
- if ($ file && 0 !== strpos ($ file , '/ ' , 0 )) {
461
- $ file = '/ ' . $ file ;
462
- }
463
- $ baseDir = $ this ->_catalogProductMediaConfig ->getBaseMediaPath ();
464
-
465
- if ('/no_selection ' == $ file ) {
466
- $ file = null ;
467
- }
468
- if ($ file ) {
469
- if (!$ this ->_fileExists ($ baseDir . $ file ) || !$ this ->_checkMemory ($ baseDir . $ file )) {
470
- $ file = null ;
471
- }
472
- }
473
- if (!$ file ) {
488
+ $ this ->imageAsset = $ this ->viewAssetImageFactory ->create (
489
+ [
490
+ 'miscParams ' => $ this ->getMiscParams (),
491
+ 'filePath ' => $ file ,
492
+ ]
493
+ );
494
+ if ($ file == 'no_selection ' || !$ this ->_fileExists ($ this ->imageAsset ->getSourceFile ())
495
+ || !$ this ->_checkMemory ($ this ->imageAsset ->getSourceFile ())
496
+ ) {
474
497
$ this ->_isBaseFilePlaceholder = true ;
475
- // check if placeholder defined in config
476
- $ isConfigPlaceholder = $ this -> _scopeConfig -> getValue (
477
- " catalog/placeholder/ { $ this ->getDestinationSubdir ()} _placeholder " ,
478
- \ Magento \ Store \ Model \ScopeInterface:: SCOPE_STORE
498
+ $ this -> imageAsset = $ this -> viewAssetPlaceholderFactory -> create (
499
+ [
500
+ ' type ' => $ this ->getDestinationSubdir (),
501
+ ]
479
502
);
480
- $ configPlaceholder = '/placeholder/ ' . $ isConfigPlaceholder ;
481
- if (!empty ($ isConfigPlaceholder ) && $ this ->_fileExists ($ baseDir . $ configPlaceholder )) {
482
- $ file = $ configPlaceholder ;
483
- } else {
484
- $ this ->_newFile = true ;
485
- return $ this ;
486
- }
487
- }
488
-
489
- $ baseFile = $ baseDir . $ file ;
490
-
491
- if (!$ file || !$ this ->_mediaDirectory ->isFile ($ baseFile )) {
492
- throw new \Exception (__ ('We can \'t find the image file. ' ));
493
- }
494
-
495
- $ this ->_baseFile = $ baseFile ;
496
-
497
- // build new filename (most important params)
498
- $ path = [
499
- $ this ->_catalogProductMediaConfig ->getBaseMediaPath (),
500
- 'cache ' ,
501
- $ this ->getDestinationSubdir (),
502
- ];
503
- if (!empty ($ this ->_width ) || !empty ($ this ->_height )) {
504
- $ path [] = "{$ this ->_width }x {$ this ->_height }" ;
505
503
}
506
504
507
- // add misk params as a hash
508
- $ miscParams = [
509
- ($ this ->_keepAspectRatio ? '' : 'non ' ) . 'proportional ' ,
510
- ($ this ->_keepFrame ? '' : 'no ' ) . 'frame ' ,
511
- ($ this ->_keepTransparency ? '' : 'no ' ) . 'transparency ' ,
512
- ($ this ->_constrainOnly ? 'do ' : 'not ' ) . 'constrainonly ' ,
513
- $ this ->_rgbToString ($ this ->_backgroundColor ),
514
- 'angle ' . $ this ->_angle ,
515
- 'quality ' . $ this ->_quality ,
516
- ];
517
-
518
- // if has watermark add watermark params to hash
519
- if ($ this ->getWatermarkFile ()) {
520
- $ miscParams [] = $ this ->getWatermarkFile ();
521
- $ miscParams [] = $ this ->getWatermarkImageOpacity ();
522
- $ miscParams [] = $ this ->getWatermarkPosition ();
523
- $ miscParams [] = $ this ->getWatermarkWidth ();
524
- $ miscParams [] = $ this ->getWatermarkHeight ();
525
- }
526
-
527
- $ path [] = md5 (implode ('_ ' , $ miscParams ));
528
-
529
- // append prepared filename
530
- $ this ->_newFile = implode ('/ ' , $ path ) . $ file ;
531
- // the $file contains heading slash
505
+ $ this ->_baseFile = $ this ->imageAsset ->getSourceFile ();
532
506
533
507
return $ this ;
534
508
}
@@ -542,6 +516,7 @@ public function getBaseFile()
542
516
}
543
517
544
518
/**
519
+ * @deprecated
545
520
* @return bool|string
546
521
*/
547
522
public function getNewFile ()
@@ -690,10 +665,10 @@ public function setWatermark(
690
665
*/
691
666
public function saveFile ()
692
667
{
693
- if ($ this ->_isBaseFilePlaceholder && $ this -> _newFile === true ) {
668
+ if ($ this ->_isBaseFilePlaceholder ) {
694
669
return $ this ;
695
670
}
696
- $ filename = $ this ->_mediaDirectory -> getAbsolutePath ( $ this ->getNewFile ()) ;
671
+ $ filename = $ this ->getBaseFile () ? $ this ->imageAsset -> getPath () : null ;
697
672
$ this ->getImageProcessor ()->save ($ filename );
698
673
$ this ->_coreFileStorageDatabase ->saveFile ($ filename );
699
674
return $ this ;
@@ -704,17 +679,7 @@ public function saveFile()
704
679
*/
705
680
public function getUrl ()
706
681
{
707
- if ($ this ->_newFile === true ) {
708
- $ url = $ this ->_assetRepo ->getUrl (
709
- "Magento_Catalog::images/product/placeholder/ {$ this ->getDestinationSubdir ()}.jpg "
710
- );
711
- } else {
712
- $ url = $ this ->_storeManager ->getStore ()->getBaseUrl (
713
- \Magento \Framework \UrlInterface::URL_TYPE_MEDIA
714
- ) . $ this ->_newFile ;
715
- }
716
-
717
- return $ url ;
682
+ return $ this ->imageAsset ->getUrl ();
718
683
}
719
684
720
685
/**
@@ -740,9 +705,7 @@ public function getDestinationSubdir()
740
705
*/
741
706
public function isCached ()
742
707
{
743
- if (is_string ($ this ->_newFile )) {
744
- return $ this ->_fileExists ($ this ->_newFile );
745
- }
708
+ return file_exists ($ this ->imageAsset ->getPath ());
746
709
}
747
710
748
711
/**
@@ -939,18 +902,45 @@ protected function _fileExists($filename)
939
902
*/
940
903
public function getResizedImageInfo ()
941
904
{
942
- $ fileInfo = null ;
943
- if ($ this ->_newFile === true ) {
944
- $ asset = $ this ->_assetRepo ->createAsset (
945
- "Magento_Catalog::images/product/placeholder/ {$ this ->getDestinationSubdir ()}.jpg "
946
- );
947
- $ img = $ asset ->getSourceFile ();
948
- $ fileInfo = getimagesize ($ img );
905
+ if ($ this ->isBaseFilePlaceholder () == true ) {
906
+ $ image = $ this ->imageAsset ->getSourceFile ();
949
907
} else {
950
- if ($ this ->_mediaDirectory ->isFile ($ this ->_mediaDirectory ->getAbsolutePath ($ this ->_newFile ))) {
951
- $ fileInfo = getimagesize ($ this ->_mediaDirectory ->getAbsolutePath ($ this ->_newFile ));
952
- }
908
+ $ image = $ this ->imageAsset ->getPath ();
953
909
}
954
- return $ fileInfo ;
910
+
911
+ return getimagesize ($ image );
912
+ }
913
+
914
+ /**
915
+ * Retrieve misc params based on all image attributes
916
+ *
917
+ * @return array
918
+ * @SuppressWarnings(PHPMD.NPathComplexity)
919
+ */
920
+ private function getMiscParams ()
921
+ {
922
+ $ miscParams = [
923
+ 'image_type ' => $ this ->getDestinationSubdir (),
924
+ 'image_height ' => $ this ->getHeight (),
925
+ 'image_width ' => $ this ->getWidth (),
926
+ 'keep_aspect_ratio ' => ($ this ->_keepAspectRatio ? '' : 'non ' ) . 'proportional ' ,
927
+ 'keep_frame ' => ($ this ->_keepFrame ? '' : 'no ' ) . 'frame ' ,
928
+ 'keep_transparency ' => ($ this ->_keepTransparency ? '' : 'no ' ) . 'transparency ' ,
929
+ 'constrain_only ' => ($ this ->_constrainOnly ? 'do ' : 'not ' ) . 'constrainonly ' ,
930
+ 'background ' => $ this ->_rgbToString ($ this ->_backgroundColor ),
931
+ 'angle ' => $ this ->_angle ,
932
+ 'quality ' => $ this ->_quality ,
933
+ ];
934
+
935
+ // if has watermark add watermark params to hash
936
+ if ($ this ->getWatermarkFile ()) {
937
+ $ miscParams ['watermark_file ' ] = $ this ->getWatermarkFile ();
938
+ $ miscParams ['watermark_image_opacity ' ] = $ this ->getWatermarkImageOpacity ();
939
+ $ miscParams ['watermark_position ' ] = $ this ->getWatermarkPosition ();
940
+ $ miscParams ['watermark_width ' ] = $ this ->getWatermarkWidth ();
941
+ $ miscParams ['watermark_height ' ] = $ this ->getWatermarkHeight ();
942
+ }
943
+
944
+ return $ miscParams ;
955
945
}
956
946
}
0 commit comments