Skip to content

Commit

Permalink
Merge pull request #13 from simonhamp/more-customizable-themes
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhamp committed Jan 8, 2024
2 parents 12efc1d + 8463854 commit ed1c6a5
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 94 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ composer require simonhamp/the-og --with-all-dependencies
Using The OG is really simple. Here's a basic example:

```php
use SimonHamp\TheOg\Image;
use SimonHamp\TheOg\Background;
use SimonHamp\TheOg\Image;

(new Image())
->accentColor('#cc0000')
Expand Down Expand Up @@ -66,18 +66,19 @@ $image->theme(Themes::Dark);
Themes are simple classes. You can create your own theme simply by extending the `AbstractTheme` class:

```php
use SimonHamp\TheOg\Fonts\Inter;
use SimonHamp\TheOg\Themes\AbstractTheme;

$theme = new class(
accentColor: '#247BA0',
backgroundColor: '#ECEBE4',
baseColor: '#153B50',
baseFont: Font::InterBold,
baseFont: Inter::bold(),
callToActionBackgroundColor: '#153B50',
callToActionColor: '#ECEBE4',
descriptionColor: '#429EA6',
descriptionFont: Font::InterLight,
titleFont: Font::InterBlack,
descriptionFont: Inter::light(),
titleFont: Inter::black(),
) extends AbstractTheme {};

$image = new Image;
Expand Down
7 changes: 5 additions & 2 deletions src/Background.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace SimonHamp\TheOg;

use SimonHamp\TheOg\Interfaces\Background as BackgroundInterface;
use SimonHamp\TheOg\Theme\Background as BaseBackground;

enum Background: string
{
case Bananas = 'bananas.webp';
case CloudyDay = 'cloudy-day.png';
case JustWaves = 'just-waves.webp';

public function path(): string
public function load(): BackgroundInterface
{
return __DIR__ . '/../resources/images/' . $this->value;
return new BaseBackground(__DIR__ . '/../resources/images/' . $this->value);
}
}
16 changes: 0 additions & 16 deletions src/Font.php

This file was deleted.

42 changes: 23 additions & 19 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use Intervention\Image\Image as RenderedImage;
use Intervention\Image\Colors\Rgb\Color;
use Intervention\Image\Encoders\PngEncoder;
use Intervention\Image\Interfaces\EncoderInterface;
use SimonHamp\TheOg\Background as BuiltInBackground;
use SimonHamp\TheOg\Interfaces\Background;
use SimonHamp\TheOg\Interfaces\Layout;
use SimonHamp\TheOg\Layout\Layouts;
use SimonHamp\TheOg\Interfaces\Theme;
use SimonHamp\TheOg\Themes\Themes;
use SimonHamp\TheOg\Layout\Layouts\Standard;
use SimonHamp\TheOg\Theme\Theme as BuiltInTheme;

