/**
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return int|null|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // php app/console gfb:mosaic:create --file="4.jpg" --size=32 --level=2 --accuracy=16 --opacity=0.6
     $this->webDir = $this->getContainer()->get("kernel")->getRootDir() . "/../web/";
     $this->originsPath = $this->getContainer()->getParameter("gfb_mosaic.origins_dir");
     $this->resultsPath = $this->getContainer()->getParameter("gfb_mosaic.results_dir");
     $this->basePath = $this->getContainer()->getParameter("gfb_mosaic.base_dir");
     $this->output = $output;
     if (!extension_loaded('imagick')) {
         $output->writeln("Error: Imagick not found! You should install it!");
         return -1;
     }
     $mosaicFullPath = $this->webDir . $this->resultsPath;
     if (!file_exists($mosaicFullPath)) {
         mkdir($mosaicFullPath, 0777);
     }
     if (!is_writable($mosaicFullPath)) {
         chmod($mosaicFullPath, 0777);
     }
     $imagick = new ImagickExt();
     $needleFormats = array("JPG", "JPEG", "PNG", "GIF");
     $formatsAll = $imagick->queryFormats();
     if (count(array_intersect($needleFormats, $formatsAll)) != count($needleFormats)) {
         $output->writeln("Error: not all the required formats are supported!");
         exit;
     }
     $name = $input->getOption('file');
     $partSize = $input->getOption('size');
     $segmentationLevel = $input->getOption("level");
     $accuracy = $input->getOption('accuracy');
     $partOpacity = $input->getOption("opacity");
     $imagick->readImage($this->webDir . $this->originsPath . $name);
     // Do some magic!
     try {
         $processor = new MosaicProcessor($imagick, $this->webDir);
         $processor->setPartRepo($this->getContainer()->get("doctrine")->getEntityManager()->getRepository("GFBMosaicBundle:Part"));
         $processor->segmentation($partSize, $segmentationLevel);
         $processor->paving($accuracy, $partOpacity);
         $imagick->writeImage($mosaicFullPath . "R" . $name);
         $markupGen = new MarkupGenerator($this->webDir, $this->basePath);
         $markupGen->generate($imagick, $this->resultsPath . "R" . $name, $processor->getSegments());
     } catch (\Exception $ex) {
         echo $ex->getMessage() . "\n";
     }
     // Finish some magic!
     return 0;
 }
 /**
  * @param array $results
  * @param string $color
  */
 private function processResults($results, $color)
 {
     $em = $this->getEm();
     /** @var PartRepo $partRepo */
     $partRepo = $em->getRepository("GFBMosaicBundle:Part");
     foreach ($results as $imgData) {
         $code = md5($imgData["imageId"] . $imgData["url"]);
         $this->output->write("  >>> " . $imgData["url"]);
         $imagick = new ImagickExt();
         try {
             $imagick->readImage($imgData["url"]);
         } catch (\Exception $ex) {
             $this->output->writeln(" [FAIL] ");
             continue;
         }
         $this->output->writeln(" [OK] ");
         $imagick->cutToSquare();
         $imagick->setFormat("png");
         $filename = $code . ".png";
         $imagick->writeImage($this->webDir . $this->basePath . $filename);
         $part = $partRepo->findOneByCode($code);
         if (!$part) {
             $part = new Part();
         }
         $part->setAvgColor($imagick->getAvgColor())->setCode($code)->setPath($filename)->setWidth($imagick->getWidth())->setHeight($imagick->getHeight())->setColor($color);
         if ($part->getId() > 0) {
             $em->merge($part);
         } else {
             $em->persist($part);
         }
         $em->flush();
     }
 }