Initial commit
This commit is contained in:
48
src/GdLabel/LabelRendererService.php
Normal file
48
src/GdLabel/LabelRendererService.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\Bundle\GdLabelBundle\GdLabel;
|
||||
|
||||
use NoccyLabs\GdLabel\Renderer;
|
||||
|
||||
class LabelRendererService
|
||||
{
|
||||
/** @var object */
|
||||
protected $kernel;
|
||||
/** @var Renderer */
|
||||
protected $renderer;
|
||||
|
||||
public function __construct($kernel)
|
||||
{
|
||||
$this->kernel = $kernel;
|
||||
$this->renderer = new Renderer();
|
||||
}
|
||||
|
||||
public function renderToData($template, array $params=[])
|
||||
{
|
||||
$path = $this->kernel->locateResource($template);
|
||||
|
||||
$wd = getcwd();
|
||||
chdir(dirname($path));
|
||||
$gd = $this->renderer->renderFile($path, $params);
|
||||
chdir($wd);
|
||||
|
||||
ob_start();
|
||||
imagepng($gd);
|
||||
$binary = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
$data = base64_encode($binary);
|
||||
return sprintf("data:%s;%s,%s", "image/png", "base64", $data);
|
||||
}
|
||||
|
||||
public function renderToFile($template, $output, array $params=[])
|
||||
{
|
||||
$path = $this->kernel->locateResource($template);
|
||||
|
||||
$wd = getcwd();
|
||||
chdir(dirname($path));
|
||||
$this->renderer->renderFile($path, $params, $output);
|
||||
chdir($wd);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user