You are here

function _animated_gif_is_animated in Animated GIF 8

Helper method to know if a gif image is animated.

Plugin annotation

@SuppressWarnings(PHPMD . ErrorControlOperator);

Parameters

string $fileUri: The uri file.

Return value

bool Return true if file contains multiple "frames".

4 calls to _animated_gif_is_animated()
animated_gif_field_widget_image_image_form_alter in ./animated_gif.module
Implements hook_field_widget_WIDGET_TYPE_form_alter().
animated_gif_preprocess_image_formatter in ./animated_gif.module
Implements template_preprocess_image_formatter().
animated_gif_preprocess_image_style in ./animated_gif.module
Implements template_preprocess_image_style().
animated_gif_preprocess_responsive_image_formatter in ./animated_gif.module
Implements template_preprocess_responsive_image_formatter().

File

./animated_gif.module, line 101

Code

function _animated_gif_is_animated(string $fileUri) : bool {
  $fopen = @fopen($fileUri, 'rb');
  if (!$fopen) {
    return FALSE;
  }
  $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)
  // We read through the file til we reach the end of the file, or we've found
  // at least 2 frame headers.
  while (!feof($fopen) && $count < 2) {

    // Read 100kb at a time.
    $chunk = fread($fopen, 1024 * 100);
    $count += preg_match_all('#\\x00\\x21\\xF9\\x04.{4}\\x00[\\x2C\\x21]#s', (string) $chunk);
  }
  fclose($fopen);
  return $count > 1;
}