public function scan(Map $map)
{
$info = getimagesize($this->imagePath);
/*
* Setup result
*/
$result = new Result();
$result->setDimensions($info[0], $info[1]);
$result->setImageMime($info['mime']);
$result->setImagePath($this->imagePath);
/*
* Check map
*/
$topRightMap = $map->topRight();
$bottomLeftMap = $map->bottomLeft();
$angleMap = $this->anglePoints($topRightMap, $bottomLeftMap);
$distanceCornersMap = $this->distancePoints($topRightMap, $bottomLeftMap);
/*
* Check image
*/
$topRightImage = $this->topRight($topRightMap);
$bottomLeftImage = $this->bottomLeft($bottomLeftMap);
/*
* Ajust angle image
*/
$angleImage = $this->anglePoints($topRightImage, $bottomLeftImage);
$this->ajustRotate($angleMap - $angleImage);
/*
* Check image again
*/
$topRightImage = $this->topRight($topRightMap);
$bottomLeftImage = $this->bottomLeft($bottomLeftMap);
/*
* Ajust size image
*/
$distanceCornersImage = $this->distancePoints($topRightImage, $bottomLeftImage);
$p = 100 - 100 * $distanceCornersImage / $distanceCornersMap;
$this->ajustSize($p);
/*
* Check image again
*/
$topRightImage = $this->topRight($topRightMap);
$bottomLeftImage = $this->bottomLeft($bottomLeftMap);
$ajustX = $topRightImage->getX() - $topRightMap->getX();
$ajustY = $bottomLeftImage->getY() - $bottomLeftMap->getY();
foreach ($map->targets() as $target) {
if ($target instanceof TextTarget) {
$target->setImageBlob($this->textArea($target->getA()->moveX($ajustX)->moveY($ajustY), $target->getB()->moveX($ajustX)->moveY($ajustY)));
} else {
if ($target instanceof RectangleTarget) {
$area = $this->rectangleArea($target->getA()->moveX($ajustX)->moveY($ajustY), $target->getB()->moveX($ajustX)->moveY($ajustY), $target->getTolerance());
}
if ($target instanceof CircleTarget) {
$area = $this->circleArea($target->getPoint()->moveX($ajustX)->moveY($ajustY), $target->getRadius(), $target->getTolerance());
}
$target->setBlackPixelsPercent($area->percentBlack());
$target->setMarked($area->percentBlack() >= $target->getTolerance());
}
$result->addTarget($target);
}
if ($this->debug) {
$this->debug();
}
$this->finish();
return $result;
}