Skip to content

Commit

Permalink
Init + basic implementation of renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Sep 6, 2019
0 parents commit e7eba05
Show file tree
Hide file tree
Showing 9 changed files with 524 additions and 0 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Vizualizator
============

Smart engine for elegant creating images and graphic visualizations.

Render to SVG, PNG and JPG. All output is in base64 format valid in HTML document.

Install
-------

By Composer:

```shell
composer require mathematicator-core/vizualizator
```

Usage
-----

Imagine you can render all your images by simple objective request.

First inject `Renderer` to your script and create request:

```php
$renderer = new Renderer;
$request = $renderer->createRequest();
```

Now you can add some lines and more:

```php
$request->addLine(10, 10, 35, 70);
$request->addLine(35, 70, 70, 35);
```

And render to page (output is valid HTML code, `base64` or `svg` tag):

```php
// Render specific format:

echo $request->render(Renderer::FORMAT_PNG);
echo $request->render(Renderer::FORMAT_JPG);
echo $request->render(Renderer::FORMAT_SVG);

// Or use default renderer and __toString() method

echp $request;
```

Full simple short example
-------------------------

This example use short fluid-syntax. Final image size is `200x100`:

```php
echo (new Renderer)->createRequest(200, 100)
->addLine(10, 10, 35, 70, '#aaa')
->addLine(35, 70, 70, 35, 'red');
```
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "mathematicator-core/vizualizator",
"description": "Core logic for elegant graphic visualization. Render to SVG, PNG, JPG and Base64.",
"homepage": "https://github.com/mathematicator-core/vizualizator",
"authors": [
{
"name": "Jan Barášek",
"homepage": "https://baraja.cz"
}
],
"require": {
"php": ">=7.1",
"nette/utils": "^3.0",
},
"autoload": {
"classmap": [
"src/"
]
},
"minimum-stability": "stable"
}
18 changes: 18 additions & 0 deletions src/Compiler/Compiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);


namespace Mathematicator\Vizualizator;


interface Compiler
{

/**
* @param RenderRequest $request
* @return string
*/
public function compile(RenderRequest $request): string;

}
33 changes: 33 additions & 0 deletions src/Compiler/JpgCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Mathematicator\Vizualizator;


class JpgCompiler extends PhpGDCompiler
{

/**
* @param RenderRequest $request
* @return string
*/
public function compile(RenderRequest $request): string
{
$this->image = imagecreatetruecolor($request->getWidth(), $request->getHeight());
imagefill($this->image, 0, 0, $this->getColor(255, 255, 255));

$this->process($request);

imagesavealpha($this->image, true);

ob_start();
imagejpeg($this->image);
$bin = ob_get_clean();

imagedestroy($this->image);

return $bin;
}

}
64 changes: 64 additions & 0 deletions src/Compiler/PhpGDCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);


namespace Mathematicator\Vizualizator;


abstract class PhpGDCompiler implements Compiler
{

/**
* @var resource
*/
protected $image;

/**
* @param int $r
* @param int $g
* @param int $b
* @return int
*/
protected function getColor(int $r = 0, int $g = 0, int $b = 0): int
{
static $cache = [];

$key = $r . ';' . $g . ';' . $b;

if (isset($cache[$key]) === false) {
$cache[$key] = imagecolorallocate($this->image, $r, $g, $b);
}

return $cache[$key];
}

/**
* @param int[]|null $params
* @return int
*/
protected function getParameterColor(?array $params): int
{
if ($params === null) {
return $this->getColor();
}

return $this->getColor($params[0], $params[1], $params[2]);
}

/**
* @param RenderRequest $request
*/
protected function process(RenderRequest $request): void
{
foreach ($request->getLines() as $line) {
$this->renderLine($line);
}
}

protected function renderLine(array $line): void
{
imageline($this->image, $line[0], $line[1], $line[2], $line[3], $this->getParameterColor($line[4] ?? null));
}

}
36 changes: 36 additions & 0 deletions src/Compiler/PngCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Mathematicator\Vizualizator;


class PngCompiler extends PhpGDCompiler
{

/**
* @param RenderRequest $request
* @return string
*/
public function compile(RenderRequest $request): string
{
$this->image = imagecreatetruecolor($request->getWidth(), $request->getHeight());
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
$alphaColor = imagecolorallocatealpha($this->image, 0, 0, 0, 127);
imagefill($this->image, 0, 0, $alphaColor);

$this->process($request);

imagesavealpha($this->image, true);

ob_start();
imagepng($this->image);
$bin = ob_get_clean();

imagedestroy($this->image);

return $bin;
}

}
66 changes: 66 additions & 0 deletions src/Compiler/SvgCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Mathematicator\Vizualizator;


class SvgCompiler implements Compiler
{

/**
* @param RenderRequest $request
* @return string
*/
public function compile(RenderRequest $request): string
{
$return = '';

foreach ($request->getLines() as $line) {
$return .= $this->renderLine($line);
}

return '<svg width="' . $request->getWidth() . '" height="' . $request->getHeight() . '">' . $return . '</svg>';
}

/**
* @param string $name
* @param string[] $params
* @return string
*/
private function renderElement(string $name, array $params): string
{
$arguments = [];

foreach ($params as $key => $value) {
$arguments[] = $key . '="' . $value . '"';
}

return '<' . $name . ' ' . implode(' ', $arguments) . ' />';
}

/**
* @param int[]|null $params
* @return string
*/
private function getColor(?array $params): string
{
if ($params === null) {
return 'rgb(0,0,0)';
}

return 'rgb(' . $params[0] . ',' . $params[1] . ',' . $params[2] . ')';
}

private function renderLine(array $line): string
{
return $this->renderElement('line', [
'x1' => $line[0],
'y1' => $line[1],
'x2' => $line[2],
'y2' => $line[3],
'style' => 'stroke:' . $this->getColor($line[4] ?? null) . ';stroke-width:1',
]);
}

}
Loading

0 comments on commit e7eba05

Please sign in to comment.