From e69271f24688529e0467853fa28ec7a5216776e0 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 18 May 2017 09:06:52 +0000 Subject: [PATCH] initial commit --- CHANGELOG.md | 5 +++ LICENSE | 21 ++++++++++++ README.md | 50 ++++++++++++++++++++++++++++ blueprints.yaml | 31 ++++++++++++++++++ twig-image64.php | 38 +++++++++++++++++++++ twig-image64.yaml | 2 ++ twig/Base64Extension.php | 71 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 218 insertions(+) create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 blueprints.yaml create mode 100644 twig-image64.php create mode 100644 twig-image64.yaml create mode 100644 twig/Base64Extension.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5920771 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# v0.1.0 +## 05/17/2017 + +1. [](#new) + * ChangeLog started... diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fa8d580 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2017 Paul Massendari + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b1fa006 --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# Twig Image64 Plugin + +**This README.md file should be modified to describe the features, installation, configuration, and general usage of this plugin.** + +The **Twig Image64** Plugin is for [Grav CMS](http://github.com/getgrav/grav). Base 64 encode image + +## Installation + +Installing the Twig Image64 plugin can be done in one of two ways. The GPM (Grav Package Manager) installation method enables you to quickly and easily install the plugin with a simple terminal command, while the manual method enables you to do so via a zip file. + +### GPM Installation (Preferred) + +The simplest way to install this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm) through your system's terminal (also called the command line). From the root of your Grav install type: + + bin/gpm install twig-image64 + +This will install the Twig Image64 plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/twig-image64`. + +### Manual Installation + +To install this plugin, just download the zip version of this repository and unzip it under `/your/site/grav/user/plugins`. Then, rename the folder to `twig-image64`. You can find these files on [GitHub](https://github.com/paulmassen/grav-plugin-twig-image64) or via [GetGrav.org](http://getgrav.org/downloads/plugins#extras). + +You should now have all the plugin files under + + /your/site/grav/user/plugins/twig-image64 + +> NOTE: This plugin is a modular component for Grav which requires [Grav](http://github.com/getgrav/grav) and the [Error](https://github.com/getgrav/grav-plugin-error) and [Problems](https://github.com/getgrav/grav-plugin-problems) to operate. + +## Configuration + +Before configuring this plugin, you should copy the `user/plugins/twig-image64/twig-image64.yaml` to `user/config/plugins/twig-image64.yaml` and only edit that copy. + +Here is the default configuration and an explanation of available options: + +```yaml +enabled: true +``` + +## Usage + +**Describe how to use the plugin.** + +## Credits + +**Did you incorporate third-party code? Want to thank somebody?** + +## To Do + +- [ ] Future plans, if any + diff --git a/blueprints.yaml b/blueprints.yaml new file mode 100644 index 0000000..6d3d7b5 --- /dev/null +++ b/blueprints.yaml @@ -0,0 +1,31 @@ +name: Twig Image64 +version: 0.1.0 +description: Base 64 encode image +icon: plug +author: + name: Paul Massendari + email: paul@massendari.com +homepage: https://github.com/paulmassen/grav-plugin-twig-image64 +demo: http://demo.yoursite.com +keywords: grav, plugin, etc +bugs: https://github.com/paulmassen/grav-plugin-twig-image64/issues +docs: https://github.com/paulmassen/grav-plugin-twig-image64/blob/develop/README.md +license: MIT + +form: + validation: strict + fields: + enabled: + type: toggle + label: Plugin status + highlight: 1 + default: 0 + options: + 1: Enabled + 0: Disabled + validate: + type: bool + text_var: + type: text + label: Text Variable + help: Text to add to the top of a page diff --git a/twig-image64.php b/twig-image64.php new file mode 100644 index 0000000..63cefda --- /dev/null +++ b/twig-image64.php @@ -0,0 +1,38 @@ + ['onPluginsInitialized', 0], + ]; + } + + public function onPluginsInitialized() + { + if ($this->isAdmin()) { + $this->active = false; + return; + } + + $this->enable([ + 'onTwigExtensions' => ['onTwigExtensions', 0], + ]); + } + + public function onTwigExtensions() + { + require_once(__DIR__ . '/twig/Base64Extension.php'); + $this->grav['twig']->twig->addExtension(new Base64Extension()); + } + +} diff --git a/twig-image64.yaml b/twig-image64.yaml new file mode 100644 index 0000000..da689e8 --- /dev/null +++ b/twig-image64.yaml @@ -0,0 +1,2 @@ +enabled: true +text_var: Custom Text added by the **Twig Image64** plugin (disable plugin to remove) diff --git a/twig/Base64Extension.php b/twig/Base64Extension.php new file mode 100644 index 0000000..4759d96 --- /dev/null +++ b/twig/Base64Extension.php @@ -0,0 +1,71 @@ + + * @link http://yoann.aparici.fr/post/18599782775/extension-twig-pour-encoder-les-images-en-base-64 + */ +class Base64Extension extends \Twig_Extension +{ + private $webDir; + + /** + * Constructor + * Inject path to web directory + * + * @param string $webDir + */ + public function __construct() + { + $this->config = Grav::instance()['config']; + } + + /** + * {@inheritdoc} + */ + public function getFunctions() + { + return array( + 'image64' => new \Twig_Function_Method($this, 'image64'), + ); + } + + /** + * Return a base 64 encoded string only from image content type + * + * @param string $path Path to image + * @return string + */ + public function image64($path) + { + $fullPath = $this->webDir.$path; + $file = new ImageFile($fullPath, true); + + + $binary = file_get_contents($fullPath); + + return sprintf('data:image/%s;base64,%s', $file->extension(), base64_encode($binary)); + } + + /** + * {@inheritdoc} + * + * @return string + */ + public function getName() + { + return 'Base64Extension'; + } + public function getFilters() + { + return array( + 'image64' => new \Twig_SimpleFilter('image64', array($this, 'image64')), + ); + + } +} \ No newline at end of file