Skip to content
This repository

Symfony2 Bundle to assist in imagine manipulation using the imagine library

Merge pull request #396 from formapro-forks/configuration-is-not-arra…

…y-given

[config] correctly process resolvers\loaders section if not array or null
latest commit 2be6f95769
Maksim Kotlyar makasim authored
Octocat-spinner-32 Binary Move data loaders to binary folder April 04, 2014
Octocat-spinner-32 Command Merge pull request #387 from formapro-forks/remove-cli-command April 07, 2014
Octocat-spinner-32 Controller add path prefix to exception message. March 14, 2014
Octocat-spinner-32 DependencyInjection [config] correctly process resolvers\loaders section if not array or … April 14, 2014
Octocat-spinner-32 Events changed comments April 10, 2014
Octocat-spinner-32 Exception throw NotStorableException if cannot store cache. add null logger. March 14, 2014
Octocat-spinner-32 Form Add link filter June 20, 2013
Octocat-spinner-32 Imagine fix getBaseUrl function for windows platform April 14, 2014
Octocat-spinner-32 Model [data loader] rework to return binary instance. add mime type guesser. March 14, 2014
Octocat-spinner-32 Resources Added doc for cache and proxies options in AwsS3 cache resolver April 14, 2014
Octocat-spinner-32 Routing [filter-config] add tests for all method. March 14, 2014
Octocat-spinner-32 Templating [filter] add ability to configre filters at runtime. March 14, 2014
Octocat-spinner-32 Tests [config] correctly process resolvers\loaders section if not array or … April 14, 2014
Octocat-spinner-32 .gitignore removed image, added rule to gitignore April 04, 2014
Octocat-spinner-32 .travis.yml use phpunit from vendors on travis. March 14, 2014
Octocat-spinner-32 CHANGELOG.md deprecate the phpcr loader as CmfMediaBundle provides a better one now. September 17, 2013
Octocat-spinner-32 ImagineEvents.php Rename events. Remove dock blocks. Remove resolver from event. April 09, 2014
Octocat-spinner-32 LiipImagineBundle.php [config] Fix default loader not found bug. April 07, 2014
Octocat-spinner-32 README.md Documentation update fixes #389 April 09, 2014
Octocat-spinner-32 UPGRADE.md Renamed configuration keys for data loaders. Tests for data loaders m… April 04, 2014
Octocat-spinner-32 composer.json Tests for cli command to resolve cache April 03, 2014
Octocat-spinner-32 phpunit.xml.dist AbstractFilesystemResolverTest + CacheManagerTest February 08, 2013
README.md

LiipImagineBundle

This bundle is a fork of AvalancheImagineBundle which provides easy image manipulation support for Symfony2. The goal of the fork is to make the code more extensible and as a result applicable for more use cases.

For more details on the reason for the fork see: https://github.com/avalanche123/AvalancheImagineBundle/pull/25

For example with this bundle the following is possible:

<img src="{{ '/relative/path/to/image.jpg' | imagine_filter('thumbnail') }}" />

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".

Build Status Total Downloads Latest Stable Version

Installation

In case you are not sure how to install this bundle, see the installation instructions.

Configuration

After installing the bundle, make sure you add this route to your routing:

# app/config/routing.yml

_imagine:
    resource: .
    type:     imagine

For a complete configuration drill-down see the respective chapter in the documentation.

Basic Usage

This bundle works by configuring a set of filters and then applying those filters to images inside a template So, start by creating some sort of filter that you need to apply somewhere in your application. For example, suppose you want to thumbnail an image to a size of 120x90 pixels:

# app/config/config.yml

liip_imagine:
    resolvers:
       default:
          web_path: ~

    filter_sets:
        cache: ~
        my_thumb:
            quality: 75
            filters:
                thumbnail: { size: [120, 90], mode: outbound }

You've now defined a filter set called my_thumb that performs a thumbnail transformation. We'll learn more about available transformations later, but for now, this new filter can be used immediately in a template:

<img src="{{ '/relative/path/to/image.jpg' | imagine_filter('my_thumb') }}" />

Or if you're using PHP templates:

