diff --git a/src/CounterImageRendererService.php b/src/CounterImageRendererService.php index 6880f6c..153b5c0 100644 --- a/src/CounterImageRendererService.php +++ b/src/CounterImageRendererService.php @@ -34,18 +34,30 @@ final class CounterImageRendererService $this->counterRepository->incrementCount(); $count = $this->counterRepository->getCount(); + $counterImage = file_get_contents($this->sourceImagePath); + + return $this->replaceImagePlaceholders($counterImage, $count); + } + + private function replaceImagePlaceholders(string $counterImage, int $count): string + { + $countLength = $this->countIntegerLength($count); + $minImageWidth = 98; $minCounterBackgroundWidth = 17; $minCounterTextLength = 70; $minCounterTextMarginLeft = 885; - $countLength = $this->countIntegerLength($count); - $counterImage = file_get_contents($this->sourceImagePath); + $imageWidth = $minImageWidth + (8 * $countLength); + $counterBackgroundWidth = $minCounterBackgroundWidth + (8 * $countLength); + $counterTextLength = $minCounterTextLength + (80 * $countLength); + $counterTextMarginLeft = $minCounterTextMarginLeft + (40 * $countLength); + $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); + $counterImage = str_replace('%IMAGE_WIDTH%', $imageWidth, $counterImage); + $counterImage = str_replace('%COUNTER_BACKGROUND_WIDTH%', $counterBackgroundWidth, $counterImage); + $counterImage = str_replace('%COUNTER_TEXT_LENGTH%', $counterTextLength, $counterImage); + $counterImage = str_replace('%COUNTER_TEXT_MARGIN_LEFT%', $counterTextMarginLeft, $counterImage); return $counterImage; }