diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..8a0ff39 --- /dev/null +++ b/CHANGELOG.md @@ -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 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ed639cb --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4b4fc4c --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# [Grav](http://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](http://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 +``` \ No newline at end of file diff --git a/assets/example.png b/assets/example.png new file mode 100644 index 0000000..c01ea42 Binary files /dev/null and b/assets/example.png differ diff --git a/blueprints.yaml b/blueprints.yaml new file mode 100644 index 0000000..4fd8469 --- /dev/null +++ b/blueprints.yaml @@ -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: petr.grishin@grishini.ru +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 \ No newline at end of file diff --git a/image_collage.php b/image_collage.php new file mode 100644 index 0000000..7e29604 --- /dev/null +++ b/image_collage.php @@ -0,0 +1,31 @@ + + */ + +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()); + } +} \ No newline at end of file diff --git a/image_collage.yaml b/image_collage.yaml new file mode 100644 index 0000000..772f33f --- /dev/null +++ b/image_collage.yaml @@ -0,0 +1,4 @@ +enabled: true +column: 2 +border_size: 10 +image_width: 900 \ No newline at end of file diff --git a/image_collage_twig_extension.php b/image_collage_twig_extension.php new file mode 100644 index 0000000..a7959f7 --- /dev/null +++ b/image_collage_twig_extension.php @@ -0,0 +1,94 @@ + + */ + +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; + } +} \ No newline at end of file