Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmassen committed May 18, 2017
0 parents commit e69271f
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# v0.1.0
## 05/17/2017

1. [](#new)
* ChangeLog started...
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Twig Image64
version: 0.1.0
description: Base 64 encode image
icon: plug
author:
name: Paul Massendari
email: [email protected]
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
38 changes: 38 additions & 0 deletions twig-image64.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace Grav\Plugin;

use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;

/**
* Class TwigImage64Plugin
* @package Grav\Plugin
*/
class TwigImage64Plugin extends Plugin
{
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['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());
}

}
2 changes: 2 additions & 0 deletions twig-image64.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
enabled: true
text_var: Custom Text added by the **Twig Image64** plugin (disable plugin to remove)
71 changes: 71 additions & 0 deletions twig/Base64Extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Grav\Plugin;
use \Grav\Common\File;
use \Grav\Common\Grav;
use \Grav\Common\Page\Medium\ImageFile;
/**
* Base64Extension class
*
* @author David Guyon <[email protected]>
* @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')),
);

}
}

0 comments on commit e69271f

Please sign in to comment.