Grafika\Gd\Helper\GifHelper::expandBlocks PHP Method

expandBlocks() public method

Expand GIF blocks into useful info.
public expandBlocks ( array $blocks ) : array
$blocks array Accepts the array returned by decodeToBlocks
return array
    public function expandBlocks($blocks)
    {
        $decoded = array();
        foreach ($blocks as $blockName => $block) {
            $bytes = new GifByteStream($block);
            if (false !== strpos($blockName, 'header')) {
                $part = $bytes->bite(3);
                $decoded['signature'] = $this->_hexToAscii($part);
                $part = $bytes->bite(3);
                $decoded['version'] = $this->_hexToAscii($part);
            } else {
                if (false !== strpos($blockName, 'logicalScreenDescriptor')) {
                    $part = $bytes->bite(2);
                    $decoded['canvasWidth'] = hexdec($this->_switchEndian($part));
                    $part = $bytes->bite(2);
                    $decoded['canvasHeight'] = hexdec($this->_switchEndian($part));
                    $part = $bytes->bite(1);
                    $bin = $this->_fixSize($this->_hexToBin($part), 8);
                    // Make sure len is correct
                    $decoded['globalColorTableFlag'] = bindec(substr($bin, 0, 1));
                    $decoded['colorResolution'] = bindec(substr($bin, 1, 3));
                    $decoded['sortFlag'] = bindec(substr($bin, 4, 1));
                    $decoded['sizeOfGlobalColorTable'] = bindec(substr($bin, 5, 3));
                    $part = $bytes->bite(1);
                    $decoded['backgroundColorIndex'] = hexdec($part);
                    $part = $bytes->bite(1);
                    $decoded['pixelAspectRatio'] = hexdec($part);
                } else {
                    if (false !== strpos($blockName, 'globalColorTable')) {
                        $decoded['globalColorTable'] = $block;
                    } else {
                        if (false !== strpos($blockName, 'applicationExtension')) {
                            $index = explode('-', $blockName, 2);
                            $index = $index[1];
                            $bytes->next(2);
                            // Skip ext intro and label: 21 ff
                            $appNameSize = $bytes->bite(1);
                            // 0x0b or 11 according to spec but we check anyways
                            $appNameSize = hexdec($appNameSize);
                            $appName = $this->_hexToAscii($bytes->bite($appNameSize));
                            $subBlocks = array();
                            while (!$bytes->isEnd()) {
                                // loop thru all app sub blocks
                                $nextSize = $bytes->bite(1);
                                if ($nextSize !== '00') {
                                    $size = hexdec($nextSize);
                                    $subBlocks[] = $bytes->bite($size);
                                }
                            }
                            if ($appName === 'NETSCAPE2.0') {
                                $decoded['applicationExtension'][$index]['appId'] = 'NETSCAPE';
                                $decoded['applicationExtension'][$index]['appCode'] = '2.0';
                                $decoded['applicationExtension'][$index]['subBlocks'] = $subBlocks;
                                $decoded['loopCount'] = hexdec($this->_switchEndian(substr($subBlocks[0], 2, 4)));
                            } else {
                                $decoded['applicationExtension'][$index]['appId'] = substr($appName, 0, 8);
                                $decoded['applicationExtension'][$index]['appCode'] = substr($appName, 8, 3);
                                $decoded['applicationExtension'][$index]['subBlocks'] = $subBlocks;
                            }
                        } else {
                            if (false !== strpos($blockName, 'graphicControlExtension')) {
                                $index = explode('-', $blockName, 2);
                                $index = $index[1];
                                $bytes->next(3);
                                // Skip ext intro, label, and block size which is always 4: 21 f9 04
                                $part = $bytes->bite(1);
                                // packed field
                                $bin = $this->_fixSize($this->_hexToBin($part), 8);
                                // Make sure len is correct
                                $decoded['frames'][$index]['disposalMethod'] = bindec(substr($bin, 3, 3));
                                $decoded['frames'][$index]['userInputFlag'] = bindec(substr($bin, 6, 1));
                                $decoded['frames'][$index]['transparentColorFlag'] = bindec(substr($bin, 7, 1));
                                $part = $bytes->bite(2);
                                $decoded['frames'][$index]['delayTime'] = hexdec($this->_switchEndian($part));
                                $part = $bytes->bite(1);
                                $decoded['frames'][$index]['transparentColorIndex'] = hexdec($part);
                            } else {
                                if (false !== strpos($blockName, 'imageDescriptor')) {
                                    $index = explode('-', $blockName, 2);
                                    $index = $index[1];
                                    $bytes->next(1);
                                    // skip separator: 2c
                                    $part = $bytes->bite(2);
                                    $decoded['frames'][$index]['imageLeft'] = hexdec($this->_switchEndian($part));
                                    $part = $bytes->bite(2);
                                    $decoded['frames'][$index]['imageTop'] = hexdec($this->_switchEndian($part));
                                    $part = $bytes->bite(2);
                                    $decoded['frames'][$index]['imageWidth'] = hexdec($this->_switchEndian($part));
                                    $part = $bytes->bite(2);
                                    $decoded['frames'][$index]['imageHeight'] = hexdec($this->_switchEndian($part));
                                    $part = $bytes->bite(1);
                                    // packed field
                                    $bin = $this->_fixSize($this->_hexToBin($part), 8);
                                    $decoded['frames'][$index]['localColorTableFlag'] = bindec(substr($bin, 0, 1));
                                    $decoded['frames'][$index]['interlaceFlag'] = bindec(substr($bin, 1, 1));
                                    $decoded['frames'][$index]['sortFlag'] = bindec(substr($bin, 2, 1));
                                    $decoded['frames'][$index]['sizeOfLocalColorTable'] = bindec(substr($bin, 5, 3));
                                } else {
                                    if (false !== strpos($blockName, 'localColorTable')) {
                                        $index = explode('-', $blockName, 2);
                                        $index = $index[1];
                                        $decoded['frames'][$index]['localColorTable'] = $block;
                                    } else {
                                        if (false !== strpos($blockName, 'imageData')) {
                                            $index = explode('-', $blockName, 2);
                                            $index = $index[1];
                                            $decoded['frames'][$index]['imageData'] = $block;
                                        } else {
                                            if ($blockName === 'trailer') {
                                                $decoded['trailer'] = $block;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            unset($bytes);
        }
        return $decoded;
    }