9
9
use Magento \Framework \App \Filesystem \DirectoryList ;
10
10
use Magento \Framework \App \ObjectManager ;
11
11
use Magento \Framework \Image as MagentoImage ;
12
+ use Magento \Framework \Serialize \SerializerInterface ;
12
13
13
14
/**
14
15
* @method string getFile()
@@ -172,6 +173,16 @@ class Image extends \Magento\Framework\Model\AbstractModel
172
173
*/
173
174
private $ imageAsset ;
174
175
176
+ /**
177
+ * @var string
178
+ */
179
+ private $ cachePrefix = 'IMG_INFO ' ;
180
+
181
+ /**
182
+ * @var SerializerInterface
183
+ */
184
+ private $ serializer ;
185
+
175
186
/**
176
187
* Constructor
177
188
*
@@ -190,6 +201,7 @@ class Image extends \Magento\Framework\Model\AbstractModel
190
201
* @param array $data
191
202
* @param \Magento\Catalog\Model\View\Asset\ImageFactory|null $viewAssetImageFactory
192
203
* @param \Magento\Catalog\Model\View\Asset\PlaceholderFactory|null $viewAssetPlaceholderFactory
204
+ * @param SerializerInterface|null $serializer
193
205
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
194
206
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
195
207
*/
@@ -208,7 +220,8 @@ public function __construct(
208
220
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
209
221
array $ data = [],
210
222
\Magento \Catalog \Model \View \Asset \ImageFactory $ viewAssetImageFactory = null ,
211
- \Magento \Catalog \Model \View \Asset \PlaceholderFactory $ viewAssetPlaceholderFactory = null
223
+ \Magento \Catalog \Model \View \Asset \PlaceholderFactory $ viewAssetPlaceholderFactory = null ,
224
+ SerializerInterface $ serializer = null
212
225
) {
213
226
$ this ->_storeManager = $ storeManager ;
214
227
$ this ->_catalogProductMediaConfig = $ catalogProductMediaConfig ;
@@ -223,6 +236,7 @@ public function __construct(
223
236
->get (\Magento \Catalog \Model \View \Asset \ImageFactory::class);
224
237
$ this ->viewAssetPlaceholderFactory = $ viewAssetPlaceholderFactory ?: ObjectManager::getInstance ()
225
238
->get (\Magento \Catalog \Model \View \Asset \PlaceholderFactory::class);
239
+ $ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (SerializerInterface::class);
226
240
}
227
241
228
242
/**
@@ -356,86 +370,6 @@ public function setSize($size)
356
370
return $ this ;
357
371
}
358
372
359
- /**
360
- * @param string|null $file
361
- * @return bool
362
- */
363
- protected function _checkMemory ($ file = null )
364
- {
365
- return $ this ->_getMemoryLimit () > $ this ->_getMemoryUsage () + $ this ->_getNeedMemoryForFile (
366
- $ file
367
- )
368
- || $ this ->_getMemoryLimit () == -1 ;
369
- }
370
-
371
- /**
372
- * @return string
373
- */
374
- protected function _getMemoryLimit ()
375
- {
376
- $ memoryLimit = trim (strtoupper (ini_get ('memory_limit ' )));
377
-
378
- if (!isset ($ memoryLimit [0 ])) {
379
- $ memoryLimit = "128M " ;
380
- }
381
-
382
- if (substr ($ memoryLimit , -1 ) == 'K ' ) {
383
- return substr ($ memoryLimit , 0 , -1 ) * 1024 ;
384
- }
385
- if (substr ($ memoryLimit , -1 ) == 'M ' ) {
386
- return substr ($ memoryLimit , 0 , -1 ) * 1024 * 1024 ;
387
- }
388
- if (substr ($ memoryLimit , -1 ) == 'G ' ) {
389
- return substr ($ memoryLimit , 0 , -1 ) * 1024 * 1024 * 1024 ;
390
- }
391
- return $ memoryLimit ;
392
- }
393
-
394
- /**
395
- * @return int
396
- */
397
- protected function _getMemoryUsage ()
398
- {
399
- if (function_exists ('memory_get_usage ' )) {
400
- return memory_get_usage ();
401
- }
402
- return 0 ;
403
- }
404
-
405
- /**
406
- * @param string|null $file
407
- * @return float|int
408
- * @SuppressWarnings(PHPMD.NPathComplexity)
409
- */
410
- protected function _getNeedMemoryForFile ($ file = null )
411
- {
412
- $ file = $ file === null ? $ this ->getBaseFile () : $ file ;
413
- if (!$ file ) {
414
- return 0 ;
415
- }
416
-
417
- if (!$ this ->_mediaDirectory ->isExist ($ file )) {
418
- return 0 ;
419
- }
420
-
421
- $ imageInfo = getimagesize ($ this ->_mediaDirectory ->getAbsolutePath ($ file ));
422
-
423
- if (!isset ($ imageInfo [0 ]) || !isset ($ imageInfo [1 ])) {
424
- return 0 ;
425
- }
426
- if (!isset ($ imageInfo ['channels ' ])) {
427
- // if there is no info about this parameter lets set it for maximum
428
- $ imageInfo ['channels ' ] = 4 ;
429
- }
430
- if (!isset ($ imageInfo ['bits ' ])) {
431
- // if there is no info about this parameter lets set it for maximum
432
- $ imageInfo ['bits ' ] = 8 ;
433
- }
434
- return round (
435
- ($ imageInfo [0 ] * $ imageInfo [1 ] * $ imageInfo ['bits ' ] * $ imageInfo ['channels ' ] / 8 + Pow (2 , 16 )) * 1.65
436
- );
437
- }
438
-
439
373
/**
440
374
* Convert array of 3 items (decimal r, g, b) to string of their hex values
441
375
*
@@ -472,9 +406,7 @@ public function setBaseFile($file)
472
406
'filePath ' => $ file ,
473
407
]
474
408
);
475
- if ($ file == 'no_selection ' || !$ this ->_fileExists ($ this ->imageAsset ->getSourceFile ())
476
- || !$ this ->_checkMemory ($ this ->imageAsset ->getSourceFile ())
477
- ) {
409
+ if ($ file == 'no_selection ' || !$ this ->_fileExists ($ this ->imageAsset ->getSourceFile ())) {
478
410
$ this ->_isBaseFilePlaceholder = true ;
479
411
$ this ->imageAsset = $ this ->viewAssetPlaceholderFactory ->create (
480
412
[
@@ -682,11 +614,14 @@ public function getDestinationSubdir()
682
614
}
683
615
684
616
/**
685
- * @return bool|void
617
+ * @return bool
686
618
*/
687
619
public function isCached ()
688
620
{
689
- return file_exists ($ this ->imageAsset ->getPath ());
621
+ return (
622
+ is_array ($ this ->loadImageInfoFromCache ($ this ->imageAsset ->getPath ())) ||
623
+ file_exists ($ this ->imageAsset ->getPath ())
624
+ );
690
625
}
691
626
692
627
/**
@@ -856,6 +791,7 @@ public function clearCache()
856
791
$ this ->_mediaDirectory ->delete ($ directory );
857
792
858
793
$ this ->_coreFileStorageDatabase ->deleteFolder ($ this ->_mediaDirectory ->getAbsolutePath ($ directory ));
794
+ $ this ->clearImageInfoFromCache ();
859
795
}
860
796
861
797
/**
@@ -890,7 +826,7 @@ public function getResizedImageInfo()
890
826
$ image = $ this ->imageAsset ->getPath ();
891
827
}
892
828
893
- $ imageProperties = getimagesize ($ image );
829
+ $ imageProperties = $ this -> getimagesize ($ image );
894
830
895
831
return $ imageProperties ;
896
832
} finally {
@@ -932,4 +868,66 @@ private function getMiscParams()
932
868
933
869
return $ miscParams ;
934
870
}
871
+
872
+ /**
873
+ * Get image size
874
+ *
875
+ * @param string $imagePath
876
+ * @return array
877
+ */
878
+ private function getImageSize ($ imagePath )
879
+ {
880
+ $ imageInfo = $ this ->loadImageInfoFromCache ($ imagePath );
881
+ if (!isset ($ imageInfo ['size ' ])) {
882
+ $ imageSize = getimagesize ($ imagePath );
883
+ $ this ->saveImageInfoToCache (['size ' => $ imageSize ], $ imagePath );
884
+ return $ imageSize ;
885
+ } else {
886
+ return $ imageInfo ['size ' ];
887
+ }
888
+ }
889
+
890
+ /**
891
+ * Save image data to cache
892
+ *
893
+ * @param array $imageInfo
894
+ * @param string $imagePath
895
+ * @return void
896
+ */
897
+ private function saveImageInfoToCache (array $ imageInfo , string $ imagePath )
898
+ {
899
+ $ imagePath = $ this ->cachePrefix . $ imagePath ;
900
+ $ this ->_cacheManager ->save (
901
+ $ this ->serializer ->serialize ($ imageInfo ),
902
+ $ imagePath ,
903
+ [$ this ->cachePrefix ]
904
+ );
905
+ }
906
+
907
+ /**
908
+ * Load image data from cache
909
+ *
910
+ * @param string $imagePath
911
+ * @return array|false
912
+ */
913
+ private function loadImageInfoFromCache (string $ imagePath )
914
+ {
915
+ $ imagePath = $ this ->cachePrefix . $ imagePath ;
916
+ $ cacheData = $ this ->_cacheManager ->load ($ imagePath );
917
+ if (!$ cacheData ) {
918
+ return false ;
919
+ } else {
920
+ return $ this ->serializer ->unserialize ($ cacheData );
921
+ }
922
+ }
923
+
924
+ /**
925
+ * Clear image data from cache
926
+ *
927
+ * @return void
928
+ */
929
+ private function clearImageInfoFromCache ()
930
+ {
931
+ $ this ->_cacheManager ->clean ([$ this ->cachePrefix ]);
932
+ }
935
933
}
0 commit comments