/** * Run markup file generation * Запустить генерацию файла с версткой * @param ImagickExt $imagick * @param string $filePath * @param Segment[] $segments */ public function generate($imagick, $filePath, $segments) { $markup = "\n<style>\n .mosaic_canvas {\n width: 960px;\n margin: 0 auto;\n background-image: url(/{$filePath});\n background-size: 100% auto;\n position: relative;\n }\n\n .mosaic_canvas img {\n width: 100%;\n }\n\n .mosaic_part:hover {\n background: rgba(95, 158, 160, 0.5);\n }\n\n .mosaic_popup {\n margin-left: -210px;\n width: 420px;\n background: #808080;\n padding: 24px;\n display: none;\n position: absolute;\n left: 50%;\n top: 25%;\n }\n\n .mosaic_popup img {\n width: 100%;\n }\n</style>\n "; $markup .= "<div class=\"mosaic_canvas\">"; $markup .= " <img src='/{$filePath}' style='visibility:hidden;'/>"; $cW = $imagick->getWidth(); $cH = $imagick->getHeight(); echo "\nCanvas size : {$cW}x{$cH}\n"; foreach ($segments as $segment) { $markup .= $this->itemCode($cW, $cH, $segment); } $markup .= "</div>"; $markup .= ' <div class="mosaic_popup"> <img src="#"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script> if (window.jQuery != undefined) { $(function() { var $popup = $(".mosaic_popup"); $(".mosaic_canvas .mosaic_part").click(function(event) { event.stopPropagation(); $popup.find("img").attr("src", $(this).data("orig")); $popup.show(); }); $("body").click(function() { $popup.hide(); }); }); } </script> '; file_put_contents($this->webDir . $filePath . ".html", $markup); }
/** * Check segmentation need of image part * Проверяет необходимость деления части изображения * @param ImagickExt $imagick * @return bool */ private function needDividePart($imagick) { $width = $imagick->getWidth(); $height = $imagick->getHeight(); $topLeftPart = $imagick->getImageFromRect(0, 0, $width / 2, $height / 2); $bottomRightPart = $imagick->getImageFromRect($width / 2, $height / 2, $width, $height); $topRightPart = $imagick->getImageFromRect($width / 2, 0, $width, $height / 2); $bottomLeftPart = $imagick->getImageFromRect(0, $height / 2, $width / 2, $height); $sub1 = Color::compare($topLeftPart->getAvgColor(), $bottomRightPart->getAvgColor()); $sub2 = Color::compare($topRightPart->getAvgColor(), $bottomLeftPart->getAvgColor()); return ($sub1 + $sub2) / 2 > 8; }
/** * @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(); } }