Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #205

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
89a3347
Add Crop filter support
ardianys Feb 21, 2013
bf0c349
Add service config for new crop filter
ardianys Feb 21, 2013
151e1f1
Add docs for new crop filter
ardianys Feb 21, 2013
919b188
Merge pull request #123 from ardianys/develop
avalanche123 Mar 17, 2013
4c92935
catch exception when invalid filter is used
kbond Apr 11, 2013
1f68bb2
Merge pull request #136 from kbond/patch-1
avalanche123 Apr 11, 2013
3158bdb
Update composer.json
ejsidisi Apr 16, 2013
a465aac
added missing use statement
kbond Apr 16, 2013
5d4068e
Merge pull request #138 from kbond/patch-1
avalanche123 Apr 16, 2013
d64adf9
Merge pull request #137 from ejsidisi/develop
avalanche123 Apr 16, 2013
f194696
Fixed bug with symfony 2.3
Bup3 May 7, 2013
99d1a44
Check if $cached is not a directory to avoid error on unlink
EricGrivilers May 7, 2013
db4216e
Merge branch 'develop'
Bup3 May 7, 2013
eba1fa0
Merge recursively the config
yanickouellet May 31, 2013
3cf61e7
Fixing bug in cached content verifying (switching from 'file_exists' …
Jul 31, 2013
ae27b50
Fixing bug related to concurrency in dir creation. See PHP bug #35326.
Jul 31, 2013
5e3fbe8
Merge pull request #153 from yanickouellet/merge-recursive
avalanche123 Aug 29, 2013
1444421
Merge pull request #160 from straube/develop
avalanche123 Aug 29, 2013
5c29c78
fixed link to imagine library
josaliba Dec 24, 2013
9d390d2
Merge pull request #169 from josaliba/patch-1
avalanche123 Dec 24, 2013
6dc212c
can be overrided in filter options.
athlan May 10, 2014
61599a3
Merge branch 'feature-webroot-per-filter' into develop
athlan May 10, 2014
6e8eaad
Update composer.json
athlan May 10, 2014
3380daa
Merge pull request #181 from athlan/develop
avalanche123 Jun 7, 2014
3055eb3
Add permissions configuration setting to set specific chmod value on …
ChMat Aug 25, 2014
92142e6
Merge pull request #190 from ChMat/develop
avalanche123 Aug 25, 2014
fce1cce
Merge pull request #146 from caravane/develop
avalanche123 Aug 28, 2014
52c6005
Merge pull request #145 from Bup3/develop
avalanche123 Aug 28, 2014
21fbabb
Update README.md
JavierJimenez Sep 19, 2014
09007db
Merge pull request #191 from JavierJimenez/develop
avalanche123 Sep 19, 2014
3483480
Update README.md
rdohms Oct 31, 2014
8f47e02
Update README.md
avalanche123 Dec 4, 2014
d287162
Update imagine.xml
FlashBlack Mar 3, 2015
3a3abdc
Update CachePathResolver.php
FlashBlack Mar 3, 2015
0313dc3
Merge pull request #193 from rdohms/patch-1
avalanche123 Mar 3, 2015
8ee4e5f
Merge pull request #198 from FlashBlack/develop
avalanche123 Mar 3, 2015
74bfb4b
Update composer.json
i--storm Jul 25, 2016
a82c9d6
Merge remote-tracking branch 'origin/master' into develop
Jul 25, 2016
c846b1e
json fix
Nov 26, 2016
f912d63
js3 fix
Nov 26, 2016
f4c9f25
fix Twig_SimpleFilter
i--storm Feb 1, 2018
1fd042a
Update composer.json
i--storm Mar 17, 2021
1879cb1
imagine update
Oct 10, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use Avalanche\Bundle\ImagineBundle\Imagine\Filter\FilterManager;
use Imagine\Image\ImagineInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Avalanche\Bundle\ImagineBundle\Imagine\CacheManager;
use Symfony\Component\Routing\Exception\RouteNotFoundException;

class ImagineController
{
Expand Down Expand Up @@ -57,7 +59,11 @@ public function __construct(ImagineInterface $imagine, CacheManager $cacheManage
*/
public function filter($path, $filter)
{
$cachedPath = $this->cacheManager->cacheImage($this->request->getBaseUrl(), $path, $filter);
try {
$cachedPath = $this->cacheManager->cacheImage($this->request->getBaseUrl(), $path, $filter);
} catch (RouteNotFoundException $e) {
throw new NotFoundHttpException('Filter doesn\'t exist.');
}

// if cache path cannot be determined, return 404
if (null === $cachedPath) {
Expand Down Expand Up @@ -115,4 +121,16 @@ public function setRequest(Request $request = null)
{
$this->request = $request;
}

/**
* Set the request
*
* @param Request $request
*/
public function setRequestStack(RequestStack $request_stack = null)
{
if(!isset($this->request)) {
$this->request=$request_stack->getCurrentRequest();
}
}
}
2 changes: 1 addition & 1 deletion DependencyInjection/AvalancheImagineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private function mergeConfig(array $configs)
$config = array();

foreach ($configs as $cnf) {
$config = array_merge($config, $cnf);
$config = array_merge_recursive($config, $cnf);
}

return $config;
Expand Down
39 changes: 31 additions & 8 deletions Imagine/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ class CacheManager
* @param Filesystem $filesystem
* @param string $webRoot
* @param string $sourceRoot
* @param int $permissions
*/
public function __construct(
CachePathResolver $cachePathResolver,
ImagineInterface $imagine,
FilterManager $filterManager,
Filesystem $filesystem,
$webRoot,
$sourceRoot
$sourceRoot,
$permissions
)
{
$this->cachePathResolver = $cachePathResolver;
Expand All @@ -34,6 +36,7 @@ public function __construct(
$this->filesystem = $filesystem;
$this->webRoot = $webRoot;
$this->sourceRoot = $sourceRoot;
$this->permissions = $permissions;
}

/**
Expand Down Expand Up @@ -62,24 +65,32 @@ public function cacheImage($basePath, $path, $filter)
}

$realPath = $this->webRoot.$browserPath;
$sourcePath = $this->sourceRoot.$path;

$sourcePathRoot = $this->filterManager->getOption($filter, "source_root", $this->sourceRoot);
$sourcePath = $sourcePathRoot.$path;

// if the file has already been cached, just return path
if (file_exists($realPath)) {
if (is_file($realPath)) {
return $realPath;
}

if (!file_exists($sourcePath)) {
if (!is_file($sourcePath)) {
return null;
}

$dir = pathinfo($realPath, PATHINFO_DIRNAME);

if (!is_dir($dir)) {
if (false === $this->filesystem->mkdir($dir)) {
throw new \RuntimeException(sprintf(
'Could not create directory %s', $dir
));
try {
if (false === $this->filesystem->mkdir($dir)) {
throw new \RuntimeException(sprintf(
'Could not create directory %s', $dir
));
}
} catch (\Exception $e) {
if (!is_dir($dir)) {
throw $e;
}
}
}

Expand All @@ -92,6 +103,18 @@ public function cacheImage($basePath, $path, $filter)
))
;

try {
if (!chmod($realPath, $this->permissions))
{
throw new \RuntimeException(sprintf(
'Could not set permissions %s on image saved in %s', $this->permissions, $realPath
));
}

} catch (Exception $e) {
throw $e;
}

return $realPath;
}
}
17 changes: 12 additions & 5 deletions Imagine/CachePathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class CachePathResolver
* @var string
*/
private $webRoot;

/**
* @var string
*/
private $sourceRoot;

/**
* @var Symfony\Component\Routing\RouterInterface
Expand All @@ -21,11 +26,13 @@ class CachePathResolver
* Constructs cache path resolver with a given web root and cache prefix
*
* @param string $webRoot
* @param string $sourceRoot
* @param Symfony\Component\Routing\RouterInterface $router
*/
public function __construct($webRoot, RouterInterface $router)
public function __construct($webRoot, $sourceRoot, RouterInterface $router)
{
$this->webRoot = $webRoot;
$this->sourceRoot = $sourceRoot;
$this->router = $router;
}

Expand All @@ -38,11 +45,11 @@ public function __construct($webRoot, RouterInterface $router)
*/
public function getBrowserPath($path, $filter, $absolute = false)
{
// identify if current path is not under specified web root and return
// identify if current path is not under specified source root and return
// unmodified path in that case
$realPath = realpath($this->webRoot.$path);
$realPath = realpath($this->sourceRoot.$path);

if (!0 === strpos($realPath, $this->webRoot)) {
if (!0 === strpos($realPath, $this->sourceRoot)) {
return $path;
}

Expand All @@ -56,7 +63,7 @@ public function getBrowserPath($path, $filter, $absolute = false)

$cached = realpath($this->webRoot.$path);

if (file_exists($cached) && filemtime($realPath) > filemtime($cached)) {
if (file_exists($cached) && !is_dir($cached) && filemtime($realPath) > filemtime($cached)) {
unlink($cached);
}

Expand Down
20 changes: 20 additions & 0 deletions Imagine/Filter/Loader/CropFilterLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Avalanche\Bundle\ImagineBundle\Imagine\Filter\Loader;

use Imagine\Image\Box;
use Imagine\Image\Point;

use Imagine\Filter\Basic\Crop;
use Imagine\Image\ImageInterface;

class CropFilterLoader implements LoaderInterface
{
public function load(array $options = array())
{
list($x, $y) = $options['start'];
list($width, $height) = $options['size'];

return new Crop(new Point($x, $y), new Box($width, $height));
}
}
45 changes: 30 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Deprecated
==========

This project is no longer actively maintained, please find one of the populate forks. Thanks!

AvalancheImagineBundle
======================

Expand All @@ -12,7 +17,7 @@ This will perform the transformation called `thumbnail`, which you can define
to do a number of different things, such as resizing, cropping, drawing,
masking, etc.

This bundle integrates the standalone PHP "[Imagine library](/avalanche123/Imagine)".
This bundle integrates the standalone PHP "[Imagine library](https://github.com/avalanche123/Imagine)".

## Installation

Expand All @@ -26,18 +31,8 @@ Installation is a quick 3 step process:

Add AvalancheImagineBundle in your composer.json:

```js
{
"require": {
"avalanche123/imagine-bundle": "v2.1"
}
}
```

Now tell composer to download the bundle by running the command:

``` bash
$ php composer.phar update avalanche123/imagine-bundle
```sh
composer require avalanche123/imagine-bundle
```

Composer will install the bundle to your project's `vendor/avalanche123/imagine-bundle` directory.
Expand Down Expand Up @@ -209,6 +204,7 @@ Each filter that you specify have the following options:
- `type` - determine the type of filter to be used, refer to *Filters* section for more information
- `options` - options that should be passed to the specific filter type
- `path` - override the global `cache_prefix` and replace it with this path
- `source_root` - override the global `source_root` and replace it with this path

## Built-in Filters

Expand Down Expand Up @@ -313,6 +309,18 @@ avalanche_imagine:

```

### Crop

The `crop` filter crop an image with start coordinate, and size dimension.

``` yaml
avalanche_imagine:
filters:
crop:
type : crop
options: { start: [0, 0], size: [100, 100] } #crop image with 100x100 square box
```

## Load your Custom Filters

The ImagineBundle allows you to load your own custom filter classes. The only
Expand Down Expand Up @@ -361,12 +369,19 @@ will apply your filter to '.jpg', and then concatenate the result to

## Using as a service

You can also use ImagineBundle as a service and create the cache image from controller.
You can use ImagineBundle as a service and resolve the cached image path.
```php
$avalancheService = $this->get('imagine.cache.path.resolver');
```

Then, call the getBrowserPath and pass the original image webpath and the filter you want to use
```php
$cachedImage = $avalancheService->getBrowserPath($object->getWebPath(), 'my_thumb');
```
```

And also use ImagineBundle as a service and create the cache image from controller.
```php
$cacheManager = $this->get('imagine.cache.manager');
$cachedPath = $cacheManager->cacheImage($this->getRequest()->getBaseUrl(), '/images/picture1.jpg', 'my_filter');
```

12 changes: 12 additions & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<parameter key="imagine.source_root">%kernel.root_dir%/../web</parameter>
<parameter key="imagine.cache_prefix">media/cache</parameter>
<parameter key="imagine.filters" type="collection" />
<parameter key="imagine.permissions">0644</parameter>

<!-- Utility classes -->

Expand Down Expand Up @@ -49,6 +50,8 @@

<parameter key="imagine.filter.loader.chain.class">Avalanche\Bundle\ImagineBundle\Imagine\Filter\Loader\ChainFilterLoader</parameter>

<parameter key="imagine.filter.loader.crop.class">Avalanche\Bundle\ImagineBundle\Imagine\Filter\Loader\CropFilterLoader</parameter>

</parameters>

<services>
Expand All @@ -57,6 +60,7 @@

<service id="imagine.cache.path.resolver" class="%imagine.cache.path.resolver.class%">
<argument>%imagine.web_root%</argument>
<argument>%imagine.source_root%</argument>
<argument type="service" id="router" />
</service>

Expand All @@ -67,6 +71,7 @@
<argument type="service" id="filesystem" />
<argument>%imagine.web_root%</argument>
<argument>%imagine.source_root%</argument>
<argument>%imagine.permissions%</argument>
</service>

<service id="imagine.filter.manager" class="%imagine.filter.manager.class%">
Expand All @@ -82,6 +87,9 @@
<call method="setRequest">
<argument type="service" id="request" on-invalid="null" strict="false" />
</call>
<call method="setRequestStack">
<argument type="service" id="request_stack" on-invalid="null" strict="false" />
</call>
</service>

<!-- Route Loader -->
Expand Down Expand Up @@ -137,6 +145,10 @@
<argument type="service" id="imagine.filter.manager" />
<tag name="imagine.filter.loader" filter="chain" />
</service>

<service id="imagine.filter.loader.crop" class="%imagine.filter.loader.crop.class%">
<tag name="imagine.filter.loader" filter="crop" />
</service>

</services>
</container>
2 changes: 1 addition & 1 deletion Templating/ImagineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(CachePathResolver $cachePathResolver)
public function getFilters()
{
return array(
'apply_filter' => new \Twig_Filter_Method($this, 'applyFilter'),
new \Twig_SimpleFilter('apply_filter', array($this, 'applyFilter')),
);
}

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "avalanche123/imagine-bundle",
"name": "i-storm/avalanche-imagine-bundle",
"description": "Image manipulation using Imagine and Twig Filters.",
"keywords": ["image manipulation", "imagine"],
"homepage": "https://github.com/avalanche123/AvalancheImagineBundle",
Expand All @@ -20,8 +20,8 @@
}
],
"require": {
"symfony/framework-bundle": "2.*",
"imagine/Imagine": "v0.4.1"
"symfony/framework-bundle": "2.*|3.*",
"imagine/Imagine": "^1.3"
},
"require-dev": {
"makasim/temp-file": "dev-master"
Expand All @@ -33,4 +33,4 @@
"psr-0": { "Avalanche\\Bundle\\ImagineBundle": "" }
},
"target-dir": "Avalanche/Bundle/ImagineBundle"
}
}