1
+
2
+
1
3
Basic Usage
2
4
===========
3
5
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.
8
56
9
57
.. code-block :: yaml
10
58
11
59
# app/config/config.yml
60
+
12
61
liip_imagine :
13
62
resolvers :
14
63
default :
15
64
web_path : ~
16
65
17
66
filter_sets :
18
67
cache : ~
68
+
69
+ # the name of the "filter set"
19
70
my_thumb :
71
+
72
+ # adjust the image quality to 75%
20
73
quality : 75
74
+
75
+ # list of transformations to apply (the "filters")
21
76
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
22
80
thumbnail : { size: [120, 90], mode: outbound }
23
81
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.
27
101
28
102
.. configuration-block ::
29
103
30
- .. code-block :: html+jinja
104
+ .. code-block :: html+twig
31
105
32
- <img src="{{ '/relative/path/to/image.jpg'| imagine_filter('my_thumb') }}" />
106
+ <img src="{{ '/relative/path/to/image.jpg' | imagine_filter('my_thumb') }}" />
33
107
34
108
.. code-block :: html+php
35
109
36
110
<img src="<?php echo $this['imagine']->filter('/relative/path/to/image.jpg', 'my_thumb') ?>" />
37
111
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
+
41
131
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
+ ---------------
45
134
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.
47
139
48
140
.. configuration-block ::
49
141
50
- .. code-block :: html+jinja
142
+ .. code-block :: html+twig
51
143
52
144
{% set runtimeConfig = {"thumbnail": {"size": [50, 50] }} %}
145
+
53
146
<img src="{{ '/relative/path/to/image.jpg' | imagine_filter('my_thumb', runtimeConfig) }}" />
54
147
55
148
.. code-block :: html+php
@@ -62,35 +155,59 @@ You can also pass some options at runtime:
62
155
);
63
156
?>
64
157
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
+
66
160
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).
68
175
69
176
.. code-block :: bash
70
177
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
72
179
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).
75
183
76
- If you need to access filtered image URL in your controller:
184
+ .. code-block :: bash
77
185
78
- .. code-block :: php
186
+ $ php app/console liip:imagine:cache:resolve relative/path/to/image1.jpg --filters=my_thumb
79
187
80
- $this->get('liip_imagine.cache.manager')->getBrowserPath('/relative/path/to/image.jpg', 'my_thumb')
81
188
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
+ ~~~~~~~~~~~~~~~~~~~~~
85
191
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:
87
196
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
92
198
93
- .. code-block :: yaml
199
+ $imagineCacheManager->getBrowserPath('/relative/path/to/image.jpg', 'my_thumb');
94
200
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');
0 commit comments