/** * Determines if we can read the content of the file and returns a file pointer resource * * We can't use something like $node->isReadable() as it's too unreliable * Some storage classes just check for the presence of the file * * @param File $file * * @return resource * @throws InternalServerErrorServiceException */ private function isFileReadable($file) { try { $fileHandle = $file->fopen('rb'); if (!$fileHandle) { throw new \Exception(); } } catch (\Exception $exception) { throw new InternalServerErrorServiceException('Something went wrong when trying to read' . $file->getPath()); } return $fileHandle; }
/** * Tests if a GIF is animated * * 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) (Photoshop uses \x00\x21) * * We read through the file until we reach the end of the file, or we've * found at least 2 frame headers * * @link http://php.net/manual/en/function.imagecreatefromgif.php#104473 * * @param File $file * * @return bool * @throws InternalServerErrorServiceException */ private function isGifAnimated($file) { $count = 0; try { $fileHandle = $file->fopen('rb'); while (!feof($fileHandle) && $count < 2) { $chunk = fread($fileHandle, 1024 * 100); //read 100kb at a time $count += preg_match_all('#\\x00\\x21\\xF9\\x04.{4}\\x00(\\x2C|\\x21)#s', $chunk, $matches); } fclose($fileHandle); } catch (\Exception $exception) { throw new InternalServerErrorServiceException('Something went wrong when trying to read a GIF file'); } return $count > 1; }
/** * Tests if a GIF is animated * * 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) (Photoshop uses \x00\x21) * * We read through the file until we reach the end of the file, or we've * found at least 2 frame headers * * @link http://php.net/manual/en/function.imagecreatefromgif.php#104473 * * @param File $file * * @return bool */ private function isGifAnimated($file) { $fileHandle = $file->fopen('rb'); $count = 0; while (!feof($fileHandle) && $count < 2) { $chunk = fread($fileHandle, 1024 * 100); //read 100kb at a time $count += preg_match_all('#\\x00\\x21\\xF9\\x04.{4}\\x00(\\x2C|\\x21)#s', $chunk, $matches); } fclose($fileHandle); return $count > 1; }