Skip to content

Commit cdf007b

Browse files
authored
Merge pull request #789 from robfrawley/feature-update-docs
Updated README.md and RST Documentation
2 parents 683d558 + 139f816 commit cdf007b

29 files changed

+2547
-878
lines changed

README.md

+322-106
Large diffs are not rendered by default.

Resources/doc/basic-usage.rst

+153-36
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,148 @@
1+
2+
13
Basic Usage
24
===========
35

4-
This bundle works by configuring a set of filters and then applying those
5-
filters to images inside a template. So, start by creating some sort of filter
6-
that you need to apply somewhere in your application. For example, suppose
7-
you want to thumbnail an image to a size of 120x90 pixels:
6+
Generally, this bundle works by applying filter sets to images from inside
7+
a template. Your filter sets are defined within the application's configuration
8+
file (often ``app/config/config.yml``) and are comprised of a collection of
9+
filters, post-processors, and other optional parameters.
10+
11+
We'll learn more about post-processors and other available parameters later,
12+
but for now lets focus on how to define a simple filter set comprised of a
13+
few filters.
14+
15+
.. _usage-create-thumbnails:
16+
17+
Create Thumbnails
18+
-----------------
19+
20+
Before we get started, there is a small amount of configuration needed to ensure
21+
our :doc:`data loaders <data-loaders>` and :doc:`cache-resolvers <cache-resolvers>`
22+
operate correctly. Use the following configuration boilerplate.
23+
24+
.. code-block:: yaml
25+
26+
# app/config/config.yml
27+
28+
liip_imagine:
29+
30+
# configure resolvers
31+
resolvers:
32+
33+
# setup the default resolver
34+
default:
35+
36+
# use the default web path
37+
web_path: ~
38+
39+
# your filter sets are defined here
40+
filter_sets:
41+
42+
# use the default cache configuration
43+
cache: ~
44+
45+
With the basic configuration in place, we'll start with an example that fulfills a
46+
common use-case: creating thumbnails. Lets assume we want the resulting thumbnails
47+
to have the following transformations applied to them:
48+
49+
* Scale and crop the image to 120x90px.
50+
* Add a 2px black border to the scaled image.
51+
* Adjust the image quality to 75.
52+
53+
Adding onto our boilerplate from above, we need to define a filter set (which we'll
54+
name ``my_thumb``) with two filters configured: the ``thumbnail`` and ``background``
55+
filters.
856

957
.. code-block:: yaml
1058
1159
# app/config/config.yml
60+
1261
liip_imagine:
1362
resolvers:
1463
default:
1564
web_path: ~
1665
1766
filter_sets:
1867
cache: ~
68+
69+
# the name of the "filter set"
1970
my_thumb:
71+
72+
# adjust the image quality to 75%
2073
quality: 75
74+
75+
# list of transformations to apply (the "filters")
2176
filters:
77+
78+
# create a thumbnail: set size to 120x90 and use the "outbound" mode
79+
# to crop the image when the size ratio of the input differs
2280
thumbnail: { size: [120, 90], mode: outbound }
2381
24-
You've now defined a filter set called ``my_thumb`` that performs a thumbnail
25-
transformation. We'll learn more about available transformations later, but
26-
for now, this new filter can be used immediately in a template:
82+
# create a 2px black border: center the thumbnail on a black background
83+
# 4px larger to create a 2px border around the final image
84+
background: { size: [124, 94], position: center, color: '#000000' }
85+
86+
You've now created a filter set called ``my_thumb`` that performs a thumbnail
87+
transformation. The ``thumbnail`` filter sizes the image to the desired width
88+
and height (120x90px), and its ``mode: outbound`` option causes
89+
the resulting image to be cropped if the input ratio differs. The ``background``
90+
filter results in a 2px black border by creating a black canvas 124x94px in size,
91+
and positioning the thumbnail at its center.
92+
93+
.. note::
94+
95+
A filter set can have any number of filters defined for it. Simple
96+
transformations may only require a single filter, while more complex
97+
transformations can have any number of filters defined for them.
98+
99+
There are a number of additional :doc:`filters <filters>`, but for now you can use
100+
your newly defined ``my_thumb`` filter set immediately within a template.
27101

28102
.. configuration-block::
29103

30-
.. code-block:: html+jinja
104+
.. code-block:: html+twig
31105

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

34108
.. code-block:: html+php
35109

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

