From 7a96ea666c6f5f9c71b7fe305860ec43c8e929cd Mon Sep 17 00:00:00 2001 From: Sascha Weidner Date: Sat, 28 Mar 2026 19:26:33 +0100 Subject: [PATCH] Update Resizer.php Prevent animated GIF endless loop (Error 502) --- src/Resizer.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Resizer.php b/src/Resizer.php index f21b90a..f125068 100644 --- a/src/Resizer.php +++ b/src/Resizer.php @@ -247,6 +247,10 @@ protected function processResize(ImageInterface $image, ResizeConfiguration $con return $this->createImage($image, $image->getPath()); } + if ($this->isAnimatedGif($image->getPath())) { + return $this->createImage($image, $image->getPath()); + } + $cachePath = Path::join($this->cacheDir, $this->createCachePath($image->getPath(), $coordinates, $options, false)); if (!$options->getBypassCache()) { @@ -365,4 +369,30 @@ private function encodeBase32(string $bytes): string return implode('', $result); } + + /** + * Check if a file is an animated GIF. + */ + private function isAnimatedGif(string $path): bool + { + if (strtolower(pathinfo($path, PATHINFO_EXTENSION)) !== 'gif') { + return false; + } + + if (!file_exists($path)) { + return false; + } + + $content = file_get_contents($path, false, null, 0, 3145728); + + if ($content === false) { + return false; + } + + // Count occurrences of the GIF image separator byte + // Animated GIFs have multiple image blocks (0x00,0x21,0xF9) + $count = preg_match_all('#\x00\x21\xF9\x04#', $content, $matches); + + return $count > 1; + } }