Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/DiagramGenerator/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DiagramGenerator\Config;
use DiagramGenerator\Fen;
use DiagramGenerator\Image\Storage;
use DiagramGenerator\Image\StorageNew;
use DiagramGenerator\Image\Image;

/**
Expand Down Expand Up @@ -76,7 +77,11 @@ public function getImage()
*/
protected function generateImage()
{
$storage = new Storage($this->cacheDir, $this->pieceThemeUrl, $this->boardTextureUrl);
if ($this->config->hasThemeUrls()) {
$storage = new StorageNew($this->cacheDir);
} else {
$storage = new Storage($this->cacheDir, $this->pieceThemeUrl, $this->boardTextureUrl);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I usually prefer is to rename old class to StorageLegacy, because ultimately we would won't to delete the old class and new one should have the default (Storage) name. One less step during FF cleanup.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change it. I used New suffix to make review easier, as it is obvious that original file was not changed.

}
$image = new Image($storage, $this->config);
$topPadding = $storage->getMaxPieceHeight($this->fen, $this->config) - $this->config->getSize()->getCell();

Expand Down
44 changes: 44 additions & 0 deletions src/DiagramGenerator/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ class Config
#[Range(min: 0, max: 100)]
protected $compressionQualityJpg;

/**
* Custom theme URLs object with board and piece URLs.
* Structure: { board: boardUrl, wp: pieceUrl, bp: pieceUrl, ... }
*
* @var array|null
*/
#[Type('array')]
protected $themeUrls;

/**
* Gets the value of fen.
*
Expand Down Expand Up @@ -454,6 +463,41 @@ public function setCompressionQualityJpg($compressionQualityJpg)
return $this;
}

/**
* Gets the theme URLs object.
*
* @return array|null
*/
public function getThemeUrls()
{
return $this->themeUrls;
}

/**
* Sets the theme URLs object.
* Structure: { board: boardUrl, wp: pieceUrl, bp: pieceUrl, ... }
*
* @param array|null $themeUrls
*
* @return self
*/
public function setThemeUrls($themeUrls)
{
$this->themeUrls = $themeUrls;

return $this;
}

/**
* Checks if custom theme URLs are configured.
*
* @return bool
*/
public function hasThemeUrls()
{
return !empty($this->themeUrls) && is_array($this->themeUrls);
}

public function getBorderThickness()
{
return (int) round($this->getSize()->getCell() / 2);
Expand Down
28 changes: 21 additions & 7 deletions src/DiagramGenerator/Image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DiagramGenerator\Board;
use DiagramGenerator\Fen;
use DiagramGenerator\Generator;
use DiagramGenerator\Image\StorageInterface;
use Intervention\Image\Gd\Decoder;
use Intervention\Image\Gd\Font;
use Intervention\Image\Image as BaseImage;
Expand All @@ -18,13 +19,17 @@ class Image
/** @var BaseImage */
protected $image;

/** @var Storage */
/** @var StorageInterface */
protected $storage;

/** @var Config */
protected $config;

public function __construct(Storage $storage, Config $config)
/**
* @param StorageInterface $storage
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this dockblock and add type hints.

* @param Config $config
*/
public function __construct($storage, Config $config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason type hint is removed from storage?

Suggested change
public function __construct($storage, Config $config)
public function __construct(StorageInterface $storage, Config $config)

{
$this->image = (new Decoder())->initFromGdResource(imagecreatetruecolor(1, 1));
$this->storage = $storage;
Expand Down Expand Up @@ -133,13 +138,17 @@ public function drawBoardWithFigures(Fen $fen, $cellSize, $topPaddingOfCell)
{
$this->image = $this->drawBoard($this->storage->getBackgroundTextureImage($this->config), $cellSize, $topPaddingOfCell);

$boardHasTexture = $this->config->hasThemeUrls()
? isset($this->config->getThemeUrls()['board'])
: !empty($this->config->getTexture());

$this->drawCells(
$this->config->getSize()->getCell(),
$this->config->getDark(),
$this->config->getLight(),
$this->config->getHighlightSquaresColor(),
$topPaddingOfCell,
!empty($this->config->getTexture()),
$boardHasTexture,
$this->config->getHighlightSquares()
);

Expand Down Expand Up @@ -196,13 +205,18 @@ protected function getBaseBoard(BaseImage $backgroundTexture = null, $cellSize,
if ($backgroundTexture) {
$this->addTransparencyIfNeeded($board, $backgroundTexture->getCore());

$destWidth = $cellSize * Board::SQUARES_IN_ROW;
$destHeight = $cellSize * Board::SQUARES_IN_ROW + $topPaddingOfCell;
$srcWidth = $backgroundTexture->getWidth();
$srcHeight = $backgroundTexture->getHeight();

imagecopyresampled(
$board, $backgroundTexture->getCore(),
0, 0, 0, 0,
$cellSize * Board::SQUARES_IN_ROW,
$cellSize * Board::SQUARES_IN_ROW + $topPaddingOfCell,
$cellSize * Board::SQUARES_IN_ROW,
$cellSize * Board::SQUARES_IN_ROW + $topPaddingOfCell
$destWidth,
$destHeight,
$srcWidth,
$srcHeight
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if backgroundTexture image doesn't have perfect dimensions, this could result in asymetric image generated, right?

Of course, if background is always perfect, then nothing to worry about.

}

Expand Down
2 changes: 1 addition & 1 deletion src/DiagramGenerator/Image/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Intervention\Image\ImageManagerStatic;
use RuntimeException;

class Storage
class Storage implements StorageInterface
{
protected $pieces = [];

Expand Down
27 changes: 27 additions & 0 deletions src/DiagramGenerator/Image/StorageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace DiagramGenerator\Image;

use DiagramGenerator\Config;
use DiagramGenerator\Fen;
use DiagramGenerator\Fen\Piece;
use Intervention\Image\Image;

interface StorageInterface
{
/**
* @return Image
*/
public function getPieceImage(Piece $piece, Config $config);

/**
* @return Image|null
*/
public function getBackgroundTextureImage(Config $config);

/**
* @return int
*/
public function getMaxPieceHeight(Fen $fen, Config $config);
}

Loading