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

Add notes on basic usage #651

Merged
merged 1 commit into from
Oct 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
94 changes: 94 additions & 0 deletions Resources/doc/basic-usage.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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:

.. code-block:: yaml

# app/config/config.yml

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this empty line should be removed according to Symfony doc standards

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:

.. code-block:: jinja
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has to be html+jinja as it contains HTML markup as well.


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

Or if you're using PHP templates:

.. code-block:: php

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these can be put together in a code example that uses tabs for different languages:

.. configuration-block:

    .. code-block:: html+jinja

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

    .. code-block:: html+php

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

btw, /relative/path/to/image.jpg doesn't look so relative 😉


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:

.. code-block:: jinja

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

Or if you're using PHP templates:

.. code-block:: php

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comments on these 2 code blocks


Also you can resolve image url from console:

.. code-block:: jinja
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a terminal, use bash instead of jinja


app/console liip:imagine:cache:resolve relative/path/to/image.jpg relative/path/to/image2.jpg --filters=my_thumb --filters=thumbnail_default
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this doesn't work on Windows, you should use php app/console (which works on all OSes). Also, Symfony doc standard is to prefix all command with $:

$ php app/console liip:imagine:cache:resolve ...


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line should be wrapped after the first word that crosses the 72th character


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

.. code-block:: php

$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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reStructured Text uses double backticks to indicate inline code blocks (single backticks are references): /media/cache/my_thumb/S8rrlhhQ/relative/path/to/image.jpg

would save the filtered image file.

.. note::
Using the ``dev`` environment you might find that the images are not properly rendered when
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be an empty line between the directive indicator line (.. note::) and the directive body

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:

.. code-block:: jinja
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I did this correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah .. not sure if one can nest like this ..

@wouterj ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you can


web_profiler:
intercept_redirects: false
1 change: 1 addition & 0 deletions Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ performance, altered images can be cached locally and in Amazon S3 service.

* :doc:`Installation <installation>`
* :doc:`Introduction <introduction>`
* :doc:`Basic usage <basic-usage>`
* :doc:`Configuration <configuration>`
* :doc:`Filters <filters>`
* :doc:`DataLoaders <data-loaders>`
Expand Down