mirror of
https://github.com/antonkomarev/github-profile-views-counter.git
synced 2026-08-04 19:15:11 -04:00
Make badge style customizable (#8)
* Add Poser badge generator * Make badge styles configurable * Make flat style as default
This commit is contained in:
@@ -13,61 +13,43 @@ declare(strict_types=1);
|
||||
|
||||
namespace Komarev\GitHubProfileViewsCounter;
|
||||
|
||||
use Contracts\Komarev\GitHubProfileViewsCounter\InvalidPathException;
|
||||
use PUGX\Poser\Poser;
|
||||
use PUGX\Poser\Render\SvgFlatRender;
|
||||
use PUGX\Poser\Render\SvgFlatSquareRender;
|
||||
use PUGX\Poser\Render\SvgRender;
|
||||
|
||||
final class BadgeImageRendererService
|
||||
{
|
||||
public function renderBadgeWithCount(string $imagePath, int $count): string
|
||||
private Poser $poser;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->poser = new Poser([new SvgRender(), new SvgFlatRender(), new SvgFlatSquareRender()]);
|
||||
}
|
||||
|
||||
public function renderBadgeWithCount($badgeStyle, int $count): string
|
||||
{
|
||||
$message = (string) $count;
|
||||
|
||||
$messageBackgroundFill = '#007ec6';
|
||||
$messageBackgroundFill = '007ec6';
|
||||
|
||||
return $this->renderBadge($imagePath, $message, $messageBackgroundFill);
|
||||
return $this->renderBadge($badgeStyle, $message, $messageBackgroundFill);
|
||||
}
|
||||
|
||||
public function renderBadgeWithError(string $imagePath, string $message): string
|
||||
public function renderBadgeWithError($badgeStyle, string $message): string
|
||||
{
|
||||
$messageBackgroundFill = '#e05d44';
|
||||
$messageBackgroundFill = 'e05d44';
|
||||
|
||||
return $this->renderBadge($imagePath, $message, $messageBackgroundFill);
|
||||
return $this->renderBadge($badgeStyle, $message, $messageBackgroundFill);
|
||||
}
|
||||
|
||||
private function renderBadge(string $imagePath, string $message, string $messageBackgroundFill): string
|
||||
private function renderBadge($badgeStyle, string $message, string $messageBackgroundFill): string
|
||||
{
|
||||
if (!file_exists($imagePath)) {
|
||||
throw new InvalidPathException('Badge image not found');
|
||||
}
|
||||
|
||||
$image = file_get_contents($imagePath);
|
||||
|
||||
|
||||
return $this->replaceImagePlaceholders($image, $message, $messageBackgroundFill);
|
||||
return (string) $this->poser->generate('Page views', $message, $messageBackgroundFill, $badgeStyle);
|
||||
}
|
||||
|
||||
private function replaceImagePlaceholders(string $image, string $message, string $messageBackgroundFill): string
|
||||
public function renderPixel(): string
|
||||
{
|
||||
$messageLength = strlen($message);
|
||||
|
||||
$minImageWidth = 98;
|
||||
$minMessageBackgroundWidth = 17;
|
||||
$minMessageTextLength = 70;
|
||||
$minMessageTextMarginLeft = 885;
|
||||
|
||||
$label = 'Profile views';
|
||||
$imageWidth = $minImageWidth + (8 * $messageLength);
|
||||
$messageBackgroundWidth = $minMessageBackgroundWidth + (8 * $messageLength);
|
||||
$messageTextLength = $minMessageTextLength + (80 * $messageLength);
|
||||
$messageTextMarginLeft = $minMessageTextMarginLeft + (40 * $messageLength);
|
||||
|
||||
$image = str_replace('%IMAGE_WIDTH%', $imageWidth, $image);
|
||||
$image = str_replace('%LABEL%', $label, $image);
|
||||
$image = str_replace('%MESSAGE%', $message, $image);
|
||||
$image = str_replace('%MESSAGE_BACKGROUND_WIDTH%', $messageBackgroundWidth, $image);
|
||||
$image = str_replace('%MESSAGE_BACKGROUND_FILL%', $messageBackgroundFill, $image);
|
||||
$image = str_replace('%MESSAGE_TEXT_LENGTH%', $messageTextLength, $image);
|
||||
$image = str_replace('%MESSAGE_TEXT_MARGIN_LEFT%', $messageTextMarginLeft, $image);
|
||||
|
||||
return $image;
|
||||
return '<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user