Skip to content

Commit

Permalink
Big commit
Browse files Browse the repository at this point in the history
There's a lot here. I'll break this down in the release notes, but it's good...
  • Loading branch information
simonhamp committed Jan 4, 2024
1 parent 655a2f9 commit dadcdbf
Show file tree
Hide file tree
Showing 27 changed files with 1,267 additions and 363 deletions.
16 changes: 15 additions & 1 deletion src/Border.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@

namespace SimonHamp\TheOg;

use Intervention\Image\Colors\Rgb\Color;

class Border
{
protected BorderPosition $position;
protected int $width = 10;
protected int $width;
protected string $color;

public function color(Color $color): self
{
$this->color = $color;
return $this;
}

public function getColor(): Color
{
return Color::create($this->color);
}

public function position(BorderPosition $position): self
{
Expand Down
1 change: 1 addition & 0 deletions src/BorderPosition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

enum BorderPosition: string
{
case None = 'none';
case All = 'all';
case Bottom = 'bottom';
case Left = 'left';
Expand Down
1 change: 1 addition & 0 deletions src/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
enum Font: string
{
case Inter = 'Inter/static/Inter-Regular.ttf';
case InterBlack = 'Inter/static/Inter-Black.ttf';
case InterBold = 'Inter/static/Inter-Bold.ttf';
case InterLight = 'Inter/static/Inter-Light.ttf';

Expand Down
155 changes: 103 additions & 52 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,162 @@

namespace SimonHamp\TheOg;

use Intervention\Image\Image as RenderedImage;
use Intervention\Image\Colors\Rgb\Color;
use Intervention\Image\Encoders\PngEncoder;
use SimonHamp\TheOg\Traits\RendersImages;
use SimonHamp\TheOg\Interfaces\Layout;
use SimonHamp\TheOg\Layout\Layouts;
use SimonHamp\TheOg\Interfaces\Theme;
use SimonHamp\TheOg\Themes\Themes;

class Image
{
use RendersImages;

protected string $accentColor = '#000000';
protected ?Background $background = null;
protected string $backgroundColor = '#ffffff';
protected float $backgroundOpacity = 1.0;
protected ?Border $border = null;
protected string $callToAction;
protected string $description;
protected int $height = 630;
protected Layout $layout = Layout::Standard;
protected Theme $theme = Theme::Light;
protected string $title;
protected ?string $url = null;
protected int $width = 1200;
public Layout $layout;
public Theme $theme;

public function url(string $url): self
public readonly string $callToAction;
public readonly string $description;
public readonly string $picture;
public readonly string $title;
public readonly string $url;
public readonly string $watermark;

public function __construct()
{
$this->url = $url;
return $this;
$this->layout(Layouts::Standard);
$this->theme(Themes::Light);
}

public function title(string $title): self

/**
* The call to action text
*/
public function callToAction(string $content): self
{
$this->title = $title;
$this->callToAction = $content;
return $this;
}

public function image(string $image): self
/**
* The description text
*/
public function description(string $description): self
{
$this->image = $image;
$this->description = $description;
return $this;
}

public function description(string $description): self

/**
* The picture to display
*/
public function picture(string $picture): self
{
$this->description = $description;
$this->picture = $picture;
return $this;
}

public function layout(Layout $layout): self

/**
* The title text
*/
public function title(string $title): self
{
$this->layout = $layout;
$this->title = $title;
return $this;
}

public function theme(Theme $theme): self

/**
* The URL
*/
public function url(string $url): self
{
$this->theme = $theme;
$this->url = $url;
return $this;
}

public function accentColor(string $hexCode): self

/**
* The watermark image
*/
public function watermark(string $watermark, ?float $opacity = 1.0): self
{
// TODO: Make sure it's a valid hex code
$this->accentColor = $hexCode;
$this->watermark = $watermark;
return $this;
}

public function background(Background $background, float $opacity = 1.0): self
/**
* The layout to use
*/
public function layout(Layouts|Layout $layout): self
{
$this->backgroundOpacity = $opacity < 0 ? 0 : ($opacity > 1 ? 1 : $opacity);
$this->background = $background;
if ($layout instanceof Layouts) {
$this->layout = $layout->getLayout();
} else {
$this->layout = $layout;
}

return $this;
}

public function callToAction(string $content): self
/**
* The theme to use
*/
public function theme(Themes|Theme $theme): self
{
$this->callToAction = $content;
if ($theme instanceof Themes) {
$this->theme = $theme->getTheme();
} else {
$this->theme = $theme;
}

return $this;
}

public function width(int $width): self
/**
* Override the theme's default accent color
*/
public function accentColor(string $color): self
{
$this->width = $width < 100 ? 100 : $width;
$this->theme->accentColor($color);
return $this;
}

public function height(int $height): self
/**
* Override the theme's default background
*/
public function background(Background $background, ?float $opacity = 1.0): self
{
$this->height = $height < 100 ? 100 : $height;
$this->theme->background($background);
$this->theme->backgroundOpacity($opacity);
return $this;
}

/**
* Override the theme's default background color
*/
public function backgroundColor(string $backgroundColor): self
{
// TODO: Make sure it's a valid hex code
$this->backgroundColor = $backgroundColor;
$this->theme->backgroundColor($backgroundColor);
return $this;
}

public function border(int $width = 20, BorderPosition $position = BorderPosition::All): self
/**
* Override the layout's default border
*/
public function border(?BorderPosition $position = null, ?Color $color = null, ?int $width = null): self
{
$this->border = (new Border())
->width($width)
->position($position);
$this->layout->border(
(new Border())
->position($position ?? $this->layout->getBorderPosition())
->color($color ?? $this->theme->getBorderColor())
->width($width ?? $this->layout->getBorderWidth())
);

return $this;
}

public function render(): RenderedImage
{
return $this->layout->render($this);
}

public function save(string $path, string $format = PngEncoder::class): self
{
$this->render()
Expand Down
23 changes: 23 additions & 0 deletions src/Interfaces/Layout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace SimonHamp\TheOg\Interfaces;

use Intervention\Image\Image;
use SimonHamp\TheOg\Border;
use SimonHamp\TheOg\Image as Config;
use SimonHamp\TheOg\Layout\TextBox;

interface Layout
{
public function border(Border $border): self;

public function render(Config $config): Image;

public function getCallToAction(): TextBox;

public function getDescription(): TextBox;

public function getTitle(): TextBox;

public function getUrl(): TextBox;
}
74 changes: 74 additions & 0 deletions src/Interfaces/Theme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace SimonHamp\TheOg\Interfaces;

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

interface Theme
{
public function accentColor(string $color): self;

public function getAccentColor(): Color;

public function background(Background $background): self;

public function getBackground(): ?Background;

public function backgroundColor(string $color): self;

public function getBackgroundColor(): Color;

public function backgroundOpacity(float $opacity): self;

public function getBackgroundOpacity(): float;

public function baseColor(string $color): self;

public function getBaseColor(): Color;

public function baseFont(Font $font): self;

public function getBaseFont(): Font;

public function borderColor(string $color): self;

public function getBorderColor(): Color;

public function callToActionBackgroundColor(string $color): self;

public function getCallToActionBackgroundColor(): Color;

public function callToActionColor(string $color): self;

public function getCallToActionColor(): Color;

public function callToActionFont(Font $font): self;

public function getCallToActionFont(): Font;

public function descriptionColor(string $color): self;

public function getDescriptionColor(): Color;

public function descriptionFont(Font $font): self;

public function getDescriptionFont(): Font;

public function titleColor(string $color): self;

public function getTitleColor(): Color;

public function titleFont(Font $font): self;

public function getTitleFont(): Font;

public function urlColor(string $color): self;

public function getUrlColor(): Color;

public function urlFont(Font $font): self;

public function getUrlFont(): Font;
}
Loading

0 comments on commit dadcdbf

Please sign in to comment.