Library to dynamically generate chess diagrams
Notice: you need Imagick library installed
Add it to respositories section:
{
"type": "git",
"url": "[email protected]:ChessCom/DiagramGenerator.git"
}
Add it to require section:
"chesscom/diagram-generator": "dev-master"
You can define it as a service
<service id="diagram.generator" class="DiagramGenerator\Generator">
<argument type="service" id="validator" />
</service>
or create with new
operator
$generator = new \DiagramGenerator\Generator($validator);
Config is as a \DiagramGenerator\Config
object
$config = new Config();
$config
->setFen($fenString)
->setSizeIndex(2)
...
If config parameters can be represented as array (e.g. GET or POST parameters), jms serializer can be used to crete config object:
$diagramConfig = $jmsSerializer->deserialize($inputParameters, 'DiagramGenerator\Config', 'json');
Name | Description | Values | Default | Examples |
---|---|---|---|---|
size | board size | 0-3 (0,1,2,3) | 0 | /diagram/size=2 |
theme | theme (pieces) type | 0-6 | 1 | /diagram/size=1&theme=5 |
texture | board (squares) texture | 0-3 | None | /diagram/size=1&texture=1 |
caption | text to show under the board | any url encoded string | empty string | /diagram/caption=php%20c# |
coordinates | show or not board coordinates | true or false | false | /diagram/coordinates=true |
light | hex color of light board pieces | ^[a-fA-F0-9]{6}$ | eeeed2 | /diagram/light=aecef2 |
dark | hex color of dark board pieces | ^[a-fA-F0-9]{6}$ | 769656 | /diagram/dark=16a656 |
flip | allows to flip the board | true or false | false | /diagram/flip=true |
Its as simple as just call one method and set appropriate header:
$diagram = $generator->buildDiagram($diagramConfig);
header('Content-Type: image/jpeg');
echo $diagram->getImage();
UrlHelper class adds support of creating diagram image urls To generate secure and non-secure urls:
$urlHelper->getNonSecureUrl($config, $routingName);
$urlHelper->getSecureUrl($config, $routingName);
where $config is a DigramGenerator\Config
object, $routingName
is your routing name for the action, responsible for rendering diagrams
To enable config validation enable annotations for the validator
$validator->enableAnnotationMapping();
or in sf2 config:
framework:
validation: { enable_annotations: true }
Fen
object is a convenient way to work with fen strings and represents the board as a list of all pieces, each one is an instance of DiagramGenerator\Fen\Piece
object, has color, row and column properties. It can be developed according to our future needs