Fix dynamic length

This commit is contained in:
antonkomarev
2020-07-11 23:38:14 +03:00
parent 56a94f4131
commit 422c88b370
2 changed files with 18 additions and 3 deletions

View File

@@ -34,9 +34,24 @@ final class CounterImageRendererService
$this->counterRepository->incrementCount();
$count = $this->counterRepository->getCount();
$minImageWidth = 98;
$minCounterBackgroundWidth = 17;
$minCounterTextLength = 70;
$minCounterTextMarginLeft = 885;
$countLength = $this->countIntegerLength($count);
$counterImage = file_get_contents($this->sourceImagePath);
$counterImage = str_replace('%COUNT%', $count, $counterImage);
$counterImage = str_replace('%IMAGE_WIDTH%', $minImageWidth + (8 * $countLength), $counterImage);
$counterImage = str_replace('%COUNTER_BACKGROUND_WIDTH%', $minCounterBackgroundWidth + (8 * $countLength), $counterImage);
$counterImage = str_replace('%COUNTER_TEXT_LENGTH%', $minCounterTextLength + (80 * $countLength), $counterImage);
$counterImage = str_replace('%COUNTER_TEXT_MARGIN_LEFT%', $minCounterTextMarginLeft + (40 * $countLength), $counterImage);
return $counterImage;
}
private function countIntegerLength(int $integer): int
{
return strlen((string) $integer);
}
}