From: Vincent Vanwaelscappel Date: Fri, 31 Mar 2023 15:50:03 +0000 (+0200) Subject: wait #5611 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=f1358972d45952ca2c2b1ff83aba404fabb3d076;p=cubist_util.git wait #5611 --- 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