@@ -370,86 +370,6 @@ public function setSize($size)
370
370
return $ this ;
371
371
}
372
372
373
- /**
374
- * @param string|null $file
375
- * @return bool
376
- */
377
- protected function _checkMemory ($ file = null )
378
- {
379
- return $ this ->_getMemoryLimit () > $ this ->_getMemoryUsage () + $ this ->_getNeedMemoryForFile (
380
- $ file
381
- )
382
- || $ this ->_getMemoryLimit () == -1 ;
383
- }
384
-
385
- /**
386
- * @return string
387
- */
388
- protected function _getMemoryLimit ()
389
- {
390
- $ memoryLimit = trim (strtoupper (ini_get ('memory_limit ' )));
391
-
392
- if (!isset ($ memoryLimit [0 ])) {
393
- $ memoryLimit = "128M " ;
394
- }
395
-
396
- if (substr ($ memoryLimit , -1 ) == 'K ' ) {
397
- return substr ($ memoryLimit , 0 , -1 ) * 1024 ;
398
- }
399
- if (substr ($ memoryLimit , -1 ) == 'M ' ) {
400
- return substr ($ memoryLimit , 0 , -1 ) * 1024 * 1024 ;
401
- }
402
- if (substr ($ memoryLimit , -1 ) == 'G ' ) {
403
- return substr ($ memoryLimit , 0 , -1 ) * 1024 * 1024 * 1024 ;
404
- }
405
- return $ memoryLimit ;
406
- }
407
-
408
- /**
409
- * @return int
410
- */
411
- protected function _getMemoryUsage ()
412
- {
413
- if (function_exists ('memory_get_usage ' )) {
414
- return memory_get_usage ();
415
- }
416
- return 0 ;
417
- }
418
-
419
- /**
420
- * @param string|null $file
421
- * @return float|int
422
- * @SuppressWarnings(PHPMD.NPathComplexity)
423
- */
424
- protected function _getNeedMemoryForFile ($ file = null )
425
- {
426
- $ file = $ file === null ? $ this ->getBaseFile () : $ file ;
427
- if (!$ file ) {
428
- return 0 ;
429
- }
430
-
431
- if (!$ this ->_mediaDirectory ->isExist ($ file )) {
432
- return 0 ;
433
- }
434
-
435
- $ imageInfo = $ this ->getimagesize ($ this ->_mediaDirectory ->getAbsolutePath ($ file ));
436
-
437
- if (!isset ($ imageInfo [0 ]) || !isset ($ imageInfo [1 ])) {
438
- return 0 ;
439
- }
440
- if (!isset ($ imageInfo ['channels ' ])) {
441
- // if there is no info about this parameter lets set it for maximum
442
- $ imageInfo ['channels ' ] = 4 ;
443
- }
444
- if (!isset ($ imageInfo ['bits ' ])) {
445
- // if there is no info about this parameter lets set it for maximum
446
- $ imageInfo ['bits ' ] = 8 ;
447
- }
448
- return round (
449
- ($ imageInfo [0 ] * $ imageInfo [1 ] * $ imageInfo ['bits ' ] * $ imageInfo ['channels ' ] / 8 + Pow (2 , 16 )) * 1.65
450
- );
451
- }
452
-
453
373
/**
454
374
* Convert array of 3 items (decimal r, g, b) to string of their hex values
455
375
*
@@ -486,9 +406,7 @@ public function setBaseFile($file)
486
406
'filePath ' => $ file ,
487
407
]
488
408
);
489
- if ($ file == 'no_selection ' || !$ this ->_fileExists ($ this ->imageAsset ->getSourceFile ())
490
- || !$ this ->_checkMemory ($ this ->imageAsset ->getSourceFile ())
491
- ) {
409
+ if ($ file == 'no_selection ' || !$ this ->_fileExists ($ this ->imageAsset ->getSourceFile ())) {
492
410
$ this ->_isBaseFilePlaceholder = true ;
493
411
$ this ->imageAsset = $ this ->viewAssetPlaceholderFactory ->create (
494
412
[
@@ -696,11 +614,14 @@ public function getDestinationSubdir()
696
614
}
697
615
698
616
/**
699
- * @return bool|void
617
+ * @return bool
700
618
*/
701
619
public function isCached ()
702
620
{
703
- return file_exists ($ this ->imageAsset ->getPath ());
621
+ return (
622
+ is_array ($ this ->loadImageInfoFromCache ($ this ->imageAsset ->getPath ())) ||
623
+ file_exists ($ this ->imageAsset ->getPath ())
624
+ );
704
625
}
705
626
706
627
/**
@@ -955,17 +876,46 @@ private function getMiscParams()
955
876
*/
956
877
private function getImageSize ($ imagePath )
957
878
{
958
- $ key = $ this ->cachePrefix . $ imagePath ;
959
- $ size = $ this ->_cacheManager ->load ($ key );
960
- if (!$ size ) {
961
- $ size = getimagesize ($ imagePath );
962
- $ this ->_cacheManager ->save (
963
- $ this ->serializer ->serialize ($ size ),
964
- $ key
965
- );
879
+ $ imageInfo = $ this ->loadImageInfoFromCache ($ imagePath );
880
+ if (!isset ($ imageInfo ['size ' ])) {
881
+ $ imageSize = getimagesize ($ imagePath );
882
+ $ this ->saveImageInfoToCache (['size ' => $ imageSize ], $ imagePath );
883
+ return $ imageSize ;
884
+ } else {
885
+ return $ imageInfo ['size ' ];
886
+ }
887
+ }
888
+
889
+ /**
890
+ * Save image data to cache
891
+ *
892
+ * @param array $imageInfo
893
+ * @param string $imagePath
894
+ * @return void
895
+ */
896
+ private function saveImageInfoToCache (array $ imageInfo , string $ imagePath )
897
+ {
898
+ $ imagePath = $ this ->cachePrefix . $ imagePath ;
899
+ $ this ->_cacheManager ->save (
900
+ $ this ->serializer ->serialize ($ imageInfo ),
901
+ $ imagePath
902
+ );
903
+ }
904
+
905
+ /**
906
+ * Load image data from cache
907
+ *
908
+ * @param string $imagePath
909
+ * @return array|false
910
+ */
911
+ private function loadImageInfoFromCache (string $ imagePath )
912
+ {
913
+ $ imagePath = $ this ->cachePrefix . $ imagePath ;
914
+ $ cacheData = $ this ->_cacheManager ->load ($ imagePath );
915
+ if (!$ cacheData ) {
916
+ return false ;
966
917
} else {
967
- $ size = $ this ->serializer ->unserialize ($ size );
918
+ return $ this ->serializer ->unserialize ($ cacheData );
968
919
}
969
- return $ size ;
970
920
}
971
921
}
0 commit comments