Exemplo n.º 1
0
 public function testGeneration()
 {
     $dynamic = new Configuration();
     $dynamic->setImagine($this->getImagine());
     $dynamic->setColor($this->getColor());
     $dynamic->setImage(sprintf('%s/flags.png', $this->path));
     $dynamic->setStylesheet(sprintf('%s/flags.css', $this->path));
     $dynamic->setSelector(".flag.{{filename}}{background-position:{{x}}px {{y}}px}\n");
     $dynamic->getFinder()->name('*.png')->in(__DIR__ . '/Fixtures/flags')->sortByName();
     $fixed = new Configuration();
     $fixed->setImagine($this->getImagine());
     $fixed->setColor($this->getColor());
     $fixed->setImage(sprintf('%s/icons.png', $this->path));
     $fixed->setStylesheet(sprintf('%s/icons.css', $this->path));
     $fixed->setSelector(".icon.{{filename}}{background-position:{{x}}px {{y}}px}\n");
     $fixed->getFinder()->name('*.png')->in(__DIR__ . '/Fixtures/icons')->sortByName();
     $fixed->setWidth(16);
     $generator = new Generator();
     $generator->addConfiguration($dynamic);
     $generator->addConfiguration($fixed);
     $generator->addProcessor(new DynamicProcessor());
     $generator->addProcessor(new FixedProcessor());
     $generator->generate();
     $sprite = $dynamic->getImagine()->open($dynamic->getImage());
     $result = $dynamic->getImagine()->open(__DIR__ . '/Fixtures/results/flags.png');
     $this->assertImageEquals($sprite, $result);
     $this->assertFileEquals(__DIR__ . '/Fixtures/results/flags.css', $dynamic->getStylesheet());
     $sprite = $fixed->getImagine()->open($fixed->getImage());
     $result = $fixed->getImagine()->open(__DIR__ . '/Fixtures/results/icons.png');
     $this->assertImageEquals($result, $sprite);
     $this->assertFileEquals(__DIR__ . '/Fixtures/results/icons.css', $fixed->getStylesheet());
 }
Exemplo n.º 2
0
 /**
  * Returns a Configuration instance.
  *
  * @param  InputInterface $input
  * @return Configuration
  */
 protected function getConfiguration(InputInterface $input)
 {
     $configuration = new Configuration();
     $configuration->setImagine($this->getImagine($input->getOption('driver')));
     $configuration->setOptions($input->getOption('options'));
     $configuration->getFinder()->name($input->getArgument('pattern'))->in($input->getArgument('source'));
     $configuration->setImage($input->getArgument('image'));
     $configuration->setColor(new Color($input->getOption('color'), $input->getOption('alpha')));
     $configuration->setStylesheet($input->getArgument('stylesheet'));
     $configuration->setSelector($input->getArgument('selector'));
     return $configuration;
 }
Exemplo n.º 3
0
 public function testProcessingWithResizing()
 {
     $config = new Configuration();
     $config->setImagine($this->getImagine());
     $config->setColor($this->getColor());
     $config->setImage(sprintf('%s/icons_resized.png', $this->path));
     $config->setStylesheet(sprintf('%s/icons_resized.css', $this->path));
     $config->setSelector(".icon.{{filename}}{background-position:{{x}}px {{y}}px}\n");
     $config->getFinder()->name('*.png')->in(__DIR__ . '/../Fixtures/icons')->sortByName();
     $config->setWidth(12);
     $processor = new FixedProcessor(array('resize' => true));
     $processor->process($config);
     $sprite = $config->getImagine()->open($config->getImage());
     $result = $config->getImagine()->open(__DIR__ . '/../Fixtures/results/icons_resized.png');
     $this->assertImageEquals($sprite, $result);
     $this->assertFileEquals(__DIR__ . '/../Fixtures/results/icons_resized.css', $config->getStylesheet());
 }
Exemplo n.º 4
0
 public function testProcessing()
 {
     $config = new Configuration();
     $config->setImagine($this->getImagine());
     $config->setColor($this->getColor());
     $config->setImage(sprintf('%s/flags.png', $this->path));
     $config->setStylesheet(sprintf('%s/flags.css', $this->path));
     $config->setSelector(".flag.{{filename}}{background-position:{{x}}px {{y}}px}\n");
     $config->getFinder()->name('*.png')->in(__DIR__ . '/../Fixtures/flags')->sortByName();
     $processor = new DynamicProcessor();
     $processor->process($config);
     $sprite = $config->getImagine()->open($config->getImage());
     $result = $config->getImagine()->open(__DIR__ . '/../Fixtures/results/flags.png');
     $this->assertImageEquals($sprite, $result);
     $this->assertFileEquals(__DIR__ . '/../Fixtures/results/flags.css', $config->getStylesheet());
 }
