public function testInitFromData()
 {
     $data = file_get_contents('tests/images/animation.gif');
     $decoder = new Decoder();
     $decoder->initFromData($data);
     $this->assertInstanceOf('Intervention\\Gif\\Decoder', $decoder);
 }
 public function testDecodeEncoded()
 {
     // create two resource
     $res1 = imagecreatetruecolor(20, 15);
     imagefill($res1, 0, 0, 850736919);
     $res2 = imagecreatetruecolor(20, 15);
     imagefill($res1, 0, 0, 11876119);
     // create encoded
     $encoder = new Encoder();
     $encoder->setCanvas(20, 15);
     $encoder->setLoops(2);
     $encoder->createFrameFromGdResource($res1, 100);
     $encoder->createFrameFromGdResource($res2, 100);
     $encoded = $encoder->encode();
     // decode encoded
     $decoder = new Decoder();
     $decoder->initFromData($encoded);
     $decoded = $decoder->decode();
     // check after decoding
     $this->assertEquals(20, $decoded->getCanvasWidth());
     $this->assertEquals(15, $decoded->getCanvasHeight());
     $this->assertEquals(2, $decoded->countFrames());
     $this->assertEquals(2, $decoded->getLoops());
     $this->assertEquals(32, $decoded->countGlobalColors());
     $this->assertFalse($decoded->hasGlobalColorTable());
     foreach ($decoded->getFrames() as $frame) {
         $this->assertEquals(100, $frame->getDelay());
     }
 }
Exemple #3
0
 /**
  * Processes and returns encoded image as GIF string
  *
  * @return string
  */
 protected function processGif()
 {
     $image = $this->image;
     $encoder = new GifEncoder();
     $encoder->setCanvas($image->getWidth(), $image->getHeight());
     $encoder->setLoops($image->getContainer()->getLoops());
     // set frames
     foreach ($image as $frame) {
         // extract each frame
         ob_start();
         imagegif($frame->getCore());
         $frame_data = ob_get_contents();
         ob_end_clean();
         // decode frame
         $decoder = new GifDecoder();
         $decoder->initFromData($frame_data);
         $decoded = $decoder->decode();
         // add each frame
         $encoder->addFrame($decoded->getFrame()->setLocalColorTable($decoded->getGlobalColorTable())->setDelay($frame->delay));
     }
     return $encoder->encode();
 }
Exemple #4
0
 /**
  * Initiates new image from binary data
  *
  * @param  string $data
  * @return \Intervention\Image\Image
  */
 public function initFromBinary($binary)
 {
     try {
         // try to custom decode gif
         $gifDecoder = new GifDecoder();
         $decoded = $gifDecoder->initFromData($binary)->decode();
         // create image
         $container = Container::initFromDecoded($decoded);
         $image = $this->initFromContainer($container);
         $image->mime = 'image/gif';
     } catch (\Exception $e) {
         $resource = @imagecreatefromstring($binary);
         if ($resource === false) {
             throw new \Intervention\Image\Exception\NotReadableException("Unable to init from given binary data.");
         }
         // create image
         $image = $this->initFromGdResource($resource);
         $image->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $binary);
     }
     return $image;
 }