38-
Behind the scenes, the bundles applies the filter(s) to the image on the
39-
first request and then caches the image to a similar path. On the next request,
40-
the cached image would be served directly from the file system.
112+
Behind the scenes, the bundle applies the filter(s) to the image on-the-fly
113+
when the first page request is served. The transformed image is then cached
114+
for subsequent requests. The final cached image path would be similar to
115+
``/media/cache/my_thumb/relative/path/to/image.jpg``.
116+
117+
.. note::
118+
119+
Using the ``dev`` environment you might find that images are not properly
120+
rendered via the template helper. This is often caused by having
121+
``intercept_redirect`` enabled in your application configuration. To ensure
122+
images are rendered, it is strongly suggested to disable this option:
123+
124+
.. code-block:: yaml
125+
126+
# app/config/config_dev.yml
127+
128+
web_profiler:
129+
intercept_redirects: false
130+
41131
42-
In this example, the final rendered path would be something like
43-
``/media/cache/my_thumb/relative/path/to/image.jpg``. This is where Imagine
44-
would save the filtered image file.
132+
Runtime Options
133+
---------------
45134

46-
You can also pass some options at runtime:
135+
Sometimes, you may have a filter defined that fulfills 99% of your usage
136+
scenarios. Instead of defining a new filter for the erroneous 1% of cases,
137+
you may instead choose to alter the behavior of a filter at runtime by
138+
passing the template helper an options array.
47139

48140
.. configuration-block::
49141

50-
.. code-block:: html+jinja
142+
.. code-block:: html+twig
51143

52144
{% set runtimeConfig = {"thumbnail": {"size": [50, 50] }} %}
145+
53146
<img src="{{ '/relative/path/to/image.jpg' | imagine_filter('my_thumb', runtimeConfig) }}" />
54147

55148
.. code-block:: html+php
@@ -62,35 +155,59 @@ You can also pass some options at runtime:
62155
);
63156
?>
64157

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

67-
Also you can resolve image url from console:
161+
Path Resolution
162+
---------------
163+
164+
Sometimes you need to resolve the image path returned by this bundle for a
165+
filtered image. This can easily be achieved using Symfony's console binary
166+
or pragmatically from within a controller or other piece of code.
167+
168+
169+
Resolve with the Console
170+
~~~~~~~~~~~~~~~~~~~~~~~~
171+
172+
You can resolve an image URL using the console command
173+
``liip:imagine:cache:resolve``. The only required argument is one or more
174+
relative image paths (which must be separated by a space).
68175

69176
.. code-block:: bash
70177
71-
$ php app/console liip:imagine:cache:resolve relative/path/to/image.jpg relative/path/to/image2.jpg --filters=my_thumb --filters=thumbnail_default
178+
$ php app/console liip:imagine:cache:resolve relative/path/to/image1.jpg relative/path/to/image2.jpg
72179
73-
Where only paths required parameter. They are separated by space. If you
74-
omit filters option will be applied all available filters.
180+
Additionally, you can use the ``--filters`` option to specify which filter
181+
you want to resolve for (if the ``--filters`` option is omitted, all
182+
available filters will be resolved).
75183

76-
If you need to access filtered image URL in your controller:
184+
.. code-block:: bash
77185
78-
.. code-block:: php
186+
$ php app/console liip:imagine:cache:resolve relative/path/to/image1.jpg --filters=my_thumb
79187
80-
$this->get('liip_imagine.cache.manager')->getBrowserPath('/relative/path/to/image.jpg', 'my_thumb')
81188
82-
In this case, the final rendered path would contain some random data in the
83-
path ``/media/cache/my_thumb/S8rrlhhQ/relative/path/to/image.jpg``. This is where
84-
Imagine would save the filtered image file.
189+
Resolve Pragmatically
190+
~~~~~~~~~~~~~~~~~~~~~
85191

86-
.. note::
192+
You can resolve the image URL in your code using the ``getBrowserPath``
193+
method of the ``liip_imagine.cache.manager`` service. Assuming you already
194+
have the service assigned to a variable called ``$imagineCacheManager``,
195+
you would run:
87196

88-
Using the ``dev`` environment you might find that the images are not properly
89-
rendered when using the template helper. This is likely caused by having
90-
``intercept_redirect`` enabled in your application configuration. To ensure
91-
that the images are rendered disable this option:
197+
.. code-block:: php
92198
93-
.. code-block:: yaml
199+
$imagineCacheManager->getBrowserPath('/relative/path/to/image.jpg', 'my_thumb');
94200
95-
web_profiler:
96-
intercept_redirects: false
201+
Often, you need to perform this operation in a controller. Assuming your
202+
controller inherits from the base Symfony controller, you can take advantage
203+
of the inherited ``get`` method to request the ``liip_imagine.cache.manager``
204+
service, from which you can call ``getBrowserPath`` on a relative image
205+
path to get its resolved location.
206+
207+
.. code-block:: php
208+
209+
/** @var CacheManager */
210+
$imagineCacheManager = $this->get('liip_imagine.cache.manager');
211+
212+
/** @var string */
213+
$resolvedPath = $this->getBrowserPath('/relative/path/to/image.jpg', 'my_thumb');

Resources/doc/cache-manager.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
CacheManager
2-
============
31

4-
Cache removal
2+
3+
Cache Manager
4+
=============
5+
6+
Cache Removal
57
-------------
68

79
CacheManager allows to remove cache in various ways.

0 commit comments

Comments
 (0)