/** * `CompareImagesWith` compares the images at the urls stored in the `$urls` * property. There are three different "components" that are available to * interchange, namely, `comparators`, `filters`, and `parsers`. */ public function compareImagesWith($comparators = array('md5'), $filters = array(), $parser = 'html') { if (count($this->urls) < 2) { return false; } /** * Parsers are what take an html page and return a set of image URLs. * For now, we have implimented a simple HTML parser based of of * PHPs built in DOMdocument. But it wouldn't be a far stretch to * drop in another module that takes into account JS images, using * something like PHPJS or P2P5. */ $images = array(); foreach ($this->urls as $url) { $name = '\\ImageMatcher\\Parsers\\' . $parser; $imgs = @class_exists($name) ? ImageFactory::imagesFromLocationArray($name::parse($url), md5($url)) : array(); foreach ($imgs as $img) { $images[] = $img; } } $images = $this->runFilters('before_filters', $filters, $images); /** * Comparators compare images and create MatchCollection's of MatchPairs, * which hold the two image objects, as well as a score for their similarity. */ $matches = new MatchCollection(); foreach ($comparators as $comparator) { $name = '\\ImageMatcher\\Comparators\\' . $comparator; $collection = @class_exists($name) ? $name::compare($images) : new MatchCollection(); $matches->appendCollection($collection); } $matches = $this->runFilters('after_filters', $filters, $matches); return $matches; }
/** * The static `compare` method is implimented by all of the Comparators. * This method takes an array of images and performs the needed calculations * on them and outputs a MatchCollection object, which will then be appended * to the global collection. */ public static function compare($images) { // we operate on arrays of image objects if (!is_array($images)) { return false; } $hashes = array(); $matches = new MatchCollection(); foreach ($images as $image) { if ($image instanceof Image) { $image->hashes['md5'] = !$image->hashes['md5'] ? md5($image->data['raw']) : $image->hashes['md5']; if (!empty($hashes[$image->hashes['md5']]) && $hashes[$image->hashes['md5']]->page != $image->page) { $matches->addPair(new MatchPair(array($hashes[$image->hashes['md5']], $image), 'md5', 1)); } else { $hashes[$image->hashes['md5']] = $image; } } } return $matches; }