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());
     }
 }
예제 #2
0
파일: Encoder.php 프로젝트: EdgarPost/image
 /**
  * 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();
 }
예제 #3
0
 public static function initFromDecoded(Decoded $decoded)
 {
     $container = new self();
     $container->setLoops($decoded->getLoops());
     // create empty canvas
     $driver = new Driver();
     $canvas = $driver->newImage($decoded->getCanvasWidth(), $decoded->getCanvasHeight())->getCore();
     foreach ($decoded->getFrames() as $key => $frame) {
         // create resource from frame
         $encoder = new GifEncoder();
         $encoder->setFromDecoded($decoded, $key);
         $frame_resource = imagecreatefromstring($encoder->encode());
         // insert frame image data into canvas
         imagecopy($canvas, $frame_resource, $frame->getOffset()->left, $frame->getOffset()->top, 0, 0, $frame->getSize()->width, $frame->getSize()->height);
         // destory frame resource
         imagedestroy($frame_resource);
         // add frame to container
         $container->addFrame(new \Intervention\Image\Frame($canvas, $frame->getDelay()));
         // prepare next canvas
         $canvas = Helper::cloneResource($canvas);
     }
     return $container;
 }
 public function testDoesLoop()
 {
     $encoder = new Encoder();
     $this->assertFalse($encoder->doesLoop());
     $encoder->setLoops(10);
     $this->assertTrue($encoder->doesLoop());
 }