-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of the plugin version 1.0
- Loading branch information
1 parent
f7c22d3
commit 0f55f41
Showing
8 changed files
with
241 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,5 @@ | ||
# 1.0.0 | ||
## 28/05/2016 | ||
|
||
1. [](#new) | ||
* Added `image_collage(images: ImageMedium[], column: int, borderSize: int, width: int): ImageMedium` Twig Extension |
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 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Petr Grishin | ||
|
||
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. |
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 @@ | ||
# [Grav](https://github.com/getgrav/grav) Image Collage Plugin | ||
|
||
![image](assets/example.png) | ||
|
||
## Installation | ||
|
||
Installing the Image Collage plugin can be done in one of two ways. Our 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 image_collage | ||
|
||
This will install the Image Collage plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/image_collage`. | ||
|
||
### 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 `image_collage`. You can find these files either on [GitHub](https://github.com/petrgrishin/grav-plugin-image-collage) or via [GetGrav.org](http://getgrav.org/downloads/plugins#extras). | ||
|
||
You should now have all the plugin files under | ||
|
||
/your/site/grav/user/plugins/image_collage | ||
|
||
>> NOTE: This plugin is a modular component for Grav which requires [Grav](https://github.com/getgrav/grav). | ||
## Usage | ||
|
||
```php | ||
{{ images_collage(page.media.images).html }} | ||
|
||
{{ images_collage(page.media.images|slice(1, 5)).html }} //or slice | ||
|
||
{{ images_collage(page.media.images, 3, 5, 900).html }} //or with params | ||
//image_collage(images: ImageMedium[], column: int, borderSize: int, width: int): ImageMedium | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,50 @@ | ||
name: Image Collage | ||
version: 1.0.0 | ||
description: "Create beautiful image collages for all your posts and pages" | ||
icon: picture-o | ||
author: | ||
name: Petr Grishin | ||
email: [email protected] | ||
homepage: https://github.com/petrgrishin/grav-plugin-image-collage | ||
keywords: [image, collage, combination, photo, plugin] | ||
docs: https://github.com/petrgrishin/grav-plugin-image-collage/blob/master/README.md | ||
bugs: https://github.com/petrgrishin/grav-plugin-image-collage/issues | ||
license: MIT | ||
|
||
form: | ||
validation: strict | ||
fields: | ||
enabled: | ||
type: toggle | ||
label: PLUGIN_ADMIN.PLUGIN_STATUS | ||
highlight: 1 | ||
default: 1 | ||
options: | ||
1: PLUGIN_ADMIN.ENABLED | ||
0: PLUGIN_ADMIN.DISABLED | ||
validate: | ||
type: bool | ||
column: | ||
type: text | ||
size: x-small | ||
label: Number of columns | ||
default: 2 | ||
validate: | ||
type: number | ||
min: 1 | ||
border_size: | ||
type: text | ||
size: x-small | ||
label: Border size | ||
default: 10 | ||
validate: | ||
type: number | ||
min: 0 | ||
image_width: | ||
type: text | ||
size: x-small | ||
label: Result image width | ||
default: 900 | ||
validate: | ||
type: number | ||
min: 30 |
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,31 @@ | ||
<?php | ||
/** | ||
* @author Petr Grishin <[email protected]> | ||
*/ | ||
|
||
namespace Grav\Plugin; | ||
|
||
use Grav\Common\Plugin; | ||
|
||
class ImageCollagePlugin extends Plugin | ||
{ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
'onPluginsInitialized' => ['onPluginsInitialized', 0], | ||
]; | ||
} | ||
|
||
public function onPluginsInitialized() | ||
{ | ||
$this->enable([ | ||
'onTwigExtensions' => ['onTwigExtensions', 0], | ||
]); | ||
} | ||
|
||
public function onTwigExtensions() | ||
{ | ||
require_once(__DIR__ . '/image_collage_twig_extension.php'); | ||
$this->grav['twig']->twig->addExtension(new \ImageCollageTwigExtension()); | ||
} | ||
} |
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,4 @@ | ||
enabled: true | ||
column: 2 | ||
border_size: 10 | ||
image_width: 900 |
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,94 @@ | ||
<?php | ||
/** | ||
* @author Petr Grishin <[email protected]> | ||
*/ | ||
|
||
use Grav\Common\Grav; | ||
use Grav\Common\Page\Medium\ImageFile; | ||
use Grav\Common\Page\Medium\ImageMedium; | ||
use Gregwar\Image\Image; | ||
|
||
class ImageCollageTwigExtension extends \Twig_Extension | ||
{ | ||
/** @var Grav */ | ||
protected $grav; | ||
protected $column; | ||
protected $borderSize; | ||
protected $width; | ||
protected $locator; | ||
|
||
public function __construct() | ||
{ | ||
$this->grav = Grav::instance(); | ||
$this->locator = $this->grav['locator']; | ||
$this->loadParamsFromConfig([ | ||
'column' => 'column', | ||
'border_size' => 'borderSize', | ||
'image_width' => 'width' | ||
], 'plugins.image_collage.'); | ||
} | ||
|
||
/** | ||
* Returns the name of the extension. | ||
* | ||
* @return string The extension name | ||
*/ | ||
public function getName() { | ||
return 'ImageCollageTwigExtension'; | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return [ | ||
new \Twig_SimpleFunction('images_collage', [$this, 'imageCollage']), | ||
]; | ||
} | ||
|
||
/** | ||
* @param ImageMedium[] $images | ||
* @param int $column | ||
* @param int $borderSize | ||
* @param int $width | ||
* @return ImageMedium | ||
*/ | ||
public function imageCollage(array $images, $column = null, $borderSize = null, $width = null) | ||
{ | ||
is_null($column) && $column = $this->column; | ||
is_null($borderSize) && $borderSize = $this->borderSize; | ||
is_null($width) && $width = $this->width; | ||
$widthImg = $width - $borderSize; | ||
$cachePath = $this->locator->findResource('cache://images', true); | ||
$collage = ImageFile::create($widthImg, $widthImg) | ||
->setCacheDir($cachePath) | ||
->setActualCacheDir($cachePath); | ||
$collage->rectangle(0, 0, $widthImg, $widthImg, 0xffffff, true); | ||
$c = 0; | ||
$r = 0; | ||
$mergedWidth = $width / $column; | ||
foreach ($images as $image) { | ||
$mergedImage = Image::open($image->get('filepath')); | ||
$mergedImage->zoomCrop($mergedWidth - $borderSize, $mergedWidth - $borderSize); | ||
$collage->merge($mergedImage, $r * $mergedWidth, $c * $mergedWidth, $mergedWidth - $borderSize, $mergedWidth - $borderSize); | ||
$c++; | ||
if ($c % $column == 0) { | ||
$c = 0; | ||
$r++; | ||
} | ||
} | ||
$filePath = $collage->cacheFile('jpg', 85); | ||
$imageMedium = \Grav\Common\Page\Medium\MediumFactory::fromFile($filePath); | ||
return $imageMedium; | ||
} | ||
|
||
/** | ||
* @param array $mapParams | ||
* @param string $configPrefix | ||
* @return $this | ||
*/ | ||
protected function loadParamsFromConfig(array $mapParams, $configPrefix) { | ||
foreach ($mapParams as $nameConfigParam => $nameParam) { | ||
$this->{$nameParam} = $this->grav['config']->get($configPrefix . $nameConfigParam); | ||
} | ||
return $this; | ||
} | ||
} |