class Image
{
Expand All @@ -25,8 +28,8 @@ class Image

public function __construct()
{
$this->layout(Layouts::Standard);
$this->theme(Themes::Light);
$this->layout(new Standard);
$this->theme(BuiltInTheme::Light);
}

/**
Expand Down Expand Up @@ -86,24 +89,19 @@ public function watermark(string $watermark, ?float $opacity = 1.0): self
/**
* The layout to use
*/
public function layout(Layouts|Layout $layout): self
public function layout(Layout $layout): self
{
if ($layout instanceof Layouts) {
$this->layout = $layout->getLayout();
} else {
$this->layout = $layout;
}

$this->layout = $layout;
return $this;
}

/**
* The theme to use
*/
public function theme(Themes|Theme $theme): self
public function theme(Theme|BuiltInTheme $theme): self
{
if ($theme instanceof Themes) {
$this->theme = $theme->getTheme();
if ($theme instanceof BuiltInTheme) {
$this->theme = $theme->load();
} else {
$this->theme = $theme;
}
Expand Down Expand Up @@ -133,8 +131,14 @@ public function accentColor(string $color): self
/**
* Override the theme's default background
*/
public function background(Background $background, ?float $opacity = 1.0): self
public function background(Background|BuiltInBackground $background, ?float $opacity = 1.0): self
{
if ($background instanceof BuiltInBackground) {
$background = $background->load();
} else {
$background = $background;
}

$this->theme->background($background);
$this->theme->backgroundOpacity($opacity);
return $this;
Expand Down Expand Up @@ -169,19 +173,19 @@ public function render(): RenderedImage
return $this->layout->render($this);
}

public function save(string $path, string $format = PngEncoder::class): self
public function save(string $path, EncoderInterface $encoder = new PngEncoder()): self
{
$this->render()
->encode(new $format)
->encode($encoder)
->save($path);

return $this;
}

public function toString(string $format = PngEncoder::class): string
public function toString(EncoderInterface $encoder = new PngEncoder): string
{
return $this->render()
->encode(new $format)
->encode($encoder)
->toString();
}
}
8 changes: 8 additions & 0 deletions src/Interfaces/Background.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace SimonHamp\TheOg\Interfaces;

interface Background
{
public function path(): string;
}
8 changes: 8 additions & 0 deletions src/Interfaces/Font.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace SimonHamp\TheOg\Interfaces;

interface Font
{
public function path(): string;
}
2 changes: 0 additions & 2 deletions src/Interfaces/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace SimonHamp\TheOg\Interfaces;

use Intervention\Image\Colors\Rgb\Color;
use SimonHamp\TheOg\Background;
use SimonHamp\TheOg\Font;

interface Theme
{
Expand Down
3 changes: 0 additions & 3 deletions src/Layout/AbstractLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

namespace SimonHamp\TheOg\Layout;

use Intervention\Image\Colors\Rgb\Color;
use SimonHamp\TheOg\Background;
use SimonHamp\TheOg\Border;
use SimonHamp\TheOg\BorderPosition;
use SimonHamp\TheOg\Font;
use SimonHamp\TheOg\Interfaces\Layout;
use SimonHamp\TheOg\Traits\RendersFeatures;

Expand Down
20 changes: 0 additions & 20 deletions src/Layout/Layouts.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Layout/TextBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Intervention\Image\Modifiers\TextModifier;
use Intervention\Image\Typography\FontFactory;
use Intervention\Image\Typography\TextBlock;
use SimonHamp\TheOg\Font;
use SimonHamp\TheOg\Interfaces\Font;
use SimonHamp\TheOg\Modifiers\TextModifier as CustomTextModifier;

readonly class TextBox extends Box
Expand Down
15 changes: 15 additions & 0 deletions src/Theme/AbstractFont.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace SimonHamp\TheOg\Theme;

use SimonHamp\TheOg\Interfaces\Font;

abstract class AbstractFont implements Font
{
abstract public function path(): string;

protected function __construct(protected ?string $variant = null)
{

}
}
14 changes: 10 additions & 4 deletions src/Themes/AbstractTheme.php → src/Theme/AbstractTheme.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace SimonHamp\TheOg\Themes;
namespace SimonHamp\TheOg\Theme;

use Intervention\Image\Colors\Rgb\Color;
use SimonHamp\TheOg\Background;
use SimonHamp\TheOg\Font;
use SimonHamp\TheOg\Background as BuiltInBackground;
use SimonHamp\TheOg\Interfaces\Background;
use SimonHamp\TheOg\Interfaces\Font;
use SimonHamp\TheOg\Interfaces\Theme;

abstract class AbstractTheme implements Theme
Expand Down Expand Up @@ -43,8 +44,12 @@ public function getAccentColor(): Color
return Color::create($this->accentColor);
}

public function background(Background $background): self
public function background(Background|BuiltInBackground $background): self
{
if ($background instanceof BuiltInBackground) {
$background = $background->load();
}

$this->background = $background;
return $this;
}
Expand Down Expand Up @@ -101,6 +106,7 @@ public function getBaseColor(): Color
public function baseFont(Font $font): self
{
$this->baseFont = $font;

return $this;
}

Expand Down
18 changes: 18 additions & 0 deletions src/Theme/Background.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace SimonHamp\TheOg\Theme;

use SimonHamp\TheOg\Interfaces\Background as BackgroundInterface;

class Background implements BackgroundInterface
{
public function __construct(protected string $path)
{

}

public function path(): string
{
return $this->path;
}
}
33 changes: 33 additions & 0 deletions src/Theme/Fonts/Inter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace SimonHamp\TheOg\Theme\Fonts;

use SimonHamp\TheOg\Theme\AbstractFont;

class Inter extends AbstractFont
{
public static function regular(): self
{
return new self('Regular');
}

public static function black(): self
{
return new self('Black');
}

public static function bold(): self
{
return new self('Bold');
}

public static function light(): self
{
return new self('Light');
}

public function path(): string
{
return __DIR__ . "/../../../resources/fonts/Inter/static/Inter-{$this->variant}.ttf";
}
}
Loading

0 comments on commit ed1c6a5

Please sign in to comment.