From 9c4f2e52c122bdbea62512f3684cf009182fbf5d Mon Sep 17 00:00:00 2001 From: iherwig Date: Fri, 14 Jun 2024 23:26:15 +0200 Subject: [PATCH] Fix performance regression from last commit --- src/wcmf/lib/io/ImageUtil.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/wcmf/lib/io/ImageUtil.php b/src/wcmf/lib/io/ImageUtil.php index 9a3ca17fb..ae0d1582e 100644 --- a/src/wcmf/lib/io/ImageUtil.php +++ b/src/wcmf/lib/io/ImageUtil.php @@ -396,11 +396,24 @@ private static function resizeImage($sourceFile, $destFile, $width, $type=null, * @return boolean */ private static function isAnimated($imageFile) { - if (!FileUtil::fileExists($imageFile)) { + if (!($fh = @fopen($imageFile, 'rb'))) { return false; } - $manager = self::getImageManager(); - return $manager->read($imageFile)->isAnimated(); + $count = 0; + // An animated gif contains multiple "frames", with each frame having a header made up of: + // * a static 4-byte sequence (\x00\x21\xF9\x04) + // * 4 variable bytes + // * a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?) + + // We read through the file til we reach the end of the file, or we've found + // at least 2 frame headers + while (!feof($fh) && $count < 2) { + $chunk = fread($fh, 1024 * 100); //read 100kb at a time + $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches); + } + + fclose($fh); + return $count > 1; } /**