<img src="<?php $this['imagine']->filter('/relative/path/to/image.jpg', 'my_thumb') ?>" />

Behind the scenes, the bundles applies the filter(s) to the image on the first request and then caches the image to a similar path. On the next request, the cached image would be served directly from the file system.

In this example, the final rendered path would be something like /media/cache/my_thumb/relative/path/to/image.jpg. This is where Imagine would save the filtered image file.

You can also pass some options at runtime:

{% set runtimeConfig = {"thumbnail": {"size": [50, 50] }} %}
<img src="{{ '/images/dream.jpg' | imagine_filter('thumbnail_web_path', runtimeConfig) }}" />

Or if you're using PHP templates:

<?php
$runtimeConfig = array(
    "thumbnail" => array(
        "size" => array(50, 50)
    )
);
?>

<img src="<?php $this['imagine']->filter('/relative/path/to/image.jpg', 'my_thumb', $runtimeConfig) ?>" />

Also you can resolve image url from console:

app/console liip:imagine:cache:resolve images/dream.jpg images/dream2.jpg --filters=thumbnail_web_path --filters=thumbnail_default

Where only paths required parameter. They are separated by space. If you omit filters option will be applied all available filters.

If you need to access filtered image URL in your controller:

$this->get('liip_imagine.cache.manager')->getBrowserPath('/relative/path/to/image.jpg', 'my_thumb', true),

In this case, the final rendered path would contain some random data in the path /media/cache/my_thumb/S8rrlhhQ/relative/path/to/image.jpg. This is where Imagine would save the filtered image file.

Note: Using the dev environment you might find that the images are not properly rendered when using the template helper. This is likely caused by having intercept_redirect enabled in your application configuration. To ensure that the images are rendered disable this option:

web_profiler:
    intercept_redirects: false

Filters

The LiipImagineBundle provides a set of built-in filters. You may easily roll your own filter, see the filters chapter in the documentation.

Using the controller as a service

If you need to use the filters in a controller, you can just load ImagineController.php controller as a service and handle the response:

class MyController extends Controller
{
    public function indexAction()
    {
        // RedirectResponse object
        $imagemanagerResponse = $this->container
            ->get('liip_imagine.controller')
                ->filterAction(
                    'uploads/foo.jpg',      // original image you want to apply a filter to
                    'my_thumb'              // filter defined in config.yml
        );

        // string to put directly in the "src" of the tag <img>
        $cacheManager = $this->container->get('liip_imagine.cache.manager');
        $srcPath = $cacheManager->getBrowserPath('uploads/foo.jpg', 'my_thumb');

        // ..
    }
}

In case you need to add more logic the recommended solution is to either extend ImagineController.php controller or take the code from that controller as a basis for your own controller.

Outside the web root

When your setup requires your source images to live outside the web root, or if that's just the way you roll, you have to set your loader's parameter data_root in the config.yml with the absolute path where your source images are located:

liip_imagine:
    loaders:
        default:
            filesystem:
                data_root: /path/to/source/images/dir

Afterwards, you need to grant read access on Apache to access the images source directory. For achieving it you have to add the following directive to your project's vhost file:

<VirtualHost *:80>
    <!-- Rest of directives like DocumentRoot or ServerName -->

    Alias /FavouriteAlias /path/to/source/images/dir
    <Directory "/path/to/source/images/dir">
        AllowOverride None
        Allow from All
    </Directory>
</VirtualHost>

Another way would be placing the directive in a separate file living inside your project. For instance, you can create a file app/config/apache/photos.xml and add to the project's vhost the following directive:

<VirtualHost *:80>
    <!-- Rest of directives like DocumentRoot or ServerName -->

    Include "/path/to/your/project/app/config/apache/photos.xml"
</VirtualHost>

This way you keep the file along with your code and you are able to change your files directory access easily or create different environment-dependant configuration files.

Either way, once you have granted access on Apache to read the data_root files, the relative path of an image with this absolute path /path/to/source/images/dir/logo.png must be /FavouriteAlias/logo.png to be readable.

Documentation

For more detailed information about the features of this bundle, please refer to the documentation.

Something went wrong with that request. Please try again.