From f1358972d45952ca2c2b1ff83aba404fabb3d076 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 31 Mar 2023 17:50:03 +0200 Subject: [PATCH] wait #5611 --- src/Graphics/Image.php | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/Graphics/Image.php b/src/Graphics/Image.php index 4c3c1e4..6ac535c 100644 --- a/src/Graphics/Image.php +++ b/src/Graphics/Image.php @@ -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 -- 2.39.5