frames > 1; } /** * Counts GIF frames by walking the block structure, so pixel data cannot cause false matches. * * @param string $path Absolute path. * @return int */ private static function gif_frames( string $path ): int { $data = (string) file_get_contents( $path ); $len = strlen( $data ); if ( $len < 13 ) { return 1; } $flags = ord( $data[10] ); $pos = 13; if ( $flags & 0x80 ) { $pos += 3 * ( 2 << ( $flags & 0x07 ) ); } $frames = 0; while ( $pos < $len ) { $block = ord( $data[ $pos ] ); if ( 0x3B === $block ) { break; } if ( 0x21 === $block ) { $pos += 2; $pos = self::skip_gif_sub_blocks( $data, $pos, $len ); } elseif ( 0x2C === $block ) { ++$frames; if ( $pos + 10 > $len ) { break; } $local = ord( $data[ $pos + 9 ] ); $pos += 10; if ( $local & 0x80 ) { $pos += 3 * ( 2 << ( $local & 0x07 ) ); } ++$pos; // LZW minimum code size. $pos = self::skip_gif_sub_blocks( $data, $pos, $len ); } else { break; } } return max( 1, $frames ); } /** * Advances past a run of GIF data sub-blocks. * * @param string $data File contents. * @param int $pos Current offset. * @param int $len File length. * @return int New offset. */ private static function skip_gif_sub_blocks( string $data, int $pos, int $len ): int { while ( $pos < $len ) { $size = ord( $data[ $pos ] ); ++$pos; if ( 0 === $size ) { break; } $pos += $size; } return $pos; } /** * Reads the PNG fields the pipeline cares about. * * @param string $path Absolute path. * @return array{frames: int, bit_depth: int, has_alpha: bool} */ private static function png_header( string $path ): array { $result = array( 'frames' => 1, 'bit_depth' => 8, 'has_alpha' => false, ); $data = (string) file_get_contents( $path ); $length = strlen( $data ); $pos = 8; while ( $pos + 8 <= $length ) { $fields = unpack( 'Nlength', substr( $data, $pos, 4 ) ); $size = (int) ( false === $fields ? 0 : $fields['length'] ); $type = substr( $data, $pos + 4, 4 ); if ( 'IDAT' === $type || 'IEND' === $type ) { break; } if ( 'IHDR' === $type && $size >= 13 ) { $result['bit_depth'] = ord( $data[ $pos + 16 ] ); $color_type = ord( $data[ $pos + 17 ] ); $result['has_alpha'] = 4 === $color_type || 6 === $color_type; } elseif ( 'tRNS' === $type ) { $result['has_alpha'] = true; } elseif ( 'acTL' === $type && $size >= 8 ) { $count = unpack( 'Nframes', substr( $data, $pos + 8, 4 ) ); // A one-frame acTL still marks the file as APNG, which engines would flatten. $result['frames'] = max( 2, (int) ( false === $count ? 1 : $count['frames'] ) ); } $pos += 12 + $size; } return $result; } /** * Reads the WebP fields the pipeline cares about. * * @param string $path Absolute path. * @return array{frames: int, has_alpha: bool} */ private static function webp_header( string $path ): array { $data = (string) file_get_contents( $path ); $result = array( 'frames' => 1, 'has_alpha' => false, ); if ( strlen( $data ) < 30 ) { return $result; } $fourcc = substr( $data, 12, 4 ); if ( 'VP8X' === $fourcc ) { $flags = ord( $data[20] ); $result['has_alpha'] = (bool) ( $flags & 0x10 ); if ( $flags & 0x02 ) { $result['frames'] = max( 2, substr_count( $data, 'ANMF' ) ); } } elseif ( 'VP8L' === $fourcc ) { // Bit 28 of the VP8L header is the alpha_is_used hint. $result['has_alpha'] = (bool) ( ord( $data[24] ) & 0x10 ); } return $result; } }