]> _ Git - cubist_util.git/commitdiff
wait #5611
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 31 Mar 2023 15:50:03 +0000 (17:50 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 31 Mar 2023 15:50:03 +0000 (17:50 +0200)
src/Graphics/Image.php

index 4c3c1e4834243e35e19da0be382b8b610a83a771..6ac535c33ea99d0ec31fab45c804fcd3e12389d5 100644 (file)
@@ -16,10 +16,6 @@ class Image
      */
     public static function getimagesize($path)
     {
-        if (isset(self::$_imagesizeCache[$path])) {
-            return self::$_imagesizeCache[$path];
-        }
-
         if (!file_exists($path)) {
             throw new \Exception('File ' . $path . ' does not exist');
         }
@@ -30,6 +26,27 @@ class Image
         if (!in_array($ext, $allowedExtensions)) {
             throw new \Exception('File ' . $path . ' is not a valid image');
         }
+
+        $cacheKey = 'getimagesize_' . Files::hashFileAttributes($path);
+        if (isset(self::$_imagesizeCache[$cacheKey])) {
+            return self::$_imagesizeCache[$cacheKey];
+        }
+
+        $res = cache()->rememberForever($cacheKey, function () use ($ext, $path) {
+            return self::_getimagesize($path, $ext);
+        });
+        if (!$res) {
+            cache()->forget($cacheKey);
+        } else {
+            self::$_imagesizeCache[$cacheKey] = $res;
+        }
+        return $res;
+    }
+
+    protected static function _getimagesize($path, $ext)
+    {
+
+        $res = false;
         if ($ext === 'pdf') {
             $i = PDFTools::infos($path);
             $res = $i['infos']['size'];
@@ -64,7 +81,14 @@ class Image
                 }
             }
         } else {
-            $res = getimagesize($path);
+            for ($i = 0; $i <= 5; $i++) {
+                $res = @getimagesize($path);
+                if (!$res) {
+                    continue;
+                }
+                break;
+            }
+
         }
         if (!is_array($res)) {
             $res = false;
@@ -76,7 +100,6 @@ class Image
                 $res[3] = 'width="' . $res[0] . '" height="' . $res[1] . '"';
             }
         }
-        self::$_imagesizeCache[$path] = $res;
         return $res;
     }
 }
\ No newline at end of file