Exemplo n.º 5
0
 public function testProcessorIsGuessedButCouldBeOverwritten()
 {
     $config = new Configuration();
     $this->assertEquals(Configuration::PROCESSOR_DYNAMIC, $config->getProcessor());
     $config->setWidth(16);
     $this->assertEquals(Configuration::PROCESSOR_FIXED, $config->getProcessor());
     $config->setProcessor('test');
     $this->assertEquals('test', $config->getProcessor());
 }
Exemplo n.º 6
0
 /**
  * {@inheritDoc}
  */
 public function process(Configuration $config)
 {
     $sprite = $config->getImagine()->create(new Box(1, 1), $config->getColor());
     $pointer = 0;
     $styles = '';
     foreach ($config->getFinder() as $file) {
         $image = $config->getImagine()->open($file->getRealPath());
         // adjust height if necessary
         $height = $sprite->getSize()->getHeight();
         if ($image->getSize()->getHeight() > $height) {
             $height = $image->getSize()->getHeight();
         }
         // copy&paste into an extended sprite
         $sprite = $config->getImagine()->create(new Box($sprite->getSize()->getWidth() + $image->getSize()->getWidth(), $height), $config->getColor())->paste($sprite, new Point(0, 0));
         // paste image into sprite
         $sprite->paste($image, new Point($pointer, 0));
         // append stylesheet code
         $styles .= $this->parseSelector($config->getSelector(), $file, $pointer);
         // move horizontal cursor
         $pointer += $image->getSize()->getWidth();
     }
     $this->save($config, $sprite, $styles);
 }
Exemplo n.º 7
0
 /**
  * {@inheritDoc}
  */
 public function process(Configuration $config)
 {
     $sprite = $config->getImagine()->create(new Box(ceil($config->getWidth() * iterator_count($config->getFinder())), 1), $config->getColor());
     $pointer = 0;
     $styles = '';
     foreach ($config->getFinder() as $file) {
         $image = $config->getImagine()->open($file->getRealPath());
         // resize if image exceeds fixed with
         if (true === $this->getOption('resize') && $image->getSize()->getWidth() > $config->getWidth()) {
             $image->resize(new Box($config->getWidth(), round($image->getSize()->getHeight() / $image->getSize()->getWidth() * $config->getWidth())));
         }
         // adjust height if necessary
         if ($image->getSize()->getHeight() > $sprite->getSize()->getHeight()) {
             // copy&paste into an extended sprite
             $sprite = $config->getImagine()->create(new Box($sprite->getSize()->getWidth(), $image->getSize()->getHeight()), $config->getColor())->paste($sprite, new Point(0, 0));
         }
         // paste image into sprite
         $sprite->paste($image, new Point($pointer, 0));
         // append stylesheet code
         $styles .= $this->parseSelector($config->getSelector(), $file, $pointer);
         // move horizontal cursor
         $pointer += $config->getWidth();
     }
     $this->save($config, $sprite, $styles);
 }
Exemplo n.º 8
0
 /**
  * Saves the image sprite and stylesheet.
  *
  * @param  Configuration  $config The Configuration instance
  * @param  ImageInterface $image  The ImageInterface instance
  * @param  string         $styles The CSS stylesheet
  * @return void
  *
  * @throws \RuntimeException If the image sprite could not be saved
  * @throws \RuntimeException If the stylesheet could not be saved
  */
 protected function save(Configuration $config, ImageInterface $image, $styles)
 {
     $this->createDirectory(array($config->getImage(), $config->getStylesheet()));
     try {
         $image->save($config->getImage(), $config->getOptions());
     } catch (\RuntimeException $e) {
         // @codeCoverageIgnoreStart
         throw new \RuntimeException(sprintf('Unable to write file "%s".', $config->getImage()));
         // @codeCoverageIgnoreEnd
     }
     if (false === @file_put_contents($config->getStylesheet(), $styles)) {
         // @codeCoverageIgnoreStart
         throw new \RuntimeException(sprintf('Unable to write file "%s".', $config->getStylesheet()));
         // @codeCoverageIgnoreEnd
     }
 }