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

encode() public method

Encode data into GIF hex string.
public encode ( array $data ) : string
$data array The array returned by decode.
return string Hex string of GIF
    public function encode($data)
    {
        $hex = '';
        // header block
        $hex .= $this->_fixSize($this->_asciiToHex($data['signature']), 3);
        $hex .= $this->_fixSize($this->_asciiToHex($data['version']), 3);
        // logical screen descriptor block
        $hex .= $this->_switchEndian($this->_fixSize(dechex($data['canvasWidth']), 4));
        $hex .= $this->_switchEndian($this->_fixSize(dechex($data['canvasHeight']), 4));
        $packedField = decbin($data['globalColorTableFlag']);
        $packedField .= $this->_fixSize(decbin($data['colorResolution']), 3);
        $packedField .= decbin($data['sortFlag']);
        $packedField .= $this->_fixSize(decbin($data['sizeOfGlobalColorTable']), 3);
        $hex .= $this->_fixSize(dechex(bindec($packedField)), 2);
        $hex .= $this->_fixSize(dechex($data['backgroundColorIndex']), 2);
        $hex .= $this->_fixSize(dechex($data['pixelAspectRatio']), 2);
        // global color table optional
        if ($data['globalColorTableFlag'] > 0) {
            $hex .= $data['globalColorTable'];
        }
        // app ext optional
        if (isset($data['applicationExtension'])) {
            foreach ($data['applicationExtension'] as $app) {
                $hex .= '21ff0b';
                $hex .= $this->_fixSize($this->_asciiToHex($app['appId']), 8);
                $hex .= $this->_fixSize($this->_asciiToHex($app['appCode']), 3);
                foreach ($app['subBlocks'] as $subBlock) {
                    $len = $this->_fixSize(dechex(strlen($subBlock) / 2), 2);
                    $hex .= $len . $subBlock;
                }
                $hex .= '00';
            }
        }
        foreach ($data['frames'] as $i => $frame) {
            // graphics control optional
            if (isset($frame['delayTime'])) {
                $hex .= '21f904';
                $packedField = '000';
                // reserved
                $packedField .= $this->_fixSize(decbin($frame['disposalMethod']), 3);
                $packedField .= decbin($frame['userInputFlag']);
                $packedField .= decbin($frame['transparentColorFlag']);
                $hex .= $this->_fixSize(dechex(bindec($packedField)), 2);
                $hex .= $this->_switchEndian($this->_fixSize(dechex($frame['delayTime']), 4));
                $hex .= $this->_switchEndian($this->_fixSize(dechex($frame['transparentColorIndex']), 2));
                $hex .= '00';
            }
            //image desc
            $hex .= '2c';
            $hex .= $this->_switchEndian($this->_fixSize(dechex($frame['imageLeft']), 4));
            $hex .= $this->_switchEndian($this->_fixSize(dechex($frame['imageTop']), 4));
            $hex .= $this->_switchEndian($this->_fixSize(dechex($frame['imageWidth']), 4));
            $hex .= $this->_switchEndian($this->_fixSize(dechex($frame['imageHeight']), 4));
            $packedField = decbin($frame['localColorTableFlag']);
            $packedField .= decbin($frame['interlaceFlag']);
            $packedField .= decbin($frame['sortFlag']);
            $packedField .= '00';
            // reserved
            $packedField .= $this->_fixSize(decbin($frame['sizeOfLocalColorTable']), 3);
            $hex .= $this->_fixSize(dechex(bindec($packedField)), 2);
            // local color table optional
            if ($frame['localColorTableFlag'] > 0) {
                $hex .= $frame['localColorTable'];
            }
            $hex .= $frame['imageData'];
        }
        $hex .= $data['trailer'];
        return $hex;
    }

Usage Example

Beispiel #1
0
 /**
  * Save the image to an image format.
  *
  * @param Image $image
  * @param string $file File path where to save the image.
  * @param null|string $type Type of image. Can be null, "GIF", "PNG", or "JPEG".
  * @param null|string $quality Quality of image. Applies to JPEG only. Accepts number 0 - 100 where 0 is lowest and 100 is the highest quality. Or null for default.
  * @param bool|false $interlace Set to true for progressive JPEG. Applies to JPEG only.
  * @param int $permission Default permission when creating non-existing target directory.
  *
  * @return Editor
  * @throws \Exception
  */
 public function save($image, $file, $type = null, $quality = null, $interlace = false, $permission = 0755)
 {
     if (null === $type) {
         $type = $this->_getImageTypeFromFileName($file);
         // Null given, guess type from file extension
         if (ImageType::UNKNOWN === $type) {
             $type = $image->getType();
             // 0 result, use original image type
         }
     }
     $targetDir = dirname($file);
     // $file's directory
     if (false === is_dir($targetDir)) {
         // Check if $file's directory exist
         // Create and set default perms to 0755
         if (!mkdir($targetDir, $permission, true)) {
             throw new \Exception(sprintf('Cannot create %s', $targetDir));
         }
     }
     switch (strtoupper($type)) {
         case ImageType::GIF:
             if ($image->isAnimated()) {
                 $blocks = $image->getBlocks();
                 $gift = new GifHelper();
                 $hex = $gift->encode($blocks);
                 file_put_contents($file, pack('H*', $hex));
             } else {
                 imagegif($image->getCore(), $file);
             }
             break;
         case ImageType::PNG:
             // PNG is lossless and does not need compression. Although GD allow values 0-9 (0 = no compression), we leave it alone.
             imagepng($image->getCore(), $file);
             break;
         default:
             // Defaults to jpeg
             $quality = $quality === null ? 75 : $quality;
             // Default to 75 (GDs default) if null.
             $quality = $quality > 100 ? 100 : $quality;
             $quality = $quality < 0 ? 0 : $quality;
             imageinterlace($image->getCore(), $interlace);
             imagejpeg($image->getCore(), $file, $quality);
     }
     return $this;
 }