Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -4,6 +4,7 @@

use DiagramGenerator\Config;
use DiagramGenerator\Fen;
use DiagramGenerator\Image\StorageLegacy;
use DiagramGenerator\Image\Storage;
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 Storage($this->cacheDir);
} else {
$storage = new StorageLegacy($this->cacheDir, $this->pieceThemeUrl, $this->boardTextureUrl);
}
$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
32 changes: 21 additions & 11 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,13 @@ class Image
/** @var BaseImage */
protected $image;

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

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

public function __construct(Storage $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 @@ -131,15 +132,19 @@ public function addCoordinates(int $topPaddingOfCell, bool $isCoordinatesInside
*/
public function drawBoardWithFigures(Fen $fen, $cellSize, $topPaddingOfCell)
{
$this->image = $this->drawBoard($this->storage->getBackgroundTextureImage($this->config), $cellSize, $topPaddingOfCell);
$this->image = $this->drawBoard($this->storage->getBackgroundTextureImage($this->config), $cellSize, $topPaddingOfCell, $this->config->hasThemeUrls());

$boardHasTexture = $this->config->hasThemeUrls()
? isset($this->config->getThemeUrls()['board']) && !empty($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 @@ -179,14 +184,14 @@ protected function drawBorder()
);
}

protected function drawBoard(BaseImage $backgroundTexture = null, $cellSize, $topPaddingOfCell)
protected function drawBoard(BaseImage $backgroundTexture = null, $cellSize, $topPaddingOfCell, $hasThemeUrls = false)
{
$baseBoard = $this->getBaseBoard($backgroundTexture, $cellSize, $topPaddingOfCell);
$baseBoard = $this->getBaseBoard($backgroundTexture, $cellSize, $topPaddingOfCell, $hasThemeUrls);

return (new Decoder())->initFromGdResource($baseBoard);
}

protected function getBaseBoard(BaseImage $backgroundTexture = null, $cellSize, $topPaddingOfCell)
protected function getBaseBoard(BaseImage $backgroundTexture = null, $cellSize, $topPaddingOfCell, $hasThemeUrls = false)
{
$board = imagecreatetruecolor(
$cellSize * Board::SQUARES_IN_ROW,
Expand All @@ -196,13 +201,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 = $hasThemeUrls ? $backgroundTexture->getWidth() : $cellSize * Board::SQUARES_IN_ROW;
$srcHeight = ($hasThemeUrls ? $backgroundTexture->getHeight() : $cellSize * Board::SQUARES_IN_ROW) + $topPaddingOfCell;

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
Loading