From 2ab3df4ba80be4e7f57c0cb826758ff90b746474 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Thu, 26 Oct 2017 14:28:33 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 + composer.json | 26 ++++++++++ src/Command/RenderCommand.php | 34 +++++++++++++ src/DependencyInjection/Configuration.php | 29 +++++++++++ .../NoccyLabsGdLabelExtension.php | 28 +++++++++++ src/GdLabel/LabelRendererService.php | 48 +++++++++++++++++++ src/NoccyLabsGdLabelBundle.php | 9 ++++ src/Resources/config/services.yml | 7 +++ 8 files changed, 183 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Command/RenderCommand.php create mode 100644 src/DependencyInjection/Configuration.php create mode 100644 src/DependencyInjection/NoccyLabsGdLabelExtension.php create mode 100644 src/GdLabel/LabelRendererService.php create mode 100644 src/NoccyLabsGdLabelBundle.php create mode 100644 src/Resources/config/services.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ff72e2d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/composer.lock +/vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..10af809 --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "noccylabs/gd-label-bundle", + "description": "Bundle to integrate noccylabs/gd-label with symfony 3", + "type": "bundle", + "license": "MIT", + "authors": [ + { + "name": "Christopher Vagnetoft", + "email": "cvagnetoft@gmail.com" + } + ], + "require": { + "noccylabs/gd-label": "@dev" + }, + "repositories": [ + { + "type": "composer", + "url": "https://packages.noccylabs.info/composer/" + } + ], + "autoload": { + "psr-4": { + "NoccyLabs\\GdLabelBundle\\": "src/" + } + } +} \ No newline at end of file diff --git a/src/Command/RenderCommand.php b/src/Command/RenderCommand.php new file mode 100644 index 0000000..fd4829c --- /dev/null +++ b/src/Command/RenderCommand.php @@ -0,0 +1,34 @@ +setName("label:render"); + $this->setDescription("Render a label"); + $this->addOption("define", "D", InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, "key=value pairs to pass to use in the template"); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $renderer = $this->getContainer()->get("gdlabel.renderer"); + + $params = []; + foreach ($input->getOption("define") as $opt) { + list ($key, $value) = explode("=", $opt, 2); + $params[$key] = $value; + } + + echo $renderer->renderToData("@InventoryBundle/Resources/labels/inventoryreceipt.xml", $params); + } + +} diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php new file mode 100644 index 0000000..4120cff --- /dev/null +++ b/src/DependencyInjection/Configuration.php @@ -0,0 +1,29 @@ +root('noccy_labs_gd_label'); + + // Here you should define the parameters that are allowed to + // configure your bundle. See the documentation linked above for + // more information on that topic. + + return $treeBuilder; + } +} diff --git a/src/DependencyInjection/NoccyLabsGdLabelExtension.php b/src/DependencyInjection/NoccyLabsGdLabelExtension.php new file mode 100644 index 0000000..331e110 --- /dev/null +++ b/src/DependencyInjection/NoccyLabsGdLabelExtension.php @@ -0,0 +1,28 @@ +processConfiguration($configuration, $configs); + + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.yml'); + } +} diff --git a/src/GdLabel/LabelRendererService.php b/src/GdLabel/LabelRendererService.php new file mode 100644 index 0000000..e0e5552 --- /dev/null +++ b/src/GdLabel/LabelRendererService.php @@ -0,0 +1,48 @@ +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); + } + +} \ No newline at end of file diff --git a/src/NoccyLabsGdLabelBundle.php b/src/NoccyLabsGdLabelBundle.php new file mode 100644 index 0000000..3661584 --- /dev/null +++ b/src/NoccyLabsGdLabelBundle.php @@ -0,0 +1,9 @@ +