-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* config filters * readme * rename NetteConfigOperationInterface::apply to operate * docs * add gumlet config filters
- Loading branch information
Showing
32 changed files
with
1,010 additions
and
13 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
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,115 @@ | ||
# Nette filters via neon config | ||
|
||
For basic usage you can use simpler way than defining filters in classes. We use only neon config. | ||
|
||
|
||
## Registration | ||
|
||
```yaml | ||
extensions: | ||
imagist.filters: Contributte\Imagist\Bridge\Nette\DI\ImageStorageConfigFiltersExtension | ||
``` | ||
|
||
That's all. We can use [nette/utils](nette-image-filters.md) or [imagine/imagine](imagine.md) for image operations. | ||
|
||
Built-in operations are | ||
|
||
**nette/utils** | ||
|
||
```php | ||
/** | ||
* @param $flag one of fit, fill, exact, shrink_only, stretch | ||
*/ | ||
resize(int|string|null $width, int|string|null $height = null, string $flag = 'fit') | ||
|
||
crop(int|string $left, int|string $top, int|string $width, int|string $height); | ||
|
||
/** | ||
* @param $mode one of vertical, horizontal, both | ||
*/ | ||
flip(string $mode); | ||
|
||
sharpen(); | ||
``` | ||
|
||
**imagine/imagine** | ||
|
||
```php | ||
resize(int $width, int $height); | ||
``` | ||
|
||
## Usage | ||
|
||
```yaml | ||
imagist.filters: | ||
siteS: resize(200, 200, fill) ## Single operation | ||
siteXS: ## Multiple operations, first resize, second flip, ... | ||
- resize(100, 100, fill) | ||
- flip(vertical) | ||
- crop(20, 20, 20, 20) | ||
- sharpen() | ||
``` | ||
|
||
Traditional usage in latte: | ||
|
||
```html | ||
<img n:img="$image|filter:siteS"> | ||
``` | ||
|
||
## Custom operation in config | ||
|
||
**nette/utils** | ||
```php | ||
use Contributte\Imagist\Bridge\Nette\Filter\Config\NetteConfigOperationInterface; | ||
|
||
final class SharpenConfigOperation implements NetteConfigOperationInterface | ||
{ | ||
|
||
public function getName(): string | ||
{ | ||
return 'sharpen2'; | ||
} | ||
|
||
/** | ||
* @param mixed[] $arguments | ||
*/ | ||
public function operate(Image $image, FilterInterface $filter, NetteImageOptions $options, array $arguments): void | ||
{ | ||
$image->sharpen(); | ||
} | ||
|
||
} | ||
``` | ||
|
||
**imagine/imagine** | ||
|
||
```php | ||
|
||
use Contributte\Imagist\Bridge\Imagine\Config\ImagineConfigOperationInterface; | ||
|
||
final class BlurConfigOperation implements ImagineConfigOperationInterface | ||
{ | ||
|
||
public function getName(): string | ||
{ | ||
return 'blur'; | ||
} | ||
|
||
/** | ||
* @param mixed[] $arguments | ||
*/ | ||
public function operate(ImageInterface $image, FilterInterface $filter, NetteImageOptions $options, array $arguments): void | ||
{ | ||
[$amount] = $arguments; | ||
|
||
$image->effects()->blur($amount); | ||
} | ||
|
||
} | ||
``` | ||
|
||
Register as service | ||
|
||
## Tracy | ||
|
||
![tracy](https://raw.githubusercontent.com/contributte/imagist/master/.docs/img/tracy-filters.png) |
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
26 changes: 26 additions & 0 deletions
26
src/Bridge/Gumlet/Normalizer/Config/GumletConfigNormalizerInterface.php
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,26 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Contributte\Imagist\Bridge\Gumlet\Normalizer\Config; | ||
|
||
use Contributte\Imagist\Bridge\Gumlet\GumletBuilder; | ||
use Contributte\Imagist\Entity\ImageInterface; | ||
use Contributte\Imagist\Filter\FilterInterface; | ||
|
||
interface GumletConfigNormalizerInterface | ||
{ | ||
|
||
public function getName(): string; | ||
|
||
/** | ||
* @param mixed[] $options | ||
* @param mixed[] $arguments | ||
*/ | ||
public function normalize( | ||
GumletBuilder $builder, | ||
FilterInterface $filter, | ||
ImageInterface $image, | ||
array $options, | ||
array $arguments | ||
): void; | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
src/Bridge/Gumlet/Normalizer/Config/GumletConfigNormalizerRegistry.php
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,56 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Contributte\Imagist\Bridge\Gumlet\Normalizer\Config; | ||
|
||
use Contributte\Imagist\Bridge\Gumlet\GumletBuilder; | ||
use Contributte\Imagist\Config\ConfigFilterStack; | ||
use Contributte\Imagist\Entity\ImageInterface; | ||
use Contributte\Imagist\Exceptions\UnexpectedErrorException; | ||
use Contributte\Imagist\Filter\FilterInterface; | ||
use Contributte\Imagist\Filter\FilterNormalizerInterface; | ||
use InvalidArgumentException; | ||
|
||
final class GumletConfigNormalizerRegistry implements FilterNormalizerInterface | ||
{ | ||
|
||
/** @var ConfigFilterStack[] */ | ||
protected array $configFilterStacks = []; | ||
|
||
/** @var GumletConfigNormalizerInterface[] */ | ||
protected array $normalizers = []; | ||
|
||
public function addConfigFilterStack(ConfigFilterStack $configFilterStack): void | ||
{ | ||
$this->configFilterStacks[$configFilterStack->getName()] = $configFilterStack; | ||
} | ||
|
||
public function addNormalizer(GumletConfigNormalizerInterface $normalizer): void | ||
{ | ||
$this->normalizers[$normalizer->getName()] = $normalizer; | ||
} | ||
|
||
public function supports(FilterInterface $filter, ImageInterface $image, array $options): bool | ||
{ | ||
return isset($options['gumlet']) && isset($this->configFilterStacks[$filter->getName()]); | ||
} | ||
|
||
public function normalize(FilterInterface $filter, ImageInterface $image, array $options): array | ||
{ | ||
if (!isset($this->configFilterStacks[$filter->getName()])) { | ||
throw new UnexpectedErrorException(); | ||
} | ||
|
||
$builder = GumletBuilder::create(); | ||
foreach ($this->configFilterStacks[$filter->getName()]->getConfigFilters() as $configFilter) { | ||
if (!isset($this->normalizers[$configFilter->getName()])) { | ||
throw new InvalidArgumentException(sprintf('Config normalizer %s not exists.', $configFilter->getName())); | ||
} | ||
|
||
$normalizer = $this->normalizers[$configFilter->getName()]; | ||
$normalizer->normalize($builder, $filter, $image, $options, $configFilter->getArguments()); | ||
} | ||
|
||
return $builder->build(); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/Bridge/Gumlet/Normalizer/Config/Normalizer/CropConfigNormalizer.php
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 declare(strict_types = 1); | ||
|
||
namespace Contributte\Imagist\Bridge\Gumlet\Normalizer\Config\Normalizer; | ||
|
||
use Contributte\Imagist\Bridge\Gumlet\GumletBuilder; | ||
use Contributte\Imagist\Bridge\Gumlet\Normalizer\Config\GumletConfigNormalizerInterface; | ||
use Contributte\Imagist\Entity\ImageInterface; | ||
use Contributte\Imagist\Filter\FilterInterface; | ||
|
||
final class CropConfigNormalizer implements GumletConfigNormalizerInterface | ||
{ | ||
|
||
public function getName(): string | ||
{ | ||
return 'crop'; | ||
} | ||
|
||
public function normalize( | ||
GumletBuilder $builder, | ||
FilterInterface $filter, | ||
ImageInterface $image, | ||
array $options, | ||
array $arguments | ||
): void | ||
{ | ||
[$mode] = $arguments; | ||
|
||
$builder->crop($mode); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/Bridge/Gumlet/Normalizer/Config/Normalizer/ResizeConfigNormalizer.php
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,35 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Contributte\Imagist\Bridge\Gumlet\Normalizer\Config\Normalizer; | ||
|
||
use Contributte\Imagist\Bridge\Gumlet\GumletBuilder; | ||
use Contributte\Imagist\Bridge\Gumlet\Normalizer\Config\GumletConfigNormalizerInterface; | ||
use Contributte\Imagist\Entity\ImageInterface; | ||
use Contributte\Imagist\Filter\FilterInterface; | ||
|
||
final class ResizeConfigNormalizer implements GumletConfigNormalizerInterface | ||
{ | ||
|
||
public function getName(): string | ||
{ | ||
return 'resize'; | ||
} | ||
|
||
public function normalize( | ||
GumletBuilder $builder, | ||
FilterInterface $filter, | ||
ImageInterface $image, | ||
array $options, | ||
array $arguments | ||
): void | ||
{ | ||
[$width, $height, $mode] = array_replace([ | ||
null, | ||
null, | ||
null, | ||
], $arguments); | ||
|
||
$builder->resize($width, $height, $mode); | ||
} | ||
|
||
} |
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,54 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Contributte\Imagist\Bridge\Imagine\Config; | ||
|
||
use Contributte\Imagist\Bridge\Imagine\OperationInterface; | ||
use Contributte\Imagist\Config\ConfigFilterStack; | ||
use Contributte\Imagist\Exceptions\UnexpectedErrorException; | ||
use Contributte\Imagist\Filter\FilterInterface; | ||
use Contributte\Imagist\Scope\Scope; | ||
use Imagine\Image\ImageInterface; | ||
use InvalidArgumentException; | ||
|
||
final class ImagineConfigFilterRegistry implements OperationInterface | ||
{ | ||
|
||
/** @var ImagineConfigOperationInterface[] */ | ||
protected array $operations = []; | ||
|
||
/** @var ConfigFilterStack[] */ | ||
protected array $configFilterStacks = []; | ||
|
||
public function addConfigFilterStack(ConfigFilterStack $configFilterStack): void | ||
{ | ||
$this->configFilterStacks[$configFilterStack->getName()] = $configFilterStack; | ||
} | ||
|
||
public function addOperation(ImagineConfigOperationInterface $operation): void | ||
{ | ||
$this->operations[$operation->getName()] = $operation; | ||
} | ||
|
||
public function supports(FilterInterface $filter, Scope $scope): bool | ||
{ | ||
return isset($this->configFilterStacks[$filter->getName()]); | ||
} | ||
|
||
public function operate(ImageInterface $image, FilterInterface $filter): void | ||
{ | ||
if (!isset($this->configFilterStacks[$filter->getName()])) { | ||
throw new UnexpectedErrorException(); | ||
} | ||
|
||
foreach ($this->configFilterStacks[$filter->getName()]->getConfigFilters() as $configFilter) { | ||
if (!isset($this->operations[$configFilter->getName()])) { | ||
throw new InvalidArgumentException(sprintf('Config operation %s not exists.', $configFilter->getName())); | ||
} | ||
|
||
$operation = $this->operations[$configFilter->getName()]; | ||
|
||
$operation->operate($image, $filter, $configFilter->getArguments()); | ||
} | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/Bridge/Imagine/Config/ImagineConfigOperationInterface.php
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,18 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Contributte\Imagist\Bridge\Imagine\Config; | ||
|
||
use Contributte\Imagist\Filter\FilterInterface; | ||
use Imagine\Image\ImageInterface; | ||
|
||
interface ImagineConfigOperationInterface | ||
{ | ||
|
||
public function getName(): string; | ||
|
||
/** | ||
* @param mixed[] $arguments | ||
*/ | ||
public function operate(ImageInterface $image, FilterInterface $filter, array $arguments): void; | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/Bridge/Imagine/Config/Operation/ResizeConfigOperation.php
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 declare(strict_types = 1); | ||
|
||
namespace Contributte\Imagist\Bridge\Imagine\Config\Operation; | ||
|
||
use Contributte\Imagist\Bridge\Imagine\Config\ImagineConfigOperationInterface; | ||
use Contributte\Imagist\Filter\FilterInterface; | ||
use Imagine\Image\Box; | ||
use Imagine\Image\ImageInterface; | ||
use InvalidArgumentException; | ||
|
||
final class ResizeConfigOperation implements ImagineConfigOperationInterface | ||
{ | ||
|
||
public function getName(): string | ||
{ | ||
return 'resize'; | ||
} | ||
|
||
/** | ||
* @param mixed[] $arguments | ||
*/ | ||
public function operate(ImageInterface $image, FilterInterface $filter, array $arguments): void | ||
{ | ||
if (count($arguments) !== 2) { | ||
throw new InvalidArgumentException('Config filter resize must have two arguments.'); | ||
} | ||
|
||
$image->resize(new Box(...$arguments)); | ||
} | ||
|
||
} |
Oops, something went wrong.