Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cache key generation based on image URLs #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Controller/OcrController.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private function getText(): string
$cacheKey = md5(implode(
'|',
[
$this->imageUrl,
self::transformImageURLForCacheKey($this->imageUrl),
static::$params['engine'],
implode('|', static::$params['langs']),
static::$params['psm'],
Expand All @@ -237,4 +237,21 @@ private function getText(): string
return $this->engine->getText($this->imageUrl, static::$params['langs']);
});
}

/**
* Make an image URL suitable to be used as a cache key (e.g. strip protocol)
* @param string $url
* @return string
*/
private static function transformImageURLForCacheKey(string $url): string
{
return preg_replace_callback(
'/(page\d+-)(\d+)px/',
static function (array $matches) {
// Tolerate ±50px, see T286356.
return $matches[1].( round($matches[2] / 100) * 100 ).'px';
},
preg_replace('/^https?:/i', '', $url)
);
}
}