-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Init + basic implementation of renderer
- Loading branch information
0 parents
commit e7eba05
Showing
9 changed files
with
524 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]); | ||
} | ||
|
||
} |
Oops, something went